Class: Waterfall::Chain

Inherits:
Base
  • Object
show all
Defined in:
lib/waterfall/predicates/chain.rb

Constant Summary collapse

MAPPING_ERROR_MESSAGE =
"When chaining waterfalls, you must pass a mapping hash to pass data from one to the other"

Instance Method Summary collapse

Methods inherited from Base

#waterfall?, #yield_args

Constructor Details

#initialize(root, mapping_or_var_name) ⇒ Chain

Returns a new instance of Chain.



4
5
6
# File 'lib/waterfall/predicates/chain.rb', line 4

def initialize(root, mapping_or_var_name)
  @root, @mapping_or_var_name = root, mapping_or_var_name
end

Instance Method Details

#callObject



8
9
10
11
12
13
14
15
16
# File 'lib/waterfall/predicates/chain.rb', line 8

def call
  output = yield(*yield_args)

  if waterfall?(output)
    map_waterfalls(output, @mapping_or_var_name || {})
  else
    @root.update_outflow(@mapping_or_var_name, output) if @mapping_or_var_name
  end
end

#map_waterfalls(child_waterfall, mapping) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/waterfall/predicates/chain.rb', line 18

def map_waterfalls(child_waterfall, mapping)
  child_waterfall.call unless child_waterfall.has_flown?

  raise IncorrectChainingArgumentError.new(MAPPING_ERROR_MESSAGE) unless mapping.is_a?(Hash)

  mapping.each do |k, v|
    @root.update_outflow(k, child_waterfall.outflow[v])
  end

  @root.send :_add_executed_flow, child_waterfall

  if child_waterfall.dammed?
    @root.dam child_waterfall.error_pool
  end

  self
end