Class: Dry::Monads::Result::Success

Inherits:
Dry::Monads::Result show all
Includes:
Dry::Monads::RightBiased::Right
Defined in:
lib/dry/monads/result.rb,
lib/dry/monads/maybe.rb,
lib/dry/monads/either.rb,
lib/dry/monads/validated.rb

Overview

Represents a value of a successful operation.

Constant Summary

Constants inherited from Dry::Monads::Result

Left, Right

Instance Attribute Summary

Attributes inherited from Dry::Monads::Result

#failure

Instance Method Summary collapse

Methods included from Dry::Monads::RightBiased::Right

#===, #apply, #bind, #discard, included, #or, #or_fmap, #tee, #value!, #value_or

Methods inherited from Dry::Monads::Result

#monad, pure, #to_monad, #to_result

Methods included from Transformer

#fmap2, #fmap3

Constructor Details

#initialize(value) ⇒ Success

Returns a new instance of Success.

Parameters:

  • value (Object)

    a value of a successful operation



72
73
74
# File 'lib/dry/monads/result.rb', line 72

def initialize(value)
  @value = value
end

Instance Method Details

#failure?Boolean

Returns false

Returns:

  • (Boolean)


84
85
86
# File 'lib/dry/monads/result.rb', line 84

def failure?
  false
end

#flipResult::Failure

Transforms to a Failure instance

Returns:



115
116
117
# File 'lib/dry/monads/result.rb', line 115

def flip
  Failure.new(@value, RightBiased::Left.trace_caller)
end

#fmap(*args, &block) ⇒ Result::Success

Does the same thing as #bind except it also wraps the value in an instance of Result::Success monad. This allows for easier chaining of calls.

Examples:

Dry::Monads.Success(4).fmap(&:succ).fmap(->(n) { n**2 }) # => Success(25)

Parameters:

  • args (Array<Object>)

    arguments will be transparently passed through to #bind

Returns:



102
103
104
# File 'lib/dry/monads/result.rb', line 102

def fmap(*args, &block)
  Success.new(bind(*args, &block))
end

#result(_, f) ⇒ Object

Apply the second function to value.



79
80
81
# File 'lib/dry/monads/result.rb', line 79

def result(_, f)
  f.(@value)
end

#success?Boolean

Returns true

Returns:

  • (Boolean)


89
90
91
# File 'lib/dry/monads/result.rb', line 89

def success?
  true
end

#to_maybeMaybe::Some

Returns:



237
238
239
240
# File 'lib/dry/monads/maybe.rb', line 237

def to_maybe
  Kernel.warn 'Success(nil) transformed to None' if @value.nil?
  Dry::Monads::Maybe(@value)
end

#to_sString Also known as: inspect

Returns:

  • (String)


107
108
109
# File 'lib/dry/monads/result.rb', line 107

def to_s
  "Success(#{ @value.inspect })"
end

#to_validatedValidated::Valid

Transforms to Validated

Returns:



285
286
287
# File 'lib/dry/monads/validated.rb', line 285

def to_validated
  Validated::Valid.new(value!)
end