Class: Fluxo::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/fluxo/result.rb

Constant Summary collapse

ATTRIBUTES =
%i[operation type value ids transient_attributes].freeze

Instance Method Summary collapse

Constructor Details

#initialize(operation:, type:, value:, ids: nil) ⇒ Result

Returns a new instance of Result.

Parameters:

  • options (Hash)


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.

Returns:

  • (Boolean)

    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.

Returns:

  • (Boolean)

    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.

Returns:

  • (Boolean)

    true if the result is a success



34
35
36
# File 'lib/fluxo/result.rb', line 34

def success?
  type == :ok
end