Class: LightOperations::Core

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Rescuable
Defined in:
lib/light_operations/core.rb

Constant Summary collapse

MissingDependency =
Class.new(StandardError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dependencies = {}) ⇒ Core

Returns a new instance of Core.



14
15
16
# File 'lib/light_operations/core.rb', line 14

def initialize(dependencies = {})
  @dependencies = dependencies
end

Instance Attribute Details

#subjectObject (readonly)

Returns the value of attribute subject.



8
9
10
# File 'lib/light_operations/core.rb', line 8

def subject
  @subject
end

Class Method Details

.subject_name(method_name) ⇒ Object



10
11
12
# File 'lib/light_operations/core.rb', line 10

def self.subject_name(method_name)
  send(:define_method, method_name, proc { subject })
end

Instance Method Details

#bind_with(bind_object) ⇒ Object



69
70
71
72
# File 'lib/light_operations/core.rb', line 69

def bind_with(bind_object)
  @bind_object = bind_object
  self
end

#clear!Object



45
46
47
48
49
50
# File 'lib/light_operations/core.rb', line 45

def clear!
  clear_actions!
  unbind!
  clear_subject_with_errors!
  self
end

#clear_actions!Object



64
65
66
67
# File 'lib/light_operations/core.rb', line 64

def clear_actions!
  @actions = {}
  self
end

#clear_subject_with_errors!Object



57
58
59
60
61
62
# File 'lib/light_operations/core.rb', line 57

def clear_subject_with_errors!
  @subject = nil
  @fail_errors = nil
  @errors = nil
  self
end

#errorsObject



74
75
76
# File 'lib/light_operations/core.rb', line 74

def errors
  @errors ||= fail_errors || (subject.respond_to?(:errors) ? subject.errors : [])
end

#fail?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/light_operations/core.rb', line 78

def fail?
  !success?
end

#on(actions_with_responses = {}) ⇒ Object



40
41
42
43
# 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) ⇒ Object



35
36
37
38
# 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) ⇒ Object



30
31
32
33
# File 'lib/light_operations/core.rb', line 30

def on_success(binded_method = nil, &block)
  actions[:success] = binded_method || block
  self
end

#run(params = {}) ⇒ Object

do no.t override this method



19
20
21
22
23
24
25
26
27
28
# 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?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/light_operations/core.rb', line 82

def success?
  errors.respond_to?(:empty?) ? errors.empty? : !errors
end

#unbind!Object



52
53
54
55
# File 'lib/light_operations/core.rb', line 52

def unbind!
  @bind_object = nil
  self
end