Class: TypedOperation::Result::Failure

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

Overview

Represents a failed result. Immutable value object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(error) ⇒ Failure

: (untyped) -> void



16
17
18
19
# File 'lib/typed_operation/result/failure.rb', line 16

def initialize(error)
  @error = error
  freeze
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



13
14
15
# File 'lib/typed_operation/result/failure.rb', line 13

def error
  @error
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

: (untyped) -> bool



61
62
63
# File 'lib/typed_operation/result/failure.rb', line 61

def ==(other)
  other.is_a?(Failure) && other.error == @error
end

#deconstructObject

Pattern matching support - array destructuring. : () -> Array



45
46
47
# File 'lib/typed_operation/result/failure.rb', line 45

def deconstruct
  @error.is_a?(::Array) ? @error : [@error]
end

#deconstruct_keys(keys) ⇒ Object

Pattern matching support - hash destructuring. Delegates to the inner error if it responds to deconstruct_keys. : (Array?) -> Hash[Symbol, untyped]



52
53
54
55
56
57
58
# File 'lib/typed_operation/result/failure.rb', line 52

def deconstruct_keys(keys)
  if @error.respond_to?(:deconstruct_keys)
    @error.deconstruct_keys(keys)
  else
    {error: @error, failure: @error}
  end
end

#failureObject

Returns the wrapped error. : () -> untyped



39
40
41
# File 'lib/typed_operation/result/failure.rb', line 39

def failure
  @error
end

#failure?Boolean

: () -> true

Returns:

  • (Boolean)


27
28
29
# File 'lib/typed_operation/result/failure.rb', line 27

def failure?
  true
end

#hashObject

: () -> Integer



67
68
69
# File 'lib/typed_operation/result/failure.rb', line 67

def hash
  [self.class, @error].hash
end

#inspectObject Also known as: to_s

: () -> String



72
73
74
# File 'lib/typed_operation/result/failure.rb', line 72

def inspect
  "Failure(#{@error.inspect})"
end

#success?Boolean

: () -> false

Returns:

  • (Boolean)


22
23
24
# File 'lib/typed_operation/result/failure.rb', line 22

def success?
  false
end

#value!Object

Raises UnwrapError since this is a Failure. : () -> bot

Raises:



33
34
35
# File 'lib/typed_operation/result/failure.rb', line 33

def value!
  raise UnwrapError, "Cannot unwrap Failure: #{@error.inspect}"
end