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

Instance Method Summary collapse

Constructor Details

#initialize(dependencies = {}) ⇒ Core

Returns a new instance of Core.



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

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

Instance Attribute Details

#bind_objectObject (readonly)

Returns the value of attribute bind_object.



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

def bind_object
  @bind_object
end

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



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

def dependencies
  @dependencies
end

#subjectObject (readonly)

Returns the value of attribute subject.



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

def subject
  @subject
end

Instance Method Details

#bind_with(bind_object) ⇒ Object



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

def bind_with(bind_object)
  @bind_object = bind_object
  self
end

#clear!Object



41
42
43
44
45
46
# File 'lib/light_operations/core.rb', line 41

def clear!
  clear_actions!
  unbind!
  clear_subject_with_errors!
  self
end

#clear_actions!Object



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

def clear_actions!
  @actions = {}
  self
end

#clear_subject_with_errors!Object



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

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

#errorsObject



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

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

#fail?Boolean

Returns:

  • (Boolean)


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

def fail?
  !success?
end

#on(actions_with_responses = {}) ⇒ Object



34
35
36
37
38
39
# File 'lib/light_operations/core.rb', line 34

def on(actions_with_responses = {})
  actions_with_responses.slice(:success, :fail).each do |action, response|
    actions[action] = response
  end
  self
end

#on_fail(binded_method = nil, &block) ⇒ Object



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

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

#on_success(binded_method = nil, &block) ⇒ Object



24
25
26
27
# File 'lib/light_operations/core.rb', line 24

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

#run(params = {}) ⇒ Object

do no.t override this method



15
16
17
18
19
20
21
22
# File 'lib/light_operations/core.rb', line 15

def run(params = {})
  @subject = execute(params)
  execute_actions
  self
rescue => exception
  rescue_with_handler(exception) || raise
  self
end

#success?Boolean

Returns:

  • (Boolean)


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

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

#unbind!Object



48
49
50
51
# File 'lib/light_operations/core.rb', line 48

def unbind!
  @bind_object = nil
  self
end