Class: Aqueductron::SpontaneousJointPiece

Inherits:
Object
  • Object
show all
Includes:
PieceCommon
Defined in:
lib/aqueductron/partition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PieceCommon

#flow, #result?

Constructor Details

#initialize(paths, categorize, make_new_path) ⇒ SpontaneousJointPiece



8
9
10
11
12
# File 'lib/aqueductron/partition.rb', line 8

def initialize(paths, categorize, make_new_path)
  @categorize = categorize
  @make_new_path = make_new_path
  @paths = paths
end

Instance Attribute Details

#categorizeObject (readonly)

TODO: make private



7
8
9
# File 'lib/aqueductron/partition.rb', line 7

def categorize
  @categorize
end

#make_new_pathObject (readonly)

TODO: make private



7
8
9
# File 'lib/aqueductron/partition.rb', line 7

def make_new_path
  @make_new_path
end

#pathsObject (readonly)

TODO: make private



7
8
9
# File 'lib/aqueductron/partition.rb', line 7

def paths
  @paths
end

Instance Method Details

#eofObject



32
33
34
35
36
# File 'lib/aqueductron/partition.rb', line 32

def eof
  go = ->(v) { v.result? ? v : v.eof }
  new_map = paths.map_values(&go)
  construct_compound_result(new_map)
end

#receive(msg) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/aqueductron/partition.rb', line 15

def receive(msg)
  category = categorize.call(msg)
  new_map = if (paths.key? category)
    paths
  else
    paths[category] = make_new_path.call(category) # todo: don't update
    paths
  end
  go = ->(v) { v.result? ? v : v.receive(msg) }
  new_map[category] = go.call(new_map[category]) #todo: don't mutate
  if (new_map.values.all? &:result? )
    construct_compound_result(new_map)
  else
    SpontaneousJointPiece.new(new_map, categorize, make_new_path)
  end
end