Module: Ytry::Try
Class Method Summary collapse
Instance Method Summary collapse
- #each ⇒ Object (also: #on_success)
- #flat_map(&block) ⇒ Object (also: #collect_concat)
- #flatten(level = 1) ⇒ Object
- #get_or_else ⇒ Object
- #grep(pattern) ⇒ Object
- #inspect ⇒ Object
- #map(&block) ⇒ Object (also: #collect)
- #on_failure ⇒ Object
- #or_else ⇒ Object
- #reject(&block) ⇒ Object
- #select(&block) ⇒ Object
- #zip(*others) ⇒ Object
- #|(lambda) ⇒ Object
Class Method Details
.ary_to_type(value) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/ytry.rb', line 14 def self.ary_to_type value raise Try.invalid_argument('Argument must be an array-like object', value) unless value.respond_to? :to_ary return value if value.is_a? Try value.to_ary.empty? ? Failure.new(RuntimeError.new("Element not found").tap{|ex| ex.set_backtrace(caller)}) : Success.new(value.to_ary.first) end |
Instance Method Details
#each ⇒ Object Also known as: on_success
25 26 27 28 29 |
# File 'lib/ytry.rb', line 25 def each return enum_for(__method__) unless block_given? Try { yield self.get unless empty? } return self end |
#flat_map(&block) ⇒ Object Also known as: collect_concat
52 53 54 55 56 57 58 |
# File 'lib/ytry.rb', line 52 def flat_map &block block or return enum_for(method) return self if self.empty? wrapped_result = Try{block.call(self.get)} return wrapped_result if (!wrapped_result.empty? && !wrapped_result.get.respond_to?(:to_ary)) Try.ary_to_type(wrapped_result.flatten) end |
#flatten(level = 1) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/ytry.rb', line 67 def flatten level = 1 level.times.reduce(self) { |current, _| break(current) if current.empty? Try.ary_to_type current.get } end |
#get_or_else ⇒ Object
88 89 90 91 92 |
# File 'lib/ytry.rb', line 88 def get_or_else raise ArgumentError, 'missing block' unless block_given? return self.get unless empty? yield end |
#grep(pattern) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/ytry.rb', line 60 def grep pattern return self if self.empty? match = Try { (pattern === self.get) ? self.get : (raise RuntimeError.new("Element not found")) } block_given? ? match.map{|v| yield v} : match end |
#inspect ⇒ Object
93 |
# File 'lib/ytry.rb', line 93 def inspect() to_s end |
#map(&block) ⇒ Object Also known as: collect
35 36 37 38 |
# File 'lib/ytry.rb', line 35 def map &block block or return enum_for(__method__) self.empty? ? self : Try{block.call(self.get)} end |
#on_failure ⇒ Object
31 32 33 34 |
# File 'lib/ytry.rb', line 31 def on_failure return enum_for(__method__) unless block_given? return self end |
#or_else ⇒ Object
79 80 81 82 83 84 85 86 87 |
# File 'lib/ytry.rb', line 79 def or_else return self unless empty? candidate = Try{ yield } if (!candidate.empty? && !candidate.get.is_a?(Try)) raise(Try.invalid_argument('Block should evaluate to an instance of Try', candidate.get)) else candidate.flatten end end |
#reject(&block) ⇒ Object
47 48 49 50 51 |
# File 'lib/ytry.rb', line 47 def reject &block if block_given? then select {|v| ! block.call(v)} else enum_for(__method__) end end |
#select(&block) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/ytry.rb', line 40 def select &block block or return enum_for(__method__) return self if empty? predicate = Try{ block.call(self.get) } return predicate if predicate.empty? predicate.get ? self : Try.ary_to_type([]) end |
#zip(*others) ⇒ Object
73 74 75 |
# File 'lib/ytry.rb', line 73 def zip *others Try.zip(self, *others) end |
#|(lambda) ⇒ Object
76 77 78 |
# File 'lib/ytry.rb', line 76 def | lambda self.flat_map &lambda # slow but easy to read + supports symbols out of the box end |