class LightOperations::Core

Constants

MissingDependency

Attributes

bind_object[R]
dependencies[R]
fail_errors[R]
subject[R]

Public Class Methods

new(dependencies = {}) click to toggle source
# File lib/light_operations/core.rb, line 14
def initialize(dependencies = {})
  @dependencies = dependencies
end
subject_name(method_name) click to toggle source
# File lib/light_operations/core.rb, line 10
def self.subject_name(method_name)
  send(:define_method, method_name, proc { subject })
end

Public Instance Methods

bind_with(bind_object) click to toggle source
# File lib/light_operations/core.rb, line 69
def bind_with(bind_object)
  @bind_object = bind_object
  self
end
clear!() click to toggle source
# File lib/light_operations/core.rb, line 45
def clear!
  clear_actions!
  unbind!
  clear_subject_with_errors!
  self
end
clear_actions!() click to toggle source
# File lib/light_operations/core.rb, line 64
def clear_actions!
  @actions = {}
  self
end
clear_subject_with_errors!() click to toggle source
# File lib/light_operations/core.rb, line 57
def clear_subject_with_errors!
  @subject = nil
  @fail_errors = nil
  @errors = nil
  self
end
errors() click to toggle source
# File lib/light_operations/core.rb, line 74
def errors
  @errors ||= fail_errors || (subject.respond_to?(:errors) ? subject.errors : [])
end
fail?() click to toggle source
# File lib/light_operations/core.rb, line 78
def fail?
  !success?
end
on(actions_with_responses = {}) click to toggle source
# File lib/light_operations/core.rb, line 40
def on(actions_with_responses = {})
  actions_assign(actions_with_responses, :success, :fail)
  self
end
on_fail(binded_method = nil, &block) click to toggle source
# File lib/light_operations/core.rb, line 35
def on_fail(binded_method = nil, &block)
  actions[:fail] = binded_method || block
  self
end
on_success(binded_method = nil, &block) click to toggle source
# File lib/light_operations/core.rb, line 30
def on_success(binded_method = nil, &block)
  actions[:success] = binded_method || block
  self
end
run(params = {}) click to toggle source

do no.t override this method

# File lib/light_operations/core.rb, line 19
def run(params = {})
  clear_subject_with_errors!
  @subject = method(:execute).arity == 0 ? execute : execute(params)
  execute_actions
  self
rescue => exception
  rescue_with_handler(exception) || raise
  execute_actions
  self
end
success?() click to toggle source
# File lib/light_operations/core.rb, line 82
def success?
  errors.respond_to?(:empty?) ? errors.empty? : !errors
end
unbind!() click to toggle source
# File lib/light_operations/core.rb, line 52
def unbind!
  @bind_object = nil
  self
end

Protected Instance Methods

actions() click to toggle source
# File lib/light_operations/core.rb, line 110
def actions
  @actions ||= {}
end
actions_assign(hash, *keys) click to toggle source
# File lib/light_operations/core.rb, line 90
def actions_assign(hash, *keys)
  keys.each { |key| actions[key] = hash[key] if hash.key?(key) }
end
dependency(name) click to toggle source
# File lib/light_operations/core.rb, line 118
def dependency(name)
  dependencies.fetch(name)
rescue KeyError => e
  raise MissingDependency, e.message
end
execute(_params = {}) click to toggle source
# File lib/light_operations/core.rb, line 114
def execute(_params = {})
  fail 'Not implemented yet'
end
execute_action_kind(kind) click to toggle source
# File lib/light_operations/core.rb, line 98
def execute_action_kind(kind)
  return unless actions.key?(kind)
  action = actions[kind]
  bind_object.send(action, self) if action.is_a?(Symbol) && bind_object
  action.call(self) if action.is_a?(Proc)
end
execute_actions() click to toggle source
# File lib/light_operations/core.rb, line 94
def execute_actions
  success? ? execute_action_kind(:success) : execute_action_kind(:fail)
end
fail!(fail_obj = true) click to toggle source
# File lib/light_operations/core.rb, line 105
def fail!(fail_obj = true)
  @errors      = nil
  @fail_errors = fail_obj
end