Class: Dry::Monads::Maybe

Inherits:
Object
  • Object
show all
Includes:
Transformer
Defined in:
lib/dry/monads/maybe.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: 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:


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.

Parameters:

  • value (Object) (defaults to: Undefined)

    value to be wrapped with Some

  • block (Object)

    block to be wrapped with Some

Returns:


38
39
40
# File 'lib/dry/monads/maybe.rb', line 38

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

.to_procProc

Reutrns a Some wrapper converted to a block

Returns:

  • (Proc)

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_jsonObject

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

#monadMonad

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

Returns:

  • (Monad)

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.

Returns:

  • (Boolean)

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.

Returns:

  • (Boolean)

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

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

Returns:


65
66
67
# File 'lib/dry/monads/maybe.rb', line 65

def to_maybe
  self
end

#to_monadMaybe::Some, Maybe::None

Returns self.

Returns:


72
73
74
# File 'lib/dry/monads/maybe.rb', line 72

def to_monad
  self
end