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/validated.rb

Overview

Represents a value of a successful operation.

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, #deconstruct_keys, #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



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

def initialize(value)
  super()
  @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


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

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

Instance Method Details

#alt_map(_ = nil) ⇒ Result::Success

Ignores values and returns self, see Failure#alt_map

Returns:



147
148
149
# File 'lib/dry/monads/result.rb', line 147

def alt_map(_ = nil)
  self
end

#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`



123
124
125
# File 'lib/dry/monads/result.rb', line 123

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

#failure?Boolean

Returns false

Returns:

  • (Boolean)


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

def failure?
  false
end

#flipResult::Failure

Transforms to a Failure instance

Returns:



140
141
142
# File 'lib/dry/monads/result.rb', line 140

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

#fmapResult::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:



111
112
113
# File 'lib/dry/monads/result.rb', line 111

def fmap(...)
  Success.new(bind(...))
end

#result(_, f) ⇒ Object

Apply the second function to value.



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

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

#success?Boolean

Returns true

Returns:

  • (Boolean)


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

def success?
  true
end

#to_maybeMaybe

Returns:



388
389
390
391
# File 'lib/dry/monads/maybe.rb', line 388

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

#to_sString Also known as: inspect

Returns:

  • (String)


128
129
130
131
132
133
134
# File 'lib/dry/monads/result.rb', line 128

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

#to_validatedValidated::Valid

Transforms to Validated

Returns:



292
293
294
# File 'lib/dry/monads/validated.rb', line 292

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