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 { self.subject })
end

Instance Method Details

#bind_with(bind_object) ⇒ Object



67
68
69
70
# File 'lib/light_operations/core.rb', line 67

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



62
63
64
65
# File 'lib/light_operations/core.rb', line 62

def clear_actions!
  @actions = {}
  self
end

#clear_subject_with_errors!Object



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

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

#errorsObject



72
73
74
# File 'lib/light_operations/core.rb', line 72

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

#fail?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/light_operations/core.rb', line 76

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 = execute(params)
  execute_actions
  self
rescue => exception
  rescue_with_handler(exception) || raise
  execute_actions
  self
end

#success?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/light_operations/core.rb', line 80

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