Module: ResultMethods
Instance Method Summary collapse
- #and_then ⇒ Object
- #error? ⇒ Boolean
- #flat_map(&block) ⇒ Object
- #flat_map_unsafe(&block) ⇒ Object
- #join ⇒ Object
-
#map ⇒ Object
TODO: map! Not necessarily a good idea map_unsafe! See above.
- #map_err ⇒ Object
- #map_unsafe ⇒ Object
- #ok? ⇒ Boolean
- #or_else ⇒ Object
- #to_s ⇒ Object
- #unwrap ⇒ Object
Instance Method Details
#and_then ⇒ Object
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
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 |
#join ⇒ Object
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 |
#map ⇒ Object
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_err ⇒ Object
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_unsafe ⇒ Object
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 |
#or_else ⇒ Object
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_s ⇒ Object
76 77 78 |
# File 'lib/result-monad/result_methods.rb', line 76 def to_s "#{ self.class.name }: #{@result}" end |
#unwrap ⇒ Object
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 |