Class: Dry::Monads::Maybe
- Inherits:
-
Object
- Object
- Dry::Monads::Maybe
- Extended by:
- Core::ClassAttributes
- Includes:
- Transformer
- Defined in:
- lib/dry/monads/maybe.rb,
lib/dry/monads/result.rb,
lib/json/add/dry/monads/maybe.rb
Overview
Represents a value which can exist or not, i.e. it could be nil.
Defined Under Namespace
Modules: Hash, Mixin Classes: None, Some
Class Method Summary collapse
-
.coerce(value) ⇒ Maybe::Some, Maybe::None
Wraps the given value with into a Maybe object.
-
.json_create(serialized) ⇒ Object
Deserializes JSON string by using Dry::Monads::Maybe#lift method.
-
.pure(value = Undefined, &block) ⇒ Maybe::Some
Wraps the given value with ‘Some`.
-
.to_proc ⇒ Proc
Reutrns a Some wrapper converted to a block.
Instance Method Summary collapse
-
#as_json ⇒ Object
Returns a hash, that will be turned into a JSON object and represent this object.
-
#monad ⇒ Monad
Returns the Maybe monad.
-
#none? ⇒ Boolean
(also: #failure?)
Returns true for an instance of a None monad.
-
#some? ⇒ Boolean
(also: #success?)
Returns true for an instance of a Some monad.
-
#to_json ⇒ Object
Stores class name (Dry::Monads::Maybe::Some or Dry::Monads::Maybe::None) with the monad value as JSON string.
-
#to_maybe ⇒ Maybe::Some, Maybe::None
Returns self, added to keep the interface compatible with that of Either monad types.
-
#to_monad ⇒ Maybe::Some, Maybe::None
Returns self.
Methods included from Transformer
Class Method Details
.coerce(value) ⇒ Maybe::Some, Maybe::None
Wraps the given value with into a Maybe object.
22 23 24 25 26 27 28 |
# 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) ⇒ Object
Deserializes JSON string by using Dry::Monads::Maybe#lift method
14 15 16 |
# File 'lib/json/add/dry/monads/maybe.rb', line 14 def self.json_create(serialized) coerce(serialized.fetch("value")) end |
.pure(value = Undefined, &block) ⇒ Maybe::Some
Wraps the given value with ‘Some`.
36 37 38 |
# File 'lib/dry/monads/maybe.rb', line 36 def pure(value = Undefined, &block) Some.new(Undefined.default(value, block)) end |
.to_proc ⇒ Proc
Reutrns a Some wrapper converted to a block
43 44 45 |
# File 'lib/dry/monads/maybe.rb', line 43 def to_proc @to_proc ||= method(:coerce).to_proc end |
Instance Method Details
#as_json ⇒ Object
Returns a hash, that will be turned into a JSON object and represent this object.
20 21 22 23 24 25 |
# File 'lib/json/add/dry/monads/maybe.rb', line 20 def as_json(*) { JSON.create_id => self.class.name, value: none? ? nil : @value } end |
#monad ⇒ Monad
Returns the Maybe monad. This is how we’re doing polymorphism in Ruby 😕
78 79 80 |
# File 'lib/dry/monads/maybe.rb', line 78 def monad Maybe end |
#none? ⇒ Boolean Also known as: failure?
Returns true for an instance of a None monad.
49 50 51 |
# File 'lib/dry/monads/maybe.rb', line 49 def none? is_a?(None) end |
#some? ⇒ Boolean Also known as: success?
Returns true for an instance of a Some monad.
55 56 57 |
# File 'lib/dry/monads/maybe.rb', line 55 def some? is_a?(Some) end |
#to_json ⇒ Object
Stores class name (Dry::Monads::Maybe::Some or Dry::Monads::Maybe::None) with the monad value as JSON string
29 30 31 |
# File 'lib/json/add/dry/monads/maybe.rb', line 29 def to_json(...) as_json.to_json(...) end |
#to_maybe ⇒ Maybe::Some, Maybe::None
Returns self, added to keep the interface compatible with that of Either monad types.
63 64 65 |
# File 'lib/dry/monads/maybe.rb', line 63 def to_maybe self end |
#to_monad ⇒ Maybe::Some, Maybe::None
Returns self.
70 71 72 |
# File 'lib/dry/monads/maybe.rb', line 70 def to_monad self end |