Class: Fluxo::Result
- Inherits:
-
Object
- Object
- Fluxo::Result
- Defined in:
- lib/fluxo/result.rb
Constant Summary collapse
- ATTRIBUTES =
%i[operation type value ids transient_attributes].freeze
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.
13 14 15 16 17 18 19 |
# File 'lib/fluxo/result.rb', line 13 def initialize(operation:, type:, value:, ids: nil) @operation = operation @value = value @type = type @ids = Array(ids) @transient_attributes = {} end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
28 29 30 |
# File 'lib/fluxo/result.rb', line 28 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.
44 45 46 |
# File 'lib/fluxo/result.rb', line 44 def error? type == :exception end |
#failure? ⇒ Boolean
Returns true if the result is a failure.
39 40 41 |
# File 'lib/fluxo/result.rb', line 39 def failure? type == :failure end |
#mutate(**attrs) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/fluxo/result.rb', line 21 def mutate(**attrs) attrs.each do |key, value| instance_variable_set("@#{key}", value) if ATTRIBUTES.include?(key) end self end |
#on_error(*handler_ids) ⇒ Object
56 57 58 |
# File 'lib/fluxo/result.rb', line 56 def on_error(*handler_ids) tap { yield(self) if error? && (handler_ids.none? || (ids & handler_ids).any?) } end |
#on_failure(*handler_ids) ⇒ Object
52 53 54 |
# File 'lib/fluxo/result.rb', line 52 def on_failure(*handler_ids) tap { yield(self) if failure? && (handler_ids.none? || (ids & handler_ids).any?) } end |
#on_success(*handler_ids) ⇒ Object
48 49 50 |
# File 'lib/fluxo/result.rb', line 48 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.
34 35 36 |
# File 'lib/fluxo/result.rb', line 34 def success? type == :ok end |