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.



84
85
86
87
88
89
90
91
92
# File 'lib/composable_operations/operation.rb', line 84

def initialize(*args)
  super(args.last.kind_of?(Hash) ? args.pop : {})
  @input = case args.length
           when 0 then nil
           when 1 then args.first
           else
             args
           end
end

Instance Attribute Details

#backtraceObject

Returns the value of attribute backtrace.



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

def backtrace
  @backtrace
end

#inputObject (readonly)

Returns the value of attribute input.



79
80
81
# File 'lib/composable_operations/operation.rb', line 79

def input
  @input
end

#messageObject

Returns the value of attribute message.



81
82
83
# File 'lib/composable_operations/operation.rb', line 81

def message
  @message
end

#resultObject

Returns the value of attribute result.



80
81
82
# File 'lib/composable_operations/operation.rb', line 80

def result
  @result
end

Class Method Details

.exceptionObject



39
40
41
# File 'lib/composable_operations/operation.rb', line 39

def exception
  @exception or defined?(super) ? super : OperationError
end

.finalizersObject



28
29
30
31
32
33
34
35
36
37
# File 'lib/composable_operations/operation.rb', line 28

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

Raises:



8
9
10
11
12
13
14
15
# File 'lib/composable_operations/operation.rb', line 8

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

  raise exception, operation.message, operation.backtrace if operation.failed?

  operation.result
end

.preparatorsObject



17
18
19
20
21
22
23
24
25
26
# File 'lib/composable_operations/operation.rb', line 17

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)


94
95
96
# File 'lib/composable_operations/operation.rb', line 94

def failed?
  state == :failed
end

#halted?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/composable_operations/operation.rb', line 98

def halted?
  state == :halted
end

#message?Boolean

Returns:

  • (Boolean)


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

def message?
  message.present?
end

#nameObject



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

def name
  self.class.name
end

#performObject



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/composable_operations/operation.rb', line 114

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

  finalize

  self.result
end

#succeeded?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/composable_operations/operation.rb', line 102

def succeeded?
  state == :succeeded
end