Module: ResultMethods

Included in:
Error, Ok
Defined in:
lib/result-monad/result_methods.rb

Instance Method Summary collapse

Instance Method Details

#and_thenObject



37
38
39
40
41
42
# File 'lib/result-monad/result_methods.rb', line 37

def and_then
  handle_result(
    Proc.new {|val| yield(val); self},
    Proc.new {|err| self}
  )
end

#error?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/result-monad/result_methods.rb', line 72

def error?
  is_a? Error
end

#flat_map(&block) ⇒ Object



26
27
28
# File 'lib/result-monad/result_methods.rb', line 26

def flat_map(&block)
  (map &block).join
end

#flat_map_unsafe(&block) ⇒ Object



51
52
53
# File 'lib/result-monad/result_methods.rb', line 51

def flat_map_unsafe(&block)
  (map_unsafe &block).join
end

#joinObject



19
20
21
22
23
24
# File 'lib/result-monad/result_methods.rb', line 19

def join
  handle_result(
    Proc.new { |x| x.is_a?(Ok) || x.is_a?(Error) ? x.join : self },
    Proc.new { |x| self }
  )
end

#mapObject

TODO: map! Not necessarily a good idea map_unsafe! See above



5
6
7
8
9
10
# File 'lib/result-monad/result_methods.rb', line 5

def map
  handle_result(
    Proc.new {|val| Capture { yield(val) } },
    Proc.new {|err| self }
  )
end

#map_errObject



12
13
14
15
16
17
# File 'lib/result-monad/result_methods.rb', line 12

def map_err
  handle_result(
    Proc.new {|val| self },
    Proc.new {|val| Capture { yield(val) } }
  )
end

#map_unsafeObject



30
31
32
33
34
35
# File 'lib/result-monad/result_methods.rb', line 30

def map_unsafe
  handle_result(
    Proc.new {|val| yield(val) },
    Proc.new {|err| self }
  )
end

#ok?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/result-monad/result_methods.rb', line 68

def ok?
  is_a? Ok
end

#or_elseObject



44
45
46
47
48
49
# File 'lib/result-monad/result_methods.rb', line 44

def or_else
  handle_result(
    Proc.new {|val| self},
    Proc.new {|err| yield(err); self}
  )
end

#to_sObject



76
77
78
# File 'lib/result-monad/result_methods.rb', line 76

def to_s
  "#{ self.class.name }: #{@result}"
end

#unwrapObject



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/result-monad/result_methods.rb', line 55

def unwrap
  handle_result(
    Proc.new {|val| val},
    Proc.new do |err|
      if err.is_a? Exception
        raise err
      else
        raise ResultError.new, "#{err}"
      end
    end
  )
end