Module: Pathway::Plugins::Base::DSLMethods
- Defined in:
- lib/pathway.rb
Instance Method Summary collapse
- #guard(cond, &bl) ⇒ Object
- #initialize(operation, input) ⇒ Object
-
#map(callable) ⇒ Object
Execute step and replace the current state completely.
- #run(&bl) ⇒ Object
- #sequence(with_seq, &bl) ⇒ Object
-
#set(callable, *args, to: @operation.result_key) ⇒ Object
Execute step and modify the former state setting the key.
-
#step(callable, *args) ⇒ Object
Execute step and preserve the former state.
Instance Method Details
#guard(cond, &bl) ⇒ Object
165 166 167 168 169 170 |
# File 'lib/pathway.rb', line 165 def guard(cond, &bl) cond = _callable(cond) sequence(-> seq, state { seq.call if cond.call(state) }, &bl) end |
#initialize(operation, input) ⇒ Object
125 126 127 128 |
# File 'lib/pathway.rb', line 125 def initialize(operation, input) @result = wrap(State.new(operation, input: input)) @operation = operation end |
#map(callable) ⇒ Object
Execute step and replace the current state completely
153 154 155 156 |
# File 'lib/pathway.rb', line 153 def map(callable) bl = _callable(callable) @result = @result.then(bl) end |
#run(&bl) ⇒ Object
130 131 132 133 |
# File 'lib/pathway.rb', line 130 def run(&bl) instance_eval(&bl) @result end |
#sequence(with_seq, &bl) ⇒ Object
158 159 160 161 162 163 |
# File 'lib/pathway.rb', line 158 def sequence(with_seq, &bl) @result.then do |state| seq = -> { @result = dup.run(&bl) } _callable(with_seq).call(seq, state) end end |
#set(callable, *args, to: @operation.result_key) ⇒ Object
Execute step and modify the former state setting the key
143 144 145 146 147 148 149 150 |
# File 'lib/pathway.rb', line 143 def set(callable, *args, to: @operation.result_key) bl = _callable(callable) @result = @result.then do |state| wrap(bl.call(state, *args)) .then { |value| state.update(to => value) } end end |
#step(callable, *args) ⇒ Object
Execute step and preserve the former state
136 137 138 139 140 |
# File 'lib/pathway.rb', line 136 def step(callable, *args) bl = _callable(callable) @result = @result.tee { |state| bl.call(state, *args) } end |