Module: Pathway::Plugins::Base::DSLMethods
- Defined in:
- lib/pathway.rb
Instance Method Summary collapse
- #around(execution_strategy, &dsl_block) ⇒ Object (also: #sequence)
- #if_false(cond, &dsl_block) ⇒ Object
- #if_true(cond, &dsl_block) ⇒ Object (also: #guard)
- #initialize(state, operation) ⇒ Object
-
#map(callable) ⇒ Object
Execute step and replace the current state completely.
- #run(&bl) ⇒ 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, &dsl_block) ⇒ Object Also known as: sequence
177 178 179 180 181 182 183 |
# File 'lib/pathway.rb', line 177 def around(execution_strategy, &dsl_block) @result.then do |state| dsl_runner = ->(dsl = self) { @result = dsl.run(&dsl_block) } _callable(execution_strategy).call(dsl_runner, state) end end |
#if_false(cond, &dsl_block) ⇒ Object
190 191 192 193 |
# File 'lib/pathway.rb', line 190 def if_false(cond, &dsl_block) cond = _callable(cond) if_true(->(state) { !cond.call(state) }, &dsl_block) end |
#if_true(cond, &dsl_block) ⇒ Object Also known as: guard
185 186 187 188 |
# File 'lib/pathway.rb', line 185 def if_true(cond, &dsl_block) cond = _callable(cond) around(->(dsl_runner, state) { dsl_runner.call if cond.call(state) }, &dsl_block) end |
#initialize(state, operation) ⇒ Object
146 147 148 |
# File 'lib/pathway.rb', line 146 def initialize(state, operation) @result, @operation = wrap(state), operation end |
#map(callable) ⇒ Object
Execute step and replace the current state completely
172 173 174 175 |
# File 'lib/pathway.rb', line 172 def map(callable,...) bl = _callable(callable) @result = @result.then { |state| bl.call(state,...) } end |
#run(&bl) ⇒ Object
150 151 152 153 |
# File 'lib/pathway.rb', line 150 def run(&bl) instance_eval(&bl) @result end |
#set(callable, *args, to: @operation.result_key, **kwargs, &bl) ⇒ Object
Execute step and modify the former state setting the key
162 163 164 165 166 167 168 169 |
# File 'lib/pathway.rb', line 162 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
156 157 158 159 |
# File 'lib/pathway.rb', line 156 def step(callable,...) bl = _callable(callable) @result = @result.tee { |state| bl.call(state,...) } end |