Class: Literal::Failure
Defined Under Namespace
Classes: Generic
Instance Attribute Summary collapse
-
#failure_type ⇒ Object
readonly
Returns the value of attribute failure_type.
-
#success_type ⇒ Object
readonly
Returns the value of attribute success_type.
Instance Method Summary collapse
- #deconstruct ⇒ Object
- #deconstruct_keys(keys) ⇒ Object
- #error! ⇒ Object
- #failure? ⇒ Boolean
-
#initialize(error, success_type:, failure_type:) ⇒ Failure
constructor
A new instance of Failure.
- #map(type) ⇒ Object
- #success? ⇒ Boolean
- #then ⇒ Object
- #value! ⇒ Object
- #value_or {|@error| ... } ⇒ Object
Methods inherited from Result
Constructor Details
#initialize(error, success_type:, failure_type:) ⇒ Failure
Returns a new instance of Failure.
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/literal/failure.rb', line 25 def initialize(error, success_type:, failure_type:) @error = error @success_type = success_type @failure_type = failure_type Literal.check(error, failure_type) freeze end |
Instance Attribute Details
#failure_type ⇒ Object (readonly)
Returns the value of attribute failure_type.
36 37 38 |
# File 'lib/literal/failure.rb', line 36 def failure_type @failure_type end |
#success_type ⇒ Object (readonly)
Returns the value of attribute success_type.
36 37 38 |
# File 'lib/literal/failure.rb', line 36 def success_type @success_type end |
Instance Method Details
#deconstruct ⇒ Object
50 51 52 |
# File 'lib/literal/failure.rb', line 50 def deconstruct [@error] end |
#deconstruct_keys(keys) ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/literal/failure.rb', line 54 def deconstruct_keys(keys) if @error.respond_to?(:deconstruct_keys) @error.deconstruct_keys(keys) else {} end end |
#error! ⇒ Object
62 63 64 |
# File 'lib/literal/failure.rb', line 62 def error! @error end |
#failure? ⇒ Boolean
42 43 44 |
# File 'lib/literal/failure.rb', line 42 def failure? true end |
#map(type) ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/literal/failure.rb', line 66 def map(type) raise ArgumentError unless block_given? Literal::Failure.new( @error, success_type: type, failure_type: @failure_type ) end |
#success? ⇒ Boolean
38 39 40 |
# File 'lib/literal/failure.rb', line 38 def success? false end |
#then ⇒ Object
76 77 78 79 |
# File 'lib/literal/failure.rb', line 76 def then raise ArgumentError unless block_given? self end |
#value! ⇒ Object
46 47 48 |
# File 'lib/literal/failure.rb', line 46 def value! raise Literal::ArgumentError.new("Failure has no value") end |
#value_or {|@error| ... } ⇒ Object
81 82 83 84 |
# File 'lib/literal/failure.rb', line 81 def value_or raise ArgumentError unless block_given? yield(@error) end |