Class: Dry::Monads::Result::Failure
- Inherits:
-
Dry::Monads::Result
- Object
- Dry::Monads::Result
- Dry::Monads::Result::Failure
- Includes:
- Dry::Monads::RightBiased::Left
- Defined in:
- lib/dry/monads/result.rb,
lib/dry/monads/maybe.rb,
lib/dry/monads/validated.rb
Overview
Represents a value of a failed operation.
Instance Attribute Summary collapse
-
#trace ⇒ String
readonly
Line where the value was constructed.
Attributes inherited from Dry::Monads::Result
Class Method Summary collapse
-
.[](*value) ⇒ Object
Shortcut for Failure().
-
.to_proc ⇒ Proc
Returns a constructor proc.
Instance Method Summary collapse
- #===(other) ⇒ Boolean
-
#alt_map(proc = Undefined, &block) ⇒ Object
Lifts a block/proc over Failure.
-
#either(_, g) ⇒ Any
Returns result of applying second function to the internal value.
- #failure ⇒ Object
-
#failure? ⇒ Boolean
Returns true.
-
#flip ⇒ Result::Success
Transform to a Success instance.
-
#initialize(value, trace = RightBiased::Left.trace_caller) ⇒ Failure
constructor
A new instance of Failure.
-
#or(*args) ⇒ Object
If a block is given passes internal value to it and returns the result, otherwise simply returns the first argument.
-
#or_fmap ⇒ Result::Success
A lifted version of ‘#or`.
-
#result(f, _) ⇒ Object
Apply the first function to value.
-
#success? ⇒ Boolean
Returns false.
- #to_maybe ⇒ Maybe::None
- #to_s ⇒ String (also: #inspect)
-
#to_validated ⇒ Validated::Invalid
Transforms to Validated.
- #value_or(val = nil) ⇒ Object
Methods included from Dry::Monads::RightBiased::Left
#and, #apply, #bind, #deconstruct, #deconstruct_keys, #discard, #flatten, #fmap, #tee, trace_caller, #value!, #|
Methods inherited from Dry::Monads::Result
#monad, pure, #to_monad, #to_result
Methods included from Transformer
Constructor Details
#initialize(value, trace = RightBiased::Left.trace_caller) ⇒ Failure
Returns a new instance of Failure.
170 171 172 173 174 |
# File 'lib/dry/monads/result.rb', line 170 def initialize(value, trace = RightBiased::Left.trace_caller) super() @value = value @trace = trace end |
Instance Attribute Details
#trace ⇒ String (readonly)
Line where the value was constructed
166 167 168 |
# File 'lib/dry/monads/result.rb', line 166 def trace @trace end |
Class Method Details
.[](*value) ⇒ Object
151 152 153 |
# File 'lib/dry/monads/result.rb', line 151 def self.[](*value) new(value, RightBiased::Left.trace_caller) end |
.to_proc ⇒ Proc
Returns a constructor proc
158 159 160 |
# File 'lib/dry/monads/result.rb', line 158 def self.to_proc @to_proc ||= method(:new).to_proc end |
Instance Method Details
#===(other) ⇒ Boolean
246 247 248 |
# File 'lib/dry/monads/result.rb', line 246 def ===(other) Failure === other && failure === other.failure end |
#alt_map(proc) ⇒ Result::Failure #alt_map ⇒ Result::Failure
Lifts a block/proc over Failure
270 271 272 273 |
# File 'lib/dry/monads/result.rb', line 270 def alt_map(proc = Undefined, &block) f = Undefined.default(proc, block) self.class.new(f.(failure), RightBiased::Left.trace_caller) end |
#either(_, g) ⇒ Any
Returns result of applying second function to the internal value.
258 |
# File 'lib/dry/monads/result.rb', line 258 def either(_, g) = g.(failure) |
#failure ⇒ Object
177 |
# File 'lib/dry/monads/result.rb', line 177 def failure = @value |
#failure? ⇒ Boolean
Returns true
185 |
# File 'lib/dry/monads/result.rb', line 185 def failure? = true |
#flip ⇒ Result::Success
Transform to a Success instance
233 |
# File 'lib/dry/monads/result.rb', line 233 def flip = Success.new(@value) |
#or(*args) ⇒ Object
If a block is given passes internal value to it and returns the result, otherwise simply returns the first argument.
201 202 203 204 205 206 207 |
# File 'lib/dry/monads/result.rb', line 201 def or(*args) if block_given? yield(@value, *args) else args[0] end end |
#or_fmap ⇒ Result::Success
A lifted version of ‘#or`. Wraps the passed value or the block result with Result::Success.
218 |
# File 'lib/dry/monads/result.rb', line 218 def or_fmap(...) = Success.new(self.or(...)) |
#result(f, _) ⇒ Object
Apply the first function to value.
182 |
# File 'lib/dry/monads/result.rb', line 182 def result(f, _) = f.(@value) |
#success? ⇒ Boolean
Returns false
188 |
# File 'lib/dry/monads/result.rb', line 188 def success? = false |
#to_maybe ⇒ Maybe::None
367 |
# File 'lib/dry/monads/maybe.rb', line 367 def to_maybe = Maybe::None.new(trace) |
#to_s ⇒ String Also known as: inspect
221 222 223 224 225 226 227 |
# File 'lib/dry/monads/result.rb', line 221 def to_s if Unit.equal?(@value) "Failure()" else "Failure(#{@value.inspect})" end end |
#to_validated ⇒ Validated::Invalid
Transforms to Validated
287 |
# File 'lib/dry/monads/validated.rb', line 287 def to_validated = Validated::Invalid.new(failure, trace) |
#value_or(val = nil) ⇒ Object
236 237 238 239 240 241 242 |
# File 'lib/dry/monads/result.rb', line 236 def value_or(val = nil) if block_given? yield(@value) else val end end |