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



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

def bind_with(bind_object)
  @bind_object = bind_object
  self
end

#clear!Object



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

def clear!
  clear_actions!
  unbind!
  clear_subject_with_errors!
  self
end

#clear_actions!Object



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

def clear_actions!
  @actions = {}
  self
end

#clear_subject_with_errors!Object



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

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

#errorsObject



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

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

#fail?Boolean

Returns:

  • (Boolean)


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

def fail?
  !success?
end

#on(actions_with_responses = {}) ⇒ Object



39
40
41
42
# File 'lib/light_operations/core.rb', line 39

def on(actions_with_responses = {})
  actions_assign(actions_with_responses, :success, :fail)
  self
end

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



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

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

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



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

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
# 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
  self
end

#success?Boolean

Returns:

  • (Boolean)


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

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

#unbind!Object



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

def unbind!
  @bind_object = nil
  self
end