Class: FunctionalLightService::Organizer::WithReducer
- Defined in:
- lib/functional-light-service/organizer/with_reducer.rb
Constant Summary collapse
- NOOP_AROUND_EACH_HANDLER =
Handler di default condiviso: prima veniva creata una classe anonima per ogni WithReducer senza around_each
->(_context, &block) { block.call }
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#organizer ⇒ Object
Returns the value of attribute organizer.
Instance Method Summary collapse
- #around_each(handler) ⇒ Object
- #around_each_handler ⇒ Object
-
#initialize(monitored_organizer = nil) ⇒ WithReducer
constructor
A new instance of WithReducer.
- #reduce(*actions) ⇒ Object
- #reduce_rollback(actions, index_of_failed_action = nil) ⇒ Object
- #with(data = {}) ⇒ Object
Constructor Details
#initialize(monitored_organizer = nil) ⇒ WithReducer
Returns a new instance of WithReducer.
9 10 11 |
# File 'lib/functional-light-service/organizer/with_reducer.rb', line 9 def initialize(monitored_organizer = nil) @organizer = monitored_organizer end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
6 7 8 |
# File 'lib/functional-light-service/organizer/with_reducer.rb', line 6 def context @context end |
#organizer ⇒ Object
Returns the value of attribute organizer.
7 8 9 |
# File 'lib/functional-light-service/organizer/with_reducer.rb', line 7 def organizer @organizer end |
Instance Method Details
#around_each(handler) ⇒ Object
23 24 25 26 |
# File 'lib/functional-light-service/organizer/with_reducer.rb', line 23 def around_each(handler) @around_each_handler = handler self end |
#around_each_handler ⇒ Object
28 29 30 |
# File 'lib/functional-light-service/organizer/with_reducer.rb', line 28 def around_each_handler @around_each_handler || NOOP_AROUND_EACH_HANDLER end |
#reduce(*actions) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/functional-light-service/organizer/with_reducer.rb', line 32 def reduce(*actions) raise "No action(s) were provided" if actions.empty? actions.flatten! actions.each_with_index.with_object(context) do |(action, index), current_context| invoke_action(current_context, action) rescue FailWithRollbackError reduce_rollback(actions, index) ensure # For logging yield(current_context, action) if block_given? end end |
#reduce_rollback(actions, index_of_failed_action = nil) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/functional-light-service/organizer/with_reducer.rb', line 47 def reduce_rollback(actions, index_of_failed_action = nil) reversable_actions(actions, index_of_failed_action) .reverse .reduce(context) do |context, action| if action.respond_to?(:rollback) action.rollback(context) else context end end end |
#with(data = {}) ⇒ Object
13 14 15 16 17 |
# File 'lib/functional-light-service/organizer/with_reducer.rb', line 13 def with(data = {}) @context = FunctionalLightService::Context.make(data) @context.organized_by = organizer self end |