Class: LightService::Organizer::WithReducer

Inherits:
Object
  • Object
show all
Defined in:
lib/light-service/organizer/with_reducer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#around_each_handlerObject (readonly)

Returns the value of attribute around_each_handler.



4
5
6
# File 'lib/light-service/organizer/with_reducer.rb', line 4

def around_each_handler
  @around_each_handler
end

#contextObject (readonly)

Returns the value of attribute context.



4
5
6
# File 'lib/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/light-service/organizer/with_reducer.rb', line 11

def around_each(handler)
  @around_each_handler = handler
  self
end

#reduce(*actions) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/light-service/organizer/with_reducer.rb', line 16

def reduce(*actions)
  raise "No action(s) were provided" if actions.empty?
  actions.flatten!

  actions.reduce(context) do |current_context, action|
    begin
      result = invoke_action(current_context, action)
    rescue FailWithRollbackError
      result = reduce_rollback(actions)
    ensure
      # For logging
      yield(current_context, action) if block_given?
    end

    result
  end
end

#reduce_rollback(actions) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/light-service/organizer/with_reducer.rb', line 34

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/light-service/organizer/with_reducer.rb', line 6

def with(data = {})
  @context = LightService::Context.make(data)
  self
end