class Dry::Monads::Maybe
Represents a value which can exist or not, i.e. it could be nil.
@api public
Private Class Methods
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
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
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
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
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
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
Returns true for an instance of a {Maybe::None} monad.
# File lib/dry/monads/maybe.rb, line 49 def none? is_a?(None) end
Returns true for an instance of a {Maybe::Some} monad.
# File lib/dry/monads/maybe.rb, line 55 def some? is_a?(Some) end
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
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
Returns self.
@return [Maybe::Some, Maybe::None
]
# File lib/dry/monads/maybe.rb, line 70 def to_monad self end