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

Inherits:
Dry::Monads::Result show all
Includes:
Dry::Monads::RightBiased::Right
Defined in:
lib/dry/monads/result.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, #or, #or_fmap, #tee, #value!, #value_or

Methods inherited from Dry::Monads::Result

#monad, pure, #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



54
55
56
# File 'lib/dry/monads/result.rb', line 54

def initialize(value)
  @value = value
end

Instance Method Details

#failure?Boolean Also known as: left?

Returns false

Returns:

  • (Boolean)


66
67
68
# File 'lib/dry/monads/result.rb', line 66

def failure?
  false
end

#flipResult::Failure

Transform to a Failure instance

Returns:



105
106
107
# File 'lib/dry/monads/result.rb', line 105

def flip
  Failure.new(@value)
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:



86
87
88
# File 'lib/dry/monads/result.rb', line 86

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

#result(_, f) ⇒ Object

Apply the second function to value.



61
62
63
# File 'lib/dry/monads/result.rb', line 61

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

#success?Boolean Also known as: right?

Returns true

Returns:

  • (Boolean)


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

def success?
  true
end

#to_maybeMaybe::Some

Returns:



97
98
99
100
# File 'lib/dry/monads/result.rb', line 97

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)


91
92
93
# File 'lib/dry/monads/result.rb', line 91

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