Class: ActiveOperation::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Callbacks, SmartProperties
Defined in:
lib/active_operation/base.rb

Direct Known Subclasses

Pipeline

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Base

Returns a new instance of Base.

Raises:

  • (ArgumentError)


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

#errorObject

Returns the value of attribute error.



8
9
10
# File 'lib/active_operation/base.rb', line 8

def error
  @error
end

#outputObject

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

.inputsObject



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_procObject



31
32
33
34
35
# File 'lib/active_operation/base.rb', line 31

def to_proc
  ->(*args) {
    perform(*args)
  }
end

Instance Method Details

#callObject



133
134
135
# File 'lib/active_operation/base.rb', line 133

def call
  perform
end

#completed?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/active_operation/base.rb', line 150

def completed?
  halted? || succeeded?
end

#halted?Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/active_operation/base.rb', line 142

def halted?
  state == :halted
end

#performObject



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

Returns:

  • (Boolean)


146
147
148
# File 'lib/active_operation/base.rb', line 146

def succeeded?
  state == :succeeded
end