Module: LightService::Action::Macros

Defined in:
lib/light-service/action.rb

Instance Method Summary collapse

Instance Method Details

#executed(*_args, &block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/light-service/action.rb', line 37

def executed(*_args, &block)
  define_singleton_method :execute do |context = Context.make|
    action_context = create_action_context(context)
    return action_context if action_context.stop_processing?

    # Store the action within the context
    action_context.current_action = self

    Context::KeyVerifier.verify_keys(action_context, self) do
      action_context.define_accessor_methods_for_keys(all_keys)

      catch(:jump_when_failed) do
        call_before_action(action_context)

        execute_action(action_context, &block)

        # Reset the stored action in case it was changed downstream
        action_context.current_action = self
        call_after_action(action_context)
      end
    end
  end
end

#expected_keysObject



29
30
31
# File 'lib/light-service/action.rb', line 29

def expected_keys
  @expected_keys ||= []
end

#expects(*args) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/light-service/action.rb', line 15

def expects(*args)
  if expect_key_having_default?(args)
    available_defaults[args.first] = args.last[:default]

    args = [args.first]
  end

  expected_keys.concat(args)
end

#promised_keysObject



33
34
35
# File 'lib/light-service/action.rb', line 33

def promised_keys
  @promised_keys ||= []
end

#promises(*args) ⇒ Object



25
26
27
# File 'lib/light-service/action.rb', line 25

def promises(*args)
  promised_keys.concat(args)
end

#rolled_backObject



61
62
63
64
65
66
67
68
69
70
# File 'lib/light-service/action.rb', line 61

def rolled_back
  msg = "`rolled_back` macro can not be invoked again"
  raise msg if respond_to?(:rollback)

  define_singleton_method :rollback do |context = {}|
    yield(context)

    context
  end
end