Class: Mona::Result::Sequence

Inherits:
Object
  • Object
show all
Includes:
Dict::ReadInterface
Defined in:
lib/mona/result/sequence.rb

Overview

Sequence.call { … } allows monadic ‘do’ notation for Result::Dict, where #set-ing the first failure skips the remainder of the block and returns the Result::Dict

Since:

  • 0.1.0

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Dict::ReadInterface

#[], #fetch, #key?

Constructor Details

#initialize(result = Dict::EMPTY) ⇒ Sequence

Returns a new instance of Sequence.

Since:

  • 0.1.0



12
13
14
15
# File 'lib/mona/result/sequence.rb', line 12

def initialize(result = Dict::EMPTY)
  @result = result
  @throw = Object.new
end

Class Method Details

.call(result = Dict::EMPTY) ⇒ Object

Since:

  • 0.1.0



10
# File 'lib/mona/result/sequence.rb', line 10

def self.call(result = Dict::EMPTY, &) = new(result).call(&)

Instance Method Details

#callObject

Since:

  • 0.1.0



17
18
19
20
21
22
# File 'lib/mona/result/sequence.rb', line 17

def call
  catch(@throw) do
    yield self
    @result
  end
end

#set(key, value) ⇒ Object

Since:

  • 0.1.0



24
25
26
27
28
# File 'lib/mona/result/sequence.rb', line 24

def set(key, value)
  @result = @result.set(key, value)
ensure
  throw @throw, @result if @result.err?
end

#to_hObject

Since:

  • 0.1.0



30
# File 'lib/mona/result/sequence.rb', line 30

def to_h = @result.to_h