Class: FunctionalLightService::Organizer::WithReducer
- Defined in:
- lib/functional-light-service/organizer/with_reducer.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
Instance Method Summary collapse
- #around_each(handler) ⇒ Object
- #around_each_handler ⇒ Object
- #reduce(*actions) ⇒ Object
- #reduce_rollback(actions) ⇒ Object
- #with(data = {}) ⇒ Object
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
4 5 6 |
# File 'lib/functional-light-service/organizer/with_reducer.rb', line 4 def context @context end |
Instance Method Details
#around_each(handler) ⇒ Object
11 12 13 14 |
# File 'lib/functional-light-service/organizer/with_reducer.rb', line 11 def around_each(handler) @around_each_handler = handler self end |
#around_each_handler ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/functional-light-service/organizer/with_reducer.rb', line 16 def around_each_handler @around_each_handler ||= Class.new do def self.call(_context) yield end end end |
#reduce(*actions) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/functional-light-service/organizer/with_reducer.rb', line 24 def reduce(*actions) raise "No action(s) were provided" if actions.empty? actions.flatten! actions.each_with_object(context) do |action, current_context| invoke_action(current_context, action) rescue FailWithRollbackError reduce_rollback(actions) ensure # For logging yield(current_context, action) if block_given? end end |
#reduce_rollback(actions) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/functional-light-service/organizer/with_reducer.rb', line 39 def reduce_rollback(actions) reversable_actions(actions) .reverse .reduce(context) do |context, action| if action.respond_to?(:rollback) action.rollback(context) else context end end end |
#with(data = {}) ⇒ Object
6 7 8 9 |
# File 'lib/functional-light-service/organizer/with_reducer.rb', line 6 def with(data = {}) @context = FunctionalLightService::Context.make(data) self end |