Module: Pathway::Plugins::Base::DSLMethods
- Defined in:
- lib/pathway.rb
Instance Method Summary collapse
- #around(execution_strategy, &steps) ⇒ Object (also: #sequence)
- #if_false(cond, &steps) ⇒ Object
- #if_true(cond, &steps) ⇒ Object (also: #guard)
- #initialize(state, operation) ⇒ Object
-
#map(callable) ⇒ Object
Execute step and replace the current state completely.
- #run(&steps) ⇒ Object
-
#set(callable, *args, to: @operation.result_key, **kwargs, &bl) ⇒ Object
Execute step and modify the former state setting the key.
-
#step(callable) ⇒ Object
Execute step and preserve the former state.
Instance Method Details
#around(execution_strategy, &steps) ⇒ Object Also known as: sequence
180 181 182 183 184 185 186 |
# File 'lib/pathway.rb', line 180 def around(execution_strategy, &steps) @result.then do |state| steps_runner = ->(dsl = self) { dsl.run(&steps) } _callable(execution_strategy).call(steps_runner, state) end end |
#if_false(cond, &steps) ⇒ Object
193 194 195 |
# File 'lib/pathway.rb', line 193 def if_false(cond, &steps) if_true(_callable(cond) >> :!.to_proc, &steps) end |
#if_true(cond, &steps) ⇒ Object Also known as: guard
188 189 190 191 |
# File 'lib/pathway.rb', line 188 def if_true(cond, &steps) cond = _callable(cond) around(->(runner, state) { runner.call if cond.call(state) }, &steps) end |
#initialize(state, operation) ⇒ Object
149 150 151 |
# File 'lib/pathway.rb', line 149 def initialize(state, operation) @result, @operation = wrap(state), operation end |
#map(callable) ⇒ Object
Execute step and replace the current state completely
175 176 177 178 |
# File 'lib/pathway.rb', line 175 def map(callable,...) bl = _callable(callable) @result = @result.then { |state| bl.call(state,...) } end |
#run(&steps) ⇒ Object
153 154 155 156 |
# File 'lib/pathway.rb', line 153 def run(&steps) instance_eval(&steps) @result end |
#set(callable, *args, to: @operation.result_key, **kwargs, &bl) ⇒ Object
Execute step and modify the former state setting the key
165 166 167 168 169 170 171 172 |
# File 'lib/pathway.rb', line 165 def set(callable, *args, to: @operation.result_key, **kwargs, &bl) bl = _callable(callable) @result = @result.then do |state| wrap(bl.call(state, *args, **kwargs, &bl)) .then { |value| state.update(to => value) } end end |
#step(callable) ⇒ Object
Execute step and preserve the former state
159 160 161 162 |
# File 'lib/pathway.rb', line 159 def step(callable,...) bl = _callable(callable) @result = @result.tee { |state| bl.call(state,...) } end |