Class: Dry::Monads::Maybe

Inherits:
Object
  • Object
show all
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.

Direct Known Subclasses

None, Some

Defined Under Namespace

Modules: Hash, Mixin Classes: None, Some

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Transformer

#fmap2, #fmap3

Class Method Details

.coerce(value) ⇒ Maybe::Some, Maybe::None

Wraps the given value with into a Maybe object.

Parameters:

  • value (Object)

    value to be stored in the monad

Returns:



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`.

Parameters:

  • value (Object) (defaults to: Undefined)

    value to be wrapped with Some

  • block (Object)

    block to be wrapped with Some

Returns:



36
37
38
# File 'lib/dry/monads/maybe.rb', line 36

def pure(value = Undefined, &block)
  Some.new(Undefined.default(value, block))
end

.to_procProc

Reutrns a Some wrapper converted to a block

Returns:

  • (Proc)


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_jsonObject

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

#monadMonad

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

Returns:

  • (Monad)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


55
56
57
# File 'lib/dry/monads/maybe.rb', line 55

def some?
  is_a?(Some)
end

#to_jsonObject

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_maybeMaybe::Some, Maybe::None

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

Returns:



63
64
65
# File 'lib/dry/monads/maybe.rb', line 63

def to_maybe
  self
end

#to_monadMaybe::Some, Maybe::None

Returns self.

Returns:



70
71
72
# File 'lib/dry/monads/maybe.rb', line 70

def to_monad
  self
end