class Dry::Monads::Maybe

Represents a value which can exist or not, i.e. it could be nil.

@api public

Private Class Methods

coerce(value) click to toggle source

Wraps the given value with into a Maybe object.

@param value [Object] value to be stored in the monad @return [Maybe::Some, Maybe::None]

# File lib/dry/monads/maybe.rb, line 22
def coerce(value)
  if value.nil?
    None.instance
  else
    Some.new(value)
  end
end
json_create(serialized) click to toggle source

Deserializes JSON string by using Dry::Monads::Maybe#lift method

# File lib/json/add/dry/monads/maybe.rb, line 14
def self.json_create(serialized)
  coerce(serialized.fetch("value"))
end
pure(value = Undefined, &block) click to toggle source

Wraps the given value with ‘Some`.

@param value [Object] value to be wrapped with Some @param block [Object] block to be wrapped with Some @return [Maybe::Some]

# File lib/dry/monads/maybe.rb, line 36
def pure(value = Undefined, &block)
  Some.new(Undefined.default(value, block))
end
to_proc() click to toggle source

Reutrns a Some wrapper converted to a block

@return [Proc]

# File lib/dry/monads/maybe.rb, line 43
def to_proc
  @to_proc ||= method(:coerce).to_proc
end

Private Instance Methods

as_json(*) click to toggle source

Returns a hash, that will be turned into a JSON object and represent this object.

# File lib/json/add/dry/monads/maybe.rb, line 20
def as_json(*)
  {
    JSON.create_id => self.class.name,
    value: none? ? nil : @value
  }
end
failure?()
Alias for: none?
monad() click to toggle source

Returns the Maybe monad. This is how we’re doing polymorphism in Ruby 😕

@return [Monad]

# File lib/dry/monads/maybe.rb, line 78
def monad
  Maybe
end
none?() click to toggle source

Returns true for an instance of a {Maybe::None} monad.

# File lib/dry/monads/maybe.rb, line 49
def none?
  is_a?(None)
end
Also aliased as: failure?
some?() click to toggle source

Returns true for an instance of a {Maybe::Some} monad.

# File lib/dry/monads/maybe.rb, line 55
def some?
  is_a?(Some)
end
Also aliased as: success?
success?()
Alias for: some?
to_json(...) click to toggle source

Stores class name (Dry::Monads::Maybe::Some or Dry::Monads::Maybe::None) with the monad value as JSON string

# File lib/json/add/dry/monads/maybe.rb, line 29
def to_json(...)
  as_json.to_json(...)
end
to_maybe() click to toggle source

Returns self, added to keep the interface compatible with that of Either monad types.

@return [Maybe::Some, Maybe::None]

# File lib/dry/monads/maybe.rb, line 63
def to_maybe
  self
end
to_monad() click to toggle source

Returns self.

@return [Maybe::Some, Maybe::None]

# File lib/dry/monads/maybe.rb, line 70
def to_monad
  self
end