Class: LightService::Organizer::WithReducer
- Inherits:
-
Object
- Object
- LightService::Organizer::WithReducer
- Defined in:
- lib/light-service/organizer/with_reducer.rb
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) ⇒ Object
- #with(data = {}) ⇒ Object
Constructor Details
#initialize(monitored_organizer = nil) ⇒ WithReducer
7 8 9 |
# File 'lib/light-service/organizer/with_reducer.rb', line 7 def initialize(monitored_organizer = nil) @organizer = monitored_organizer end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
4 5 6 |
# File 'lib/light-service/organizer/with_reducer.rb', line 4 def context @context end |
#organizer ⇒ Object
Returns the value of attribute organizer.
5 6 7 |
# File 'lib/light-service/organizer/with_reducer.rb', line 5 def organizer @organizer end |
Instance Method Details
#around_each(handler) ⇒ Object
17 18 19 20 |
# File 'lib/light-service/organizer/with_reducer.rb', line 17 def around_each(handler) @around_each_handler = handler self end |
#around_each_handler ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/light-service/organizer/with_reducer.rb', line 22 def around_each_handler @around_each_handler ||= Class.new do def self.call(_context) yield end end end |
#reduce(*actions) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/light-service/organizer/with_reducer.rb', line 30 def reduce(*actions) raise "No action(s) were provided" if actions.empty? @context.around_actions ||= around_each_handler 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
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/light-service/organizer/with_reducer.rb', line 46 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
11 12 13 14 15 |
# File 'lib/light-service/organizer/with_reducer.rb', line 11 def with(data = {}) @context = LightService::Context.make(data) @context.organized_by = organizer self end |