-
Notifications
You must be signed in to change notification settings - Fork 15
Predicates
Chain is the main predicate, what it does depends on what the block returns
# main waterfall
Wf.new
.chain(foo: :bar) do
# child waterfall
Wf.new.chain(:bar){ 1 }.chain(:baz){ 2 }.chain{ 3 }
end
The child waterfall would have the following outflow: { bar: 1, baz: 2 }
This illustrates that when the block returns a value which is not a waterfall, it stores the returned value of the block inside the name_or_mapping
key of the outflow
or doesn't store it if name_or_mapping
is nil
.
Be aware those are equivalent:
Wf.new.chain(:foo) { 1 }
Wf.new.chain{|outflow| outflow[:foo] = 1 }
Wf.new.chain{|outflow| outflow.foo = 1 }
Wf.new.chain{|outflow, waterfall| waterfall.update_outflow(:foo, 1) }
Wf.new.chain{|outflow, waterfall| waterfall.outflow.foo = 1 }
The main waterfall would have the following outflow: { foo: 1 }
The main waterfall above receives the child waterfall as a return value of its chain
block.
All waterfalls have independent outflows.
If name_or_mapping
is nil
, the main waterfall's outflow
wouldnt be affected by its child (but if the child is dammed, the parent will be dammed).
If name_or_mapping
is a hash
, the format must be read as { name_in_parent_waterfall: :name_from_child_waterfall}
. In the above example, the child returned an outflow
with a bar
key which has be renamed as foo
in the main one.
It may look useless, because most of the time you may not rename, but... It makes things clear. You know exactly what you expect and you know exactly that you dont expect the rest the child may provide.
This predicate must always be used followed with dam
like:
Wf.new
.chain(:foo) { 1 }
.when_falsy { true }
.dam { "this wouldnt be executed" }
.when_falsy { false }
.dam { "errrrr" }
.chain(:bar) { 2 }
.on_dam {|error_pool| puts error_pool }
If the block returns a falsy value, it executes the dam
block, which will store the returned value in the error_pool
.
Once the waterfall is dammed, all following chain
blocks are skipped (wont be executed). And all the following on_dam
block would be executed.
As a result the example above would return a waterfall object having its outflow
equal to { foo: 1 }
. Remember: it has been dammed before bar
would have been set.
Its error_pool
would be "errrrr"
and it would be puts
as a result of the on_dam
Be aware those are equivalent:
Wf.new.when_falsy{ false }.dam{ 'errrr' }
Wf.new.chain{ |outflow, waterfall| waterfall.dam('errrr') unless false }
Behaves the same as when_falsy
except it dams when its return value is truthy
Dams the flow with the object provided.
Its block is executed whenever the waterfall is dammed, skipped otherwise.
Wf.new
.when_falsy { false }
.on_dam {|error_pool, outflow, waterfall| puts error_pool }