Class: ActiveOperation::Base
- Inherits:
-
Object
- Object
- ActiveOperation::Base
- Includes:
- ActiveSupport::Callbacks, SmartProperties
- Defined in:
- lib/active_operation/base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#error ⇒ Object
Returns the value of attribute error.
-
#output ⇒ Object
Returns the value of attribute output.
Class Method Summary collapse
Instance Method Summary collapse
- #call ⇒ Object
- #completed? ⇒ Boolean
- #halted? ⇒ Boolean
-
#initialize(*args) ⇒ Base
constructor
A new instance of Base.
- #perform ⇒ Object
- #succeeded? ⇒ Boolean
Constructor Details
#initialize(*args) ⇒ Base
Returns a new instance of Base.
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/active_operation/base.rb', line 99 def initialize(*args) arity = self.class.inputs.count(&:positional?) arguments = args.shift(arity) attributes = args.last.kind_of?(Hash) ? args.pop : {} raise ArgumentError, "wrong number of arguments #{arguments.length + args.length} for #{arity}" unless args.empty? self.class.inputs.select(&:positional?).each_with_index do |input, index| attributes[input.name] = arguments[index] end super(attributes) end |
Instance Attribute Details
#error ⇒ Object
Returns the value of attribute error.
8 9 10 |
# File 'lib/active_operation/base.rb', line 8 def error @error end |
#output ⇒ Object
Returns the value of attribute output.
7 8 9 |
# File 'lib/active_operation/base.rb', line 7 def output @output end |
Class Method Details
.call(*args) ⇒ Object
23 24 25 |
# File 'lib/active_operation/base.rb', line 23 def call(*args) perform(*args) end |
.inputs ⇒ Object
27 28 29 |
# File 'lib/active_operation/base.rb', line 27 def inputs [] end |
.perform(*args) ⇒ Object
19 20 21 |
# File 'lib/active_operation/base.rb', line 19 def perform(*args) new(*args).call end |
.to_proc ⇒ Object
31 32 33 34 35 |
# File 'lib/active_operation/base.rb', line 31 def to_proc ->(*args) { perform(*args) } end |
Instance Method Details
#call ⇒ Object
133 134 135 |
# File 'lib/active_operation/base.rb', line 133 def call perform end |
#completed? ⇒ Boolean
150 151 152 |
# File 'lib/active_operation/base.rb', line 150 def completed? halted? || succeeded? end |
#halted? ⇒ Boolean
142 143 144 |
# File 'lib/active_operation/base.rb', line 142 def halted? state == :halted end |
#perform ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/active_operation/base.rb', line 113 def perform run_callbacks :execute do catch(:abort) do next if completed? @output = execute self.state = :succeeded end end run_callbacks :halted if halted? run_callbacks :succeeded if succeeded? self.output rescue => error self.state = :failed self.error = error run_callbacks :error raise end |
#succeeded? ⇒ Boolean
146 147 148 |
# File 'lib/active_operation/base.rb', line 146 def succeeded? state == :succeeded end |