Class: ComposableOperations::Operation

Inherits:
Object
  • Object
show all
Includes:
SmartProperties
Defined in:
lib/composable_operations/operation.rb

Direct Known Subclasses

ComposedOperation

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Operation

Returns a new instance of Operation.

Raises:

  • (ArgumentError)


89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/composable_operations/operation.rb', line 89

def initialize(*args)
  arity = self.class.arity
  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.arguments.each_with_index do |name, index|
    attributes[name] = arguments[index]
  end

  super(attributes)
end

Instance Attribute Details

#backtraceObject

Returns the value of attribute backtrace.



86
87
88
# File 'lib/composable_operations/operation.rb', line 86

def backtrace
  @backtrace
end

#exceptionObject

Returns the value of attribute exception.



87
88
89
# File 'lib/composable_operations/operation.rb', line 87

def exception
  @exception
end

#inputObject (readonly)

Returns the value of attribute input.



83
84
85
# File 'lib/composable_operations/operation.rb', line 83

def input
  @input
end

#messageObject

Returns the value of attribute message.



85
86
87
# File 'lib/composable_operations/operation.rb', line 85

def message
  @message
end

#resultObject

Returns the value of attribute result.



84
85
86
# File 'lib/composable_operations/operation.rb', line 84

def result
  @result
end

Class Method Details

.argumentsObject



7
8
9
# File 'lib/composable_operations/operation.rb', line 7

def arguments
  []
end

.arityObject



11
12
13
# File 'lib/composable_operations/operation.rb', line 11

def arity
  arguments.count
end

.exceptionObject



46
47
48
# File 'lib/composable_operations/operation.rb', line 46

def exception
  OperationError
end

.finalizersObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/composable_operations/operation.rb', line 35

def finalizers
  finalizers = []
  klass = self
  while klass != Operation
    klass = klass.superclass
    finalizers += Array(klass.instance_variable_get(:@finalizers))
  end
  finalizers += Array(@finalizers)
  finalizers
end

.perform(*args) ⇒ Object



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

def perform(*args)
  operation = new(*args)
  operation.perform

  raise operation.exception if operation.failed?

  operation.result
end

.preparatorsObject



24
25
26
27
28
29
30
31
32
33
# File 'lib/composable_operations/operation.rb', line 24

def preparators
  preparators = []
  klass = self
  while klass != Operation
    klass = klass.superclass
    preparators += Array(klass.instance_variable_get(:@preparators))
  end
  preparators += Array(@preparators)
  preparators
end

Instance Method Details

#failed?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/composable_operations/operation.rb', line 107

def failed?
  state == :failed
end

#halted?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/composable_operations/operation.rb', line 111

def halted?
  state == :halted
end

#message?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/composable_operations/operation.rb', line 119

def message?
  message.present?
end

#nameObject



123
124
125
# File 'lib/composable_operations/operation.rb', line 123

def name
  self.class.name
end

#performObject



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/composable_operations/operation.rb', line 127

def perform
  self.result = catch(:halt) do
    prepare
    result = execute
    self.state = :succeeded
    result
  end

  finalize

  self.result
end

#succeeded?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/composable_operations/operation.rb', line 115

def succeeded?
  state == :succeeded
end