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

Class Method Summary collapse

Instance Method Summary collapse

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

#===, #and, #apply, #bind, #deconstruct, #discard, #flatten, 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



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

def initialize(value)
  @value = value
end

Class Method Details

.[](*value) ⇒ Object

Shortcut for Success([...])

@example include Dry::Monads[:result]

def call Success[200, {}, ['ok']] # => Success([200, {}, ['ok']]) end



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

def self.[](*value)
  new(value)
end

Instance Method Details

#either(f, _) ⇒ Any

Returns result of applying first function to the internal value.

Examples:

Dry::Monads.Success(1).either(-> x { x + 1 }, -> x { x + 2 }) # => 2

Parameters:

  • f (#call)

    Function to apply

  • _ (#call)

    Ignored

Returns:

  • (Any)

    Return value of f



128
129
130
# File 'lib/dry/monads/result.rb', line 128

def either(f, _)
  f.(success)
end

#failure?Boolean

Returns false

Returns:

  • (Boolean)


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

def failure?
  false
end

#flipResult::Failure

Transforms to a Failure instance

Returns:



145
146
147
# File 'lib/dry/monads/result.rb', line 145

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:



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

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

#result(_, f) ⇒ Object

Apply the second function to value.



93
94
95
# File 'lib/dry/monads/result.rb', line 93

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

#success?Boolean

Returns true

Returns:

  • (Boolean)


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

def success?
  true
end

#to_maybeMaybe::Some

Returns:



328
329
330
331
# File 'lib/dry/monads/maybe.rb', line 328

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)


133
134
135
136
137
138
139
# File 'lib/dry/monads/result.rb', line 133

def to_s
  if Unit.equal?(@value)
    'Success()'
  else
    "Success(#{@value.inspect})"
  end
end

#to_validatedValidated::Valid

Transforms to Validated

Returns:



289
290
291
# File 'lib/dry/monads/validated.rb', line 289

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