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
- .call(*args) ⇒ Object
- .from_proc(execute) ⇒ Object
- .inputs ⇒ Object
- .perform(*args) ⇒ Object
- .to_proc ⇒ Object
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.
130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/active_operation/base.rb', line 130 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
54 55 56 |
# File 'lib/active_operation/base.rb', line 54 def call(*args) perform(*args) end |
.from_proc(execute) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/active_operation/base.rb', line 23 def from_proc(execute) Class.new(self) do positional_arguments = [] keyword_arguments = [] execute.parameters.each do |type, name| case type when :req input name, type: :positional, required: true positional_arguments << name when :keyreq input name, type: :keyword, required: true keyword_arguments << name else raise ArgumentError, "Argument type not supported: #{type}" end end define_method(:execute) do args = positional_arguments.map { |name| self[name] } opts = keyword_arguments.map { |name| [name, self[name]] }.to_h if opts.empty? execute.call(*args) else execute.call(*args, **opts) end end end end |
.inputs ⇒ Object
58 59 60 |
# File 'lib/active_operation/base.rb', line 58 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
62 63 64 65 66 |
# File 'lib/active_operation/base.rb', line 62 def to_proc ->(*args) { perform(*args) } end |
Instance Method Details
#call ⇒ Object
164 165 166 |
# File 'lib/active_operation/base.rb', line 164 def call perform end |
#completed? ⇒ Boolean
181 182 183 |
# File 'lib/active_operation/base.rb', line 181 def completed? halted? || succeeded? end |
#halted? ⇒ Boolean
173 174 175 |
# File 'lib/active_operation/base.rb', line 173 def halted? state == :halted end |
#perform ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/active_operation/base.rb', line 144 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
177 178 179 |
# File 'lib/active_operation/base.rb', line 177 def succeeded? state == :succeeded end |