Class: Mona::Result::Match

Inherits:
Object
  • Object
show all
Defined in:
lib/mona/result/match.rb

Overview

Use Match.call to respond to result success or failure

Result::Match.call(result) do |r|

r.ok                 { |value| ... }
r.err(error, **meta) { |failure, reason, **meta| ... }
r.err(error)         { |failure, reason, **meta| ... }
r.err                { |failure, reason, **meta| ... }

end

Since:

  • 0.1.0

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(result) ⇒ Match

Returns a new instance of Match.

Since:

  • 0.1.0



16
17
18
19
# File 'lib/mona/result/match.rb', line 16

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

Class Method Details

.call(result) ⇒ Object

Since:

  • 0.1.0



14
# File 'lib/mona/result/match.rb', line 14

def self.call(result, &) = new(result).call(&)

Instance Method Details

#callObject

Since:

  • 0.1.0



21
22
23
24
25
26
# File 'lib/mona/result/match.rb', line 21

def call
  catch @throw do
    yield self
    raise NoMatchError, @result
  end
end

#err(match_reason = nil, **match_meta) ⇒ Object

Since:

  • 0.1.0



34
35
36
37
38
39
40
# File 'lib/mona/result/match.rb', line 34

def err(match_reason = nil, **match_meta)
  @result.or_else do |failure, reason, **meta|
    if match_reason?(match_reason, reason) && match_meta?(match_meta, meta)
      throw @throw, yield(failure, reason, **meta)
    end
  end
end

#okObject

Since:

  • 0.1.0



28
29
30
31
32
# File 'lib/mona/result/match.rb', line 28

def ok
  @result.and_then do |value|
    throw @throw, yield(value)
  end
end