Class: Dry::Monads::Maybe
- Inherits:
-
Object
- Object
- Dry::Monads::Maybe
- 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(*args) ⇒ 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.
24 25 26 27 28 29 30 |
# File 'lib/dry/monads/maybe.rb', line 24 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
13 14 15 |
# File 'lib/json/add/dry/monads/maybe.rb', line 13 def self.json_create(serialized) coerce(serialized.fetch('value')) end |
.pure(value = Undefined, &block) ⇒ Maybe::Some
Wraps the given value with Some
.
38 39 40 |
# File 'lib/dry/monads/maybe.rb', line 38 def pure(value = Undefined, &block) Some.new(Undefined.default(value, block)) end |
.to_proc ⇒ Proc
Reutrns a Some wrapper converted to a block
45 46 47 |
# File 'lib/dry/monads/maybe.rb', line 45 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.
19 20 21 22 23 24 |
# File 'lib/json/add/dry/monads/maybe.rb', line 19 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 😕
80 81 82 |
# File 'lib/dry/monads/maybe.rb', line 80 def monad Maybe end |
#none? ⇒ Boolean Also known as: failure?
Returns true for an instance of a None monad.
51 52 53 |
# File 'lib/dry/monads/maybe.rb', line 51 def none? is_a?(None) end |
#some? ⇒ Boolean Also known as: success?
Returns true for an instance of a Some monad.
57 58 59 |
# File 'lib/dry/monads/maybe.rb', line 57 def some? is_a?(Some) end |
#to_json(*args) ⇒ Object
Stores class name (Dry::Monads::Maybe::Some or Dry::Monads::Maybe::None) with the monad value as JSON string
28 29 30 |
# File 'lib/json/add/dry/monads/maybe.rb', line 28 def to_json(*args) as_json.to_json(*args) end |
#to_maybe ⇒ Maybe::Some, Maybe::None
Returns self, added to keep the interface compatible with that of Either monad types.
65 66 67 |
# File 'lib/dry/monads/maybe.rb', line 65 def to_maybe self end |
#to_monad ⇒ Maybe::Some, Maybe::None
Returns self.
72 73 74 |
# File 'lib/dry/monads/maybe.rb', line 72 def to_monad self end |