Class: Dry::Monads::Validated::Invalid

Inherits:
Dry::Monads::Validated show all
Defined in:
lib/dry/monads/validated.rb,
lib/dry/monads/maybe.rb,
lib/dry/monads/result.rb

Overview

Invalid result

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Dry::Monads::Validated

#bind, pure, #to_monad

Constructor Details

#initialize(error, trace = RightBiased::Left.trace_caller) ⇒ Invalid

Returns a new instance of Invalid.



150
151
152
153
# File 'lib/dry/monads/validated.rb', line 150

def initialize(error, trace = RightBiased::Left.trace_caller)
  @error = error
  @trace = trace
end

Instance Attribute Details

#errorObject (readonly)

The value stored inside

Returns:

  • (Object)


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

def error
  @error
end

#traceString (readonly)

Line where the value was constructed

Returns:

  • (String)


146
147
148
# File 'lib/dry/monads/validated.rb', line 146

def trace
  @trace
end

Instance Method Details

#===(other) ⇒ Boolean

Parameters:

  • other (Object)

Returns:

  • (Boolean)


216
217
218
# File 'lib/dry/monads/validated.rb', line 216

def ===(other)
  self.class == other.class && error === other.error
end

#alt_map(proc) ⇒ Validated::Invalid #alt_mapValidated::Invalid

Lifts a block/proc over Invalid

Overloads:



182
183
184
185
# File 'lib/dry/monads/validated.rb', line 182

def alt_map(proc = Undefined, &block)
  f = Undefined.default(proc, block)
  self.class.new(f.(error), RightBiased::Left.trace_caller)
end

#apply(val) ⇒ Validated::Invalid #applyValidated::Invalid

Collects errors (ignores valid results)

Overloads:



165
166
167
168
169
170
# File 'lib/dry/monads/validated.rb', line 165

def apply(val = Undefined)
  Undefined.
    default(val) { yield }.
    alt_map { |v| @error + v }.
    fmap { return self }
end

#fmap(_ = nil) ⇒ Validated::Invalid

Ignores the passed argument and returns self

Returns:



190
191
192
# File 'lib/dry/monads/validated.rb', line 190

def fmap(_ = nil)
  self
end

#inspectString Also known as: to_s

Returns:

  • (String)


209
210
211
# File 'lib/dry/monads/validated.rb', line 209

def inspect
  "Invalid(#{ @error.inspect })"
end

#or(proc) ⇒ Object #orObject

Yields the given callable and returns the result

Overloads:

  • #or(proc) ⇒ Object

    Parameters:

    • proc (#call)

    Returns:

    • (Object)
  • #orObject

    Parameters:

    • block (Proc)

    Returns:

    • (Object)


204
205
206
# File 'lib/dry/monads/validated.rb', line 204

def or(proc = Undefined, &block)
  Undefined.default(proc, block).call
end

#to_maybeMaybe::None

Converts to Maybe::None

Returns:



294
295
296
# File 'lib/dry/monads/maybe.rb', line 294

def to_maybe
  Maybe::None.new(RightBiased::Left.trace_caller)
end

#to_resultResult::Failure

Concerts to Result::Failure

Returns:



363
364
365
# File 'lib/dry/monads/result.rb', line 363

def to_result
  Result::Failure.new(error, RightBiased::Left.trace_caller)
end