module LightService::Action::Macros

Public Instance Methods

executed(*_args, &block) click to toggle source
# 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_keys() click to toggle source
# File lib/light-service/action.rb, line 29
def expected_keys
  @expected_keys ||= []
end
expects(*args) click to toggle source
# 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_keys() click to toggle source
# File lib/light-service/action.rb, line 33
def promised_keys
  @promised_keys ||= []
end
promises(*args) click to toggle source
# File lib/light-service/action.rb, line 25
def promises(*args)
  promised_keys.concat(args)
end
rolled_back() { |context| ... } click to toggle source
# 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

Private Instance Methods

all_keys() click to toggle source
# File lib/light-service/action.rb, line 106
def all_keys
  expected_keys + promised_keys
end
around_action_context?(context) click to toggle source
# File lib/light-service/action.rb, line 144
def around_action_context?(context)
  context.instance_of?(Context) &&
    context.around_actions.respond_to?(:call)
end
available_defaults() click to toggle source
# File lib/light-service/action.rb, line 85
def available_defaults
  @available_defaults ||= {}
end
call_after_action(context) click to toggle source
# File lib/light-service/action.rb, line 130
def call_after_action(context)
  invoke_callbacks(context[:_after_actions], context)
end
call_before_action(context) click to toggle source
# File lib/light-service/action.rb, line 126
def call_before_action(context)
  invoke_callbacks(context[:_before_actions], context)
end
create_action_context(context) click to toggle source
# File lib/light-service/action.rb, line 98
def create_action_context(context)
  usable_defaults(context).each do |ctx_key, default|
    context[ctx_key] = extract_default(default, context)
  end

  LightService::Context.make(context)
end
execute_action(context) { |context| ... } click to toggle source
# File lib/light-service/action.rb, line 74
def execute_action(context)
  if around_action_context?(context)
    context.around_actions.call(context) do
      yield(context)
      context
    end
  else
    yield(context)
  end
end
expect_key_having_default?(key) click to toggle source
# File lib/light-service/action.rb, line 89
def expect_key_having_default?(key)
  return false unless key.size == 2 && key.last.is_a?(Hash)
  return true if key.last.key?(:default)

  bad_key = key.last.keys.first
  err_msg = "Specify defaults with a `default` key. You have #{bad_key}."
  raise UnusableExpectKeyDefaultError, err_msg
end
extract_default(default, context) click to toggle source
# File lib/light-service/action.rb, line 120
def extract_default(default, context)
  return default unless default.respond_to?(:call)

  default.call(context)
end
invoke_callbacks(callbacks, context) click to toggle source
# File lib/light-service/action.rb, line 134
def invoke_callbacks(callbacks, context)
  return context unless callbacks

  callbacks.each do |cb|
    cb.call(context)
  end

  context
end
missing_expected_keys(context) click to toggle source
# File lib/light-service/action.rb, line 110
def missing_expected_keys(context)
  expected_keys - context.keys
end
usable_defaults(context) click to toggle source
# File lib/light-service/action.rb, line 114
def usable_defaults(context)
  available_defaults.slice(
    *(missing_expected_keys(context) & available_defaults.keys)
  )
end