Class: Fluxo::Operation
Defined Under Namespace
Modules: Attributes, Constructor
Class Method Summary
collapse
Instance Method Summary
collapse
included
included
Methods included from Attributes
included
Class Method Details
.call(**attrs) ⇒ Object
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/fluxo/operation.rb', line 18
def call(**attrs)
instance = new
begin
instance.__execute_flow__(steps: [:call!], attributes: attrs)
rescue InvalidResultError, AttributeError, ValidationDefinitionError => e
raise e
rescue => e
Fluxo::Result.new(type: :exception, value: e, operation: instance, ids: %i[error]).tap do |result|
Fluxo.config.error_handlers.each { |handler| handler.call(result) }
end
end
end
|
.flow(*methods) ⇒ Object
14
15
16
|
# File 'lib/fluxo/operation.rb', line 14
def flow(*methods)
define_method(:call!) { |**attrs| __execute_flow__(steps: methods, attributes: attrs) }
end
|
Instance Method Details
#__execute_flow__(steps: [], attributes: {}) ⇒ Object
Calls step-method by step-method always passing the value to the next step If one of the methods is a failure stop the execution and return a result.
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/fluxo/operation.rb', line 42
def __execute_flow__(steps: [], attributes: {})
transient_attributes = attributes.dup
__validate_attributes__(first_step: steps.first, attributes: transient_attributes)
result = nil
steps.unshift(:__validate__) if self.class.validations_proxy steps.each_with_index do |step, idx|
result = __wrap_result__(send(step, **transient_attributes))
break unless result.success?
if steps[idx + 1]
transient_attributes = __merge_result_attributes__(
new_attributes: result.value,
old_attributes: transient_attributes,
next_step: steps[idx + 1]
)
end
end
result
end
|
#call! ⇒ Object
33
34
35
36
37
38
|
# File 'lib/fluxo/operation.rb', line 33
def call!(**)
raise NotImplementedError, <<~ERROR
You must implement the #call! method in your operation.
For complexes operations, you can use the #flow method instead.
ERROR
end
|
#Failure(value_or_result_id = nil) ⇒ Object
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/fluxo/operation.rb', line 76
def Failure(value_or_result_id = nil)
attrs = {type: :failure, operation: self}
if block_given?
attrs[:ids] = value_or_result_id
attrs[:value] = yield
else
attrs[:value] = value_or_result_id
end
Fluxo::Result.new(**attrs)
end
|
#Success(value_or_result_id = nil) ⇒ Object
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/fluxo/operation.rb', line 64
def Success(value_or_result_id = nil)
attrs = {type: :ok, operation: self}
if block_given?
attrs[:ids] = value_or_result_id
attrs[:value] = yield
else
attrs[:value] = value_or_result_id
end
Fluxo::Result.new(**attrs)
end
|
#Void ⇒ Object
87
88
89
|
# File 'lib/fluxo/operation.rb', line 87
def Void
Success(:void) { nil }
end
|