Class: Fluxo::Result
- Inherits:
-
Object
- Object
- Fluxo::Result
- Defined in:
- lib/fluxo/result.rb
Instance Attribute Summary collapse
-
#ids ⇒ Object
readonly
Returns the value of attribute ids.
-
#operation ⇒ Object
readonly
Returns the value of attribute operation.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
-
#error? ⇒ Boolean
True if the result is an exception.
-
#failure? ⇒ Boolean
True if the result is a failure.
-
#initialize(operation:, type:, value:, ids: nil) ⇒ Result
constructor
A new instance of Result.
- #mutate(**attrs) ⇒ Object
- #on_error(*handler_ids) ⇒ Object
- #on_failure(*handler_ids) ⇒ Object
- #on_success(*handler_ids) ⇒ Object
-
#success? ⇒ Boolean
True if the result is a success.
Constructor Details
#initialize(operation:, type:, value:, ids: nil) ⇒ Result
Returns a new instance of Result.
12 13 14 15 16 17 |
# File 'lib/fluxo/result.rb', line 12 def initialize(operation:, type:, value:, ids: nil) @operation = operation @value = value @type = type @ids = Array(ids) end |
Instance Attribute Details
#ids ⇒ Object (readonly)
Returns the value of attribute ids.
5 6 7 |
# File 'lib/fluxo/result.rb', line 5 def ids @ids end |
#operation ⇒ Object (readonly)
Returns the value of attribute operation.
5 6 7 |
# File 'lib/fluxo/result.rb', line 5 def operation @operation end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
5 6 7 |
# File 'lib/fluxo/result.rb', line 5 def type @type end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
5 6 7 |
# File 'lib/fluxo/result.rb', line 5 def value @value end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
23 24 25 |
# File 'lib/fluxo/result.rb', line 23 def ==(other) other.is_a?(self.class) && other.operation == operation && other.type == type && other.value == value && other.ids == ids end |
#error? ⇒ Boolean
Returns true if the result is an exception.
39 40 41 |
# File 'lib/fluxo/result.rb', line 39 def error? type == :exception end |
#failure? ⇒ Boolean
Returns true if the result is a failure.
34 35 36 |
# File 'lib/fluxo/result.rb', line 34 def failure? type == :failure end |
#mutate(**attrs) ⇒ Object
19 20 21 |
# File 'lib/fluxo/result.rb', line 19 def mutate(**attrs) self.class.new(**{operation: operation, type: type, value: value, ids: ids}.merge(attrs)) end |
#on_error(*handler_ids) ⇒ Object
51 52 53 |
# File 'lib/fluxo/result.rb', line 51 def on_error(*handler_ids) tap { yield(self) if error? && (handler_ids.none? || (ids & handler_ids).any?) } end |
#on_failure(*handler_ids) ⇒ Object
47 48 49 |
# File 'lib/fluxo/result.rb', line 47 def on_failure(*handler_ids) tap { yield(self) if failure? && (handler_ids.none? || (ids & handler_ids).any?) } end |
#on_success(*handler_ids) ⇒ Object
43 44 45 |
# File 'lib/fluxo/result.rb', line 43 def on_success(*handler_ids) tap { yield(self) if success? && (handler_ids.none? || (ids & handler_ids).any?) } end |
#success? ⇒ Boolean
Returns true if the result is a success.
29 30 31 |
# File 'lib/fluxo/result.rb', line 29 def success? type == :ok end |