Class: ComposableOperations::Operation
- Inherits:
-
Object
- Object
- ComposableOperations::Operation
- Includes:
- SmartProperties
- Defined in:
- lib/composable_operations/operation.rb
Direct Known Subclasses
Class Attribute Summary collapse
Instance Attribute Summary collapse
-
#backtrace ⇒ Object
readonly
Returns the value of attribute backtrace.
-
#exception ⇒ Object
readonly
Returns the value of attribute exception.
-
#input ⇒ Object
readonly
Returns the value of attribute input.
-
#message ⇒ Object
readonly
Returns the value of attribute message.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
Class Method Summary collapse
- .arity ⇒ Object
- .exception ⇒ Object
- .finalizers ⇒ Object
- .perform(*args) ⇒ Object
- .preparators ⇒ Object
Instance Method Summary collapse
- #failed? ⇒ Boolean
- #halted? ⇒ Boolean
-
#initialize(*args) ⇒ Operation
constructor
A new instance of Operation.
- #message? ⇒ Boolean
- #name ⇒ Object
- #perform ⇒ Object
- #succeeded? ⇒ Boolean
Constructor Details
#initialize(*args) ⇒ Operation
Returns a new instance of Operation.
93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/composable_operations/operation.rb', line 93 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 |
Class Attribute Details
.arguments ⇒ Object
9 10 11 |
# File 'lib/composable_operations/operation.rb', line 9 def arguments @arguments ||= [] end |
Instance Attribute Details
#backtrace ⇒ Object
Returns the value of attribute backtrace.
90 91 92 |
# File 'lib/composable_operations/operation.rb', line 90 def backtrace @backtrace end |
#exception ⇒ Object
Returns the value of attribute exception.
91 92 93 |
# File 'lib/composable_operations/operation.rb', line 91 def exception @exception end |
#input ⇒ Object (readonly)
Returns the value of attribute input.
87 88 89 |
# File 'lib/composable_operations/operation.rb', line 87 def input @input end |
#message ⇒ Object
Returns the value of attribute message.
89 90 91 |
# File 'lib/composable_operations/operation.rb', line 89 def @message end |
#result ⇒ Object
Returns the value of attribute result.
88 89 90 |
# File 'lib/composable_operations/operation.rb', line 88 def result @result end |
Class Method Details
.arity ⇒ Object
13 14 15 |
# File 'lib/composable_operations/operation.rb', line 13 def arity arguments.count end |
.exception ⇒ Object
48 49 50 |
# File 'lib/composable_operations/operation.rb', line 48 def exception OperationError end |
.finalizers ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/composable_operations/operation.rb', line 37 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
17 18 19 20 21 22 23 24 |
# File 'lib/composable_operations/operation.rb', line 17 def perform(*args) operation = new(*args) operation.perform raise operation.exception if operation.failed? operation.result end |
.preparators ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/composable_operations/operation.rb', line 26 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
111 112 113 |
# File 'lib/composable_operations/operation.rb', line 111 def failed? state == :failed end |
#halted? ⇒ Boolean
115 116 117 |
# File 'lib/composable_operations/operation.rb', line 115 def halted? state == :halted end |
#message? ⇒ Boolean
123 124 125 |
# File 'lib/composable_operations/operation.rb', line 123 def .present? end |
#name ⇒ Object
127 128 129 |
# File 'lib/composable_operations/operation.rb', line 127 def name self.class.name end |
#perform ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/composable_operations/operation.rb', line 131 def perform self.result = catch(:halt) do prepare result = execute self.state = :succeeded result end finalize self.result end |
#succeeded? ⇒ Boolean
119 120 121 |
# File 'lib/composable_operations/operation.rb', line 119 def succeeded? state == :succeeded end |