class Dry::Monads::Result::Success::Failure
Represents a value of a failed operation.
@api public
Constants
- Failure
-
@see Result::Failure
- Success
-
@see
Result::Success
Attributes
Line where the value was constructed
@return [String] @api public
Public Class Methods
Source
# File lib/dry/monads/result.rb, line 151 def self.[](*value) new(value, RightBiased::Left.trace_caller) end
Shortcut for Failure()
@example include Dry::Monads[:result] def call Failure[:error, :not_found] # => Failure([:error, :not_found]) end
@api public
Source
# File lib/dry/monads/result.rb, line 363 def self.Result(error, **options) Result::Fixed[error, **options] end
Creates a module that has two methods: ‘Success` and `Failure`. `Success` is identical to {Result::Mixin::Constructors#Success} and Failure
rejects values that don’t conform the value of the ‘error` parameter. This is essentially a Result
type with the `Failure` part fixed.
@example using dry-types
module Types include Dry::Types.module end class Operation # :user_not_found and :account_not_found are the only # values allowed as failure results Error = Types.Value(:user_not_found) | Types.Value(:account_not_found) include Dry::Monads::Result(Error) def find_account(id) account = acount_repo.find(id) account ? Success(account) : Failure(:account_not_found) end def find_user(id) # ... end end
@param error [#===] the type of allowed failures @return [Module]
Source
# File lib/dry/monads/result.rb, line 170 def initialize(value, trace = RightBiased::Left.trace_caller) super() @value = value @trace = trace end
@param value [Object] failure value @param trace [String] caller line
Source
# File lib/dry/monads/result.rb, line 158 def self.to_proc @to_proc ||= method(:new).to_proc end
Returns a constructor proc
@return [Proc]
Public Instance Methods
Source
# File lib/dry/monads/result.rb, line 246 def ===(other) Failure === other && failure === other.failure end
@param other [Result] @return [Boolean]
Source
# 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
Lifts a block/proc over Failure
@overload alt_map
(proc)
@param proc [#call] @return [Result::Failure]
@overload alt_map
@param block [Proc] @return [Result::Failure]
Source
# File lib/dry/monads/result.rb, line 258 def either(_, g) = g.(failure) # Lifts a block/proc over Failure # # @overload alt_map(proc) # @param proc [#call] # @return [Result::Failure] # # @overload alt_map # @param block [Proc] # @return [Result::Failure] # def alt_map(proc = Undefined, &block) f = Undefined.default(proc, block) self.class.new(f.(failure), RightBiased::Left.trace_caller) end end
Returns result of applying second function to the internal value.
@example
Dry::Monads.Failure(1).either(-> x { x + 1 }, -> x { x + 2 }) # => 3
@param _ [#call] Ignored @param g [#call] Function to call @return [Any] Return value of ‘g`
Source
# File lib/dry/monads/result.rb, line 177 def failure = @value # Apply the first function to value. # # @api public def result(f, _) = f.(@value) # Returns true def failure? = true # Returns false def success? = false # If a block is given passes internal value to it and returns the result, # otherwise simply returns the first argument. # # @example # Dry::Monads.Failure(ArgumentError.new('error message')).or(&:message) # # => "error message" # # @param args [Array<Object>] arguments that will be passed to a block # if one was given, otherwise the first # value will be returned # @return [Object] def or(*args) if block_given? yield(@value, *args) else args[0] end end # A lifted version of `#or`. Wraps the passed value or the block # result with Result::Success. # # @example # Dry::Monads.Failure.new('no value').or_fmap('value') # => Success("value") # Dry::Monads.Failure.new('no value').or_fmap { 'value' } # => Success("value") # # @param args [Array<Object>] arguments will be passed to the underlying `#or` call # @return [Result::Success] Wrapped value def or_fmap(...) = Success.new(self.or(...)) # @return [String] def to_s if Unit.equal?(@value) "Failure()" else "Failure(#{@value.inspect})" end end alias_method :inspect, :to_s # Transform to a Success instance # # @return [Result::Success] def flip = Success.new(@value) # @see RightBiased::Left#value_or def value_or(val = nil) if block_given? yield(@value) else val end end # @param other [Result] # @return [Boolean] def ===(other) Failure === other && failure === other.failure end # Returns result of applying second function to the internal value. # # @example # Dry::Monads.Failure(1).either(-> x { x + 1 }, -> x { x + 2 }) # => 3 # # @param _ [#call] Ignored # @param g [#call] Function to call # @return [Any] Return value of `g` def either(_, g) = g.(failure) # Lifts a block/proc over Failure # # @overload alt_map(proc) # @param proc [#call] # @return [Result::Failure] # # @overload alt_map # @param block [Proc] # @return [Result::Failure] # def alt_map(proc = Undefined, &block) f = Undefined.default(proc, block) self.class.new(f.(failure), RightBiased::Left.trace_caller) end end # A module that can be included for easier access to Result monads. # # @api public module Mixin # @see Result::Success Success = Result::Success # @see Result::Failure Failure = Result::Failure # Value constructors # module Constructors # Success constructor # # @overload Success(value) # @param value [Object] # @return [Result::Success] # # @overload Success(&block) # @param block [Proc] a block to be wrapped with Success # @return [Result::Success] # def Success(value = Undefined, &block) v = Undefined.default(value, block || Unit) Success.new(v) end # Failure constructor # # @overload Success(value) # @param value [Object] # @return [Result::Failure] # # @overload Success(&block) # @param block [Proc] a block to be wrapped with Failure # @return [Result::Failure] # def Failure(value = Undefined, &block) v = Undefined.default(value, block || Unit) Failure.new(v, RightBiased::Left.trace_caller) end end include Constructors end end extend Result::Mixin::Constructors # @see Result::Success Success = Result::Success # @see Result::Failure Failure = Result::Failure # Creates a module that has two methods: `Success` and `Failure`. # `Success` is identical to {Result::Mixin::Constructors#Success} and Failure # rejects values that don't conform the value of the `error` # parameter. This is essentially a Result type with the `Failure` part # fixed. # # @example using dry-types # module Types # include Dry::Types.module # end # # class Operation # # :user_not_found and :account_not_found are the only # # values allowed as failure results # Error = # Types.Value(:user_not_found) | # Types.Value(:account_not_found) # # include Dry::Monads::Result(Error) # # def find_account(id) # account = acount_repo.find(id) # # account ? Success(account) : Failure(:account_not_found) # end # # def find_user(id) # # ... # end # end # # @param error [#===] the type of allowed failures # @return [Module] def self.Result(error, **options) Result::Fixed[error, **options] end class Maybe class Some < Maybe # Converts to Sucess(value!) # # @param fail [#call] Fallback value # @param block [Proc] Fallback block # @return [Success<Any>] def to_result(_fail = Unit, &) = Result::Success.new(@value) end class None < Maybe # Converts to Failure(fallback_value) # # @param fail [#call] Fallback value # @param block [Proc] Fallback block # @return [Failure<Any>] def to_result(fail = Unit) if block_given? Result::Failure.new(yield) else Result::Failure.new(fail) end end end end class Task # Converts to Result. Blocks the current thread if required. # # @return [Result] def to_result if promise.wait.fulfilled? Result::Success.new(promise.value) else Result::Failure.new(promise.reason, RightBiased::Left.trace_caller) end end end class Try class Value < Try # @return [Result::Success] def to_result = ::Dry::Monads::Result::Success.new(@value) end class Error < Try # @return [Result::Failure] def to_result Result::Failure.new(exception, RightBiased::Left.trace_caller) end end end class Validated class Valid < Validated # Converts to Result::Success # # @return [Result::Success] def to_result = Result.pure(value!) end class Invalid < Validated # Converts to Result::Failure # # @return [Result::Failure] def to_result Result::Failure.new(error, RightBiased::Left.trace_caller) end end end require "dry/monads/registry" register_mixin(
@private
Source
# File lib/dry/monads/result.rb, line 185 def failure? = true # Returns false def success? = false # If a block is given passes internal value to it and returns the result, # otherwise simply returns the first argument. # # @example # Dry::Monads.Failure(ArgumentError.new('error message')).or(&:message) # # => "error message" # # @param args [Array<Object>] arguments that will be passed to a block # if one was given, otherwise the first # value will be returned # @return [Object] def or(*args) if block_given? yield(@value, *args) else args[0] end end # A lifted version of `#or`. Wraps the passed value or the block # result with Result::Success. # # @example # Dry::Monads.Failure.new('no value').or_fmap('value') # => Success("value") # Dry::Monads.Failure.new('no value').or_fmap { 'value' } # => Success("value") # # @param args [Array<Object>] arguments will be passed to the underlying `#or` call # @return [Result::Success] Wrapped value def or_fmap(...) = Success.new(self.or(...)) # @return [String] def to_s if Unit.equal?(@value) "Failure()" else "Failure(#{@value.inspect})" end end alias_method :inspect, :to_s # Transform to a Success instance # # @return [Result::Success] def flip = Success.new(@value) # @see RightBiased::Left#value_or def value_or(val = nil) if block_given? yield(@value) else val end end # @param other [Result] # @return [Boolean] def ===(other) Failure === other && failure === other.failure end # Returns result of applying second function to the internal value. # # @example # Dry::Monads.Failure(1).either(-> x { x + 1 }, -> x { x + 2 }) # => 3 # # @param _ [#call] Ignored # @param g [#call] Function to call # @return [Any] Return value of `g` def either(_, g) = g.(failure) # Lifts a block/proc over Failure # # @overload alt_map(proc) # @param proc [#call] # @return [Result::Failure] # # @overload alt_map # @param block [Proc] # @return [Result::Failure] # def alt_map(proc = Undefined, &block) f = Undefined.default(proc, block) self.class.new(f.(failure), RightBiased::Left.trace_caller) end end # A module that can be included for easier access to Result monads. # # @api public module Mixin # @see Result::Success Success = Result::Success # @see Result::Failure Failure = Result::Failure # Value constructors # module Constructors # Success constructor # # @overload Success(value) # @param value [Object] # @return [Result::Success] # # @overload Success(&block) # @param block [Proc] a block to be wrapped with Success # @return [Result::Success] # def Success(value = Undefined, &block) v = Undefined.default(value, block || Unit) Success.new(v) end # Failure constructor # # @overload Success(value) # @param value [Object] # @return [Result::Failure] # # @overload Success(&block) # @param block [Proc] a block to be wrapped with Failure # @return [Result::Failure] # def Failure(value = Undefined, &block) v = Undefined.default(value, block || Unit) Failure.new(v, RightBiased::Left.trace_caller) end end include Constructors end end extend Result::Mixin::Constructors # @see Result::Success Success = Result::Success # @see Result::Failure Failure = Result::Failure # Creates a module that has two methods: `Success` and `Failure`. # `Success` is identical to {Result::Mixin::Constructors#Success} and Failure # rejects values that don't conform the value of the `error` # parameter. This is essentially a Result type with the `Failure` part # fixed. # # @example using dry-types # module Types # include Dry::Types.module # end # # class Operation # # :user_not_found and :account_not_found are the only # # values allowed as failure results # Error = # Types.Value(:user_not_found) | # Types.Value(:account_not_found) # # include Dry::Monads::Result(Error) # # def find_account(id) # account = acount_repo.find(id) # # account ? Success(account) : Failure(:account_not_found) # end # # def find_user(id) # # ... # end # end # # @param error [#===] the type of allowed failures # @return [Module] def self.Result(error, **options) Result::Fixed[error, **options] end class Maybe class Some < Maybe # Converts to Sucess(value!) # # @param fail [#call] Fallback value # @param block [Proc] Fallback block # @return [Success<Any>] def to_result(_fail = Unit, &) = Result::Success.new(@value) end class None < Maybe # Converts to Failure(fallback_value) # # @param fail [#call] Fallback value # @param block [Proc] Fallback block # @return [Failure<Any>] def to_result(fail = Unit) if block_given? Result::Failure.new(yield) else Result::Failure.new(fail) end end end end class Task # Converts to Result. Blocks the current thread if required. # # @return [Result] def to_result if promise.wait.fulfilled? Result::Success.new(promise.value) else Result::Failure.new(promise.reason, RightBiased::Left.trace_caller) end end end class Try class Value < Try # @return [Result::Success] def to_result = ::Dry::Monads::Result::Success.new(@value) end class Error < Try # @return [Result::Failure] def to_result Result::Failure.new(exception, RightBiased::Left.trace_caller) end end end class Validated class Valid < Validated # Converts to Result::Success # # @return [Result::Success] def to_result = Result.pure(value!) end class Invalid < Validated # Converts to Result::Failure # # @return [Result::Failure] def to_result Result::Failure.new(error, RightBiased::Left.trace_caller) end end end require "dry/monads/registry" register_mixin(:result, Result
Returns true
Source
# File lib/dry/monads/result.rb, line 233 def flip = Success.new(@value) # @see RightBiased::Left#value_or def value_or(val = nil) if block_given? yield(@value) else val end end # @param other [Result] # @return [Boolean] def ===(other) Failure === other && failure === other.failure end # Returns result of applying second function to the internal value. # # @example # Dry::Monads.Failure(1).either(-> x { x + 1 }, -> x { x + 2 }) # => 3 # # @param _ [#call] Ignored # @param g [#call] Function to call # @return [Any] Return value of `g` def either(_, g) = g.(failure) # Lifts a block/proc over Failure # # @overload alt_map(proc) # @param proc [#call] # @return [Result::Failure] # # @overload alt_map # @param block [Proc] # @return [Result::Failure] # def alt_map(proc = Undefined, &block) f = Undefined.default(proc, block) self.class.new(f.(failure), RightBiased::Left.trace_caller) end end # A module that can be included for easier access to Result monads. # # @api public module Mixin # @see Result::Success Success = Result::Success # @see Result::Failure Failure = Result::Failure # Value constructors # module Constructors # Success constructor # # @overload Success(value) # @param value [Object] # @return [Result::Success] # # @overload Success(&block) # @param block [Proc] a block to be wrapped with Success # @return [Result::Success] # def Success(value = Undefined, &block) v = Undefined.default(value, block || Unit) Success.new(v) end # Failure constructor # # @overload Success(value) # @param value [Object] # @return [Result::Failure] # # @overload Success(&block) # @param block [Proc] a block to be wrapped with Failure # @return [Result::Failure] # def Failure(value = Undefined, &block) v = Undefined.default(value, block || Unit) Failure.new(v, RightBiased::Left.trace_caller) end end include Constructors end end
Transform to a Success
instance
@return [Result::Success]
Source
# File lib/dry/monads/result.rb, line 201 def or(*args) if block_given? yield(@value, *args) else args[0] end end
If a block is given passes internal value to it and returns the result, otherwise simply returns the first argument.
@example
Dry::Monads.Failure(ArgumentError.new('error message')).or(&:message) # => "error message"
@param args [Array<Object>] arguments that will be passed to a block
if one was given, otherwise the first value will be returned
@return [Object]
Source
# File lib/dry/monads/result.rb, line 218 def or_fmap(...) = Success.new(self.or(...)) # @return [String] def to_s if Unit.equal?(@value) "Failure()" else "Failure(#{@value.inspect})" end end alias_method :inspect, :to_s # Transform to a Success instance # # @return [Result::Success] def flip = Success.new(@value) # @see RightBiased::Left#value_or def value_or(val = nil) if block_given? yield(@value) else val end end # @param other [Result] # @return [Boolean] def ===(other) Failure === other && failure === other.failure end # Returns result of applying second function to the internal value. # # @example # Dry::Monads.Failure(1).either(-> x { x + 1 }, -> x { x + 2 }) # => 3 # # @param _ [#call] Ignored # @param g [#call] Function to call # @return [Any] Return value of `g` def either(_, g) = g.(failure) # Lifts a block/proc over Failure # # @overload alt_map(proc) # @param proc [#call] # @return [Result::Failure] # # @overload alt_map # @param block [Proc] # @return [Result::Failure] # def alt_map(proc = Undefined, &block) f = Undefined.default(proc, block) self.class.new(f.(failure), RightBiased::Left.trace_caller) end end # A module that can be included for easier access to Result monads. # # @api public module Mixin # @see Result::Success Success = Result::Success # @see Result::Failure Failure = Result::Failure # Value constructors # module Constructors # Success constructor # # @overload Success(value) # @param value [Object] # @return [Result::Success] # # @overload Success(&block) # @param block [Proc] a block to be wrapped with Success # @return [Result::Success] # def Success(value = Undefined, &block) v = Undefined.default(value, block || Unit) Success.new(v) end # Failure constructor # # @overload Success(value) # @param value [Object] # @return [Result::Failure] # # @overload Success(&block) # @param block [Proc] a block to be wrapped with Failure # @return [Result::Failure] # def Failure(value = Undefined, &block) v = Undefined.default(value, block || Unit) Failure.new(v, RightBiased::Left.trace_caller) end end include Constructors end end extend Result::Mixin::Constructors # @see Result::Success Success = Result::Success # @see Result::Failure Failure = Result::Failure # Creates a module that has two methods: `Success` and `Failure`. # `Success` is identical to {Result::Mixin::Constructors#Success} and Failure # rejects values that don't conform the value of the `error` # parameter. This is essentially a Result type with the `Failure` part # fixed. # # @example using dry-types # module Types # include Dry::Types.module # end # # class Operation # # :user_not_found and :account_not_found are the only # # values allowed as failure results # Error = # Types.Value(:user_not_found) | # Types.Value(:account_not_found) # # include Dry::Monads::Result(Error) # # def find_account(id) # account = acount_repo.find(id) # # account ? Success(account) : Failure(:account_not_found) # end # # def find_user(id) # # ... # end # end # # @param error [#===] the type of allowed failures # @return [Module] def self.Result(error, **options) Result::Fixed[error, **options] end class Maybe class Some < Maybe # Converts to Sucess(value!) # # @param fail [#call] Fallback value # @param block [Proc] Fallback block # @return [Success<Any>] def to_result(_fail = Unit, &) = Result::Success.new(@value) end class None < Maybe # Converts to Failure(fallback_value) # # @param fail [#call] Fallback value # @param block [Proc] Fallback block # @return [Failure<Any>] def to_result(fail = Unit) if block_given? Result::Failure.new(yield) else Result::Failure.new(fail) end end end end class Task # Converts to Result. Blocks the current thread if required. # # @return [Result] def to_result if promise.wait.fulfilled? Result::Success.new(promise.value) else Result::Failure.new(promise.reason, RightBiased::Left.trace_caller) end end end class Try class Value < Try # @return [Result::Success] def to_result = ::Dry::Monads::Result::Success.new(@value) end class Error < Try # @return [Result::Failure] def to_result Result::Failure.new(exception, RightBiased::Left.trace_caller) end end end class Validated class Valid < Validated # Converts to Result::Success # # @return [Result::Success] def to_result = Result.pure(value!) end class Invalid < Validated # Converts to Result::Failure # # @return [Result::Failure] def to_result Result::Failure.new(error, RightBiased::Left.trace_caller) end end end require "dry/monads/registry" register_mixin(:result, Result::Mixin)
A lifted version of ‘#or`. Wraps the passed value or the block result with Result::Success
.
@example
Dry::Monads.Failure.new('no value').or_fmap('value') # => Success("value") Dry::Monads.Failure.new('no value').or_fmap { 'value' } # => Success("value")
@param args [Array<Object>] arguments will be passed to the underlying ‘#or` call @return [Result::Success] Wrapped value
Source
# File lib/dry/monads/result.rb, line 182 def result(f, _) = f.(@value) # Returns true def failure? = true # Returns false def success? = false # If a block is given passes internal value to it and returns the result, # otherwise simply returns the first argument. # # @example # Dry::Monads.Failure(ArgumentError.new('error message')).or(&:message) # # => "error message" # # @param args [Array<Object>] arguments that will be passed to a block # if one was given, otherwise the first # value will be returned # @return [Object] def or(*args) if block_given? yield(@value, *args) else args[0] end end # A lifted version of `#or`. Wraps the passed value or the block # result with Result::Success. # # @example # Dry::Monads.Failure.new('no value').or_fmap('value') # => Success("value") # Dry::Monads.Failure.new('no value').or_fmap { 'value' } # => Success("value") # # @param args [Array<Object>] arguments will be passed to the underlying `#or` call # @return [Result::Success] Wrapped value def or_fmap(...) = Success.new(self.or(...)) # @return [String] def to_s if Unit.equal?(@value) "Failure()" else "Failure(#{@value.inspect})" end end alias_method :inspect, :to_s # Transform to a Success instance # # @return [Result::Success] def flip = Success.new(@value) # @see RightBiased::Left#value_or def value_or(val = nil) if block_given? yield(@value) else val end end # @param other [Result] # @return [Boolean] def ===(other) Failure === other && failure === other.failure end # Returns result of applying second function to the internal value. # # @example # Dry::Monads.Failure(1).either(-> x { x + 1 }, -> x { x + 2 }) # => 3 # # @param _ [#call] Ignored # @param g [#call] Function to call # @return [Any] Return value of `g` def either(_, g) = g.(failure) # Lifts a block/proc over Failure # # @overload alt_map(proc) # @param proc [#call] # @return [Result::Failure] # # @overload alt_map # @param block [Proc] # @return [Result::Failure] # def alt_map(proc = Undefined, &block) f = Undefined.default(proc, block) self.class.new(f.(failure), RightBiased::Left.trace_caller) end end # A module that can be included for easier access to Result monads. # # @api public module Mixin # @see Result::Success Success = Result::Success # @see Result::Failure Failure = Result::Failure # Value constructors # module Constructors # Success constructor # # @overload Success(value) # @param value [Object] # @return [Result::Success] # # @overload Success(&block) # @param block [Proc] a block to be wrapped with Success # @return [Result::Success] # def Success(value = Undefined, &block) v = Undefined.default(value, block || Unit) Success.new(v) end # Failure constructor # # @overload Success(value) # @param value [Object] # @return [Result::Failure] # # @overload Success(&block) # @param block [Proc] a block to be wrapped with Failure # @return [Result::Failure] # def Failure(value = Undefined, &block) v = Undefined.default(value, block || Unit) Failure.new(v, RightBiased::Left.trace_caller) end end include Constructors end end extend Result::Mixin::Constructors # @see Result::Success Success = Result::Success # @see Result::Failure Failure = Result::Failure # Creates a module that has two methods: `Success` and `Failure`. # `Success` is identical to {Result::Mixin::Constructors#Success} and Failure # rejects values that don't conform the value of the `error` # parameter. This is essentially a Result type with the `Failure` part # fixed. # # @example using dry-types # module Types # include Dry::Types.module # end # # class Operation # # :user_not_found and :account_not_found are the only # # values allowed as failure results # Error = # Types.Value(:user_not_found) | # Types.Value(:account_not_found) # # include Dry::Monads::Result(Error) # # def find_account(id) # account = acount_repo.find(id) # # account ? Success(account) : Failure(:account_not_found) # end # # def find_user(id) # # ... # end # end # # @param error [#===] the type of allowed failures # @return [Module] def self.Result(error, **options) Result::Fixed[error, **options] end class Maybe class Some < Maybe # Converts to Sucess(value!) # # @param fail [#call] Fallback value # @param block [Proc] Fallback block # @return [Success<Any>] def to_result(_fail = Unit, &) = Result::Success.new(@value) end class None < Maybe # Converts to Failure(fallback_value) # # @param fail [#call] Fallback value # @param block [Proc] Fallback block # @return [Failure<Any>] def to_result(fail = Unit) if block_given? Result::Failure.new(yield) else Result::Failure.new(fail) end end end end class Task # Converts to Result. Blocks the current thread if required. # # @return [Result] def to_result if promise.wait.fulfilled? Result::Success.new(promise.value) else Result::Failure.new(promise.reason, RightBiased::Left.trace_caller) end end end class Try class Value < Try # @return [Result::Success] def to_result = ::Dry::Monads::Result::Success.new(@value) end class Error < Try # @return [Result::Failure] def to_result Result::Failure.new(exception, RightBiased::Left.trace_caller) end end end class Validated class Valid < Validated # Converts to Result::Success # # @return [Result::Success] def to_result = Result.pure(value!) end class Invalid < Validated # Converts to Result::Failure # # @return [Result::Failure] def to_result Result::Failure.new(error, RightBiased::Left.trace_caller) end end end require "dry/monads/registry" register_mixin(:result,
Apply the first function to value.
@api public
Source
# File lib/dry/monads/result.rb, line 188 def success? = false # If a block is given passes internal value to it and returns the result, # otherwise simply returns the first argument. # # @example # Dry::Monads.Failure(ArgumentError.new('error message')).or(&:message) # # => "error message" # # @param args [Array<Object>] arguments that will be passed to a block # if one was given, otherwise the first # value will be returned # @return [Object] def or(*args) if block_given? yield(@value, *args) else args[0] end end # A lifted version of `#or`. Wraps the passed value or the block # result with Result::Success. # # @example # Dry::Monads.Failure.new('no value').or_fmap('value') # => Success("value") # Dry::Monads.Failure.new('no value').or_fmap { 'value' } # => Success("value") # # @param args [Array<Object>] arguments will be passed to the underlying `#or` call # @return [Result::Success] Wrapped value def or_fmap(...) = Success.new(self.or(...)) # @return [String] def to_s if Unit.equal?(@value) "Failure()" else "Failure(#{@value.inspect})" end end alias_method :inspect, :to_s # Transform to a Success instance # # @return [Result::Success] def flip = Success.new(@value) # @see RightBiased::Left#value_or def value_or(val = nil) if block_given? yield(@value) else val end end # @param other [Result] # @return [Boolean] def ===(other) Failure === other && failure === other.failure end # Returns result of applying second function to the internal value. # # @example # Dry::Monads.Failure(1).either(-> x { x + 1 }, -> x { x + 2 }) # => 3 # # @param _ [#call] Ignored # @param g [#call] Function to call # @return [Any] Return value of `g` def either(_, g) = g.(failure) # Lifts a block/proc over Failure # # @overload alt_map(proc) # @param proc [#call] # @return [Result::Failure] # # @overload alt_map # @param block [Proc] # @return [Result::Failure] # def alt_map(proc = Undefined, &block) f = Undefined.default(proc, block) self.class.new(f.(failure), RightBiased::Left.trace_caller) end end # A module that can be included for easier access to Result monads. # # @api public module Mixin # @see Result::Success Success = Result::Success # @see Result::Failure Failure = Result::Failure # Value constructors # module Constructors # Success constructor # # @overload Success(value) # @param value [Object] # @return [Result::Success] # # @overload Success(&block) # @param block [Proc] a block to be wrapped with Success # @return [Result::Success] # def Success(value = Undefined, &block) v = Undefined.default(value, block || Unit) Success.new(v) end # Failure constructor # # @overload Success(value) # @param value [Object] # @return [Result::Failure] # # @overload Success(&block) # @param block [Proc] a block to be wrapped with Failure # @return [Result::Failure] # def Failure(value = Undefined, &block) v = Undefined.default(value, block || Unit) Failure.new(v, RightBiased::Left.trace_caller) end end include Constructors end end extend Result::Mixin::Constructors # @see Result::Success Success = Result::Success # @see Result::Failure Failure = Result::Failure # Creates a module that has two methods: `Success` and `Failure`. # `Success` is identical to {Result::Mixin::Constructors#Success} and Failure # rejects values that don't conform the value of the `error` # parameter. This is essentially a Result type with the `Failure` part # fixed. # # @example using dry-types # module Types # include Dry::Types.module # end # # class Operation # # :user_not_found and :account_not_found are the only # # values allowed as failure results # Error = # Types.Value(:user_not_found) | # Types.Value(:account_not_found) # # include Dry::Monads::Result(Error) # # def find_account(id) # account = acount_repo.find(id) # # account ? Success(account) : Failure(:account_not_found) # end # # def find_user(id) # # ... # end # end # # @param error [#===] the type of allowed failures # @return [Module] def self.Result(error, **options) Result::Fixed[error, **options] end class Maybe class Some < Maybe # Converts to Sucess(value!) # # @param fail [#call] Fallback value # @param block [Proc] Fallback block # @return [Success<Any>] def to_result(_fail = Unit, &) = Result::Success.new(@value) end class None < Maybe # Converts to Failure(fallback_value) # # @param fail [#call] Fallback value # @param block [Proc] Fallback block # @return [Failure<Any>] def to_result(fail = Unit) if block_given? Result::Failure.new(yield) else Result::Failure.new(fail) end end end end class Task # Converts to Result. Blocks the current thread if required. # # @return [Result] def to_result if promise.wait.fulfilled? Result::Success.new(promise.value) else Result::Failure.new(promise.reason, RightBiased::Left.trace_caller) end end end class Try class Value < Try # @return [Result::Success] def to_result = ::Dry::Monads::Result::Success.new(@value) end class Error < Try # @return [Result::Failure] def to_result Result::Failure.new(exception, RightBiased::Left.trace_caller) end end end class Validated class Valid < Validated # Converts to Result::Success # # @return [Result::Success] def to_result = Result.pure(value!) end class Invalid < Validated # Converts to Result::Failure # # @return [Result::Failure] def to_result Result::Failure.new(error, RightBiased::Left.trace_caller) end end end require "dry/monads/registry" register_mixin(:result, Result::Mixin
Returns false
Source
# File lib/dry/monads/result.rb, line 221 def to_s if Unit.equal?(@value) "Failure()" else "Failure(#{@value.inspect})" end end
@return [String]
Source
# File lib/dry/monads/result.rb, line 236 def value_or(val = nil) if block_given? yield(@value) else val end end
@see RightBiased::Left#value_or