Class: Enumerator
Instance Method Summary collapse
-
#result!(&block) ⇒ Object
Iterates the Enumerator with a given
block
, and checks that the result is not nil. -
#results!(&block) ⇒ Enumerable
Iterates the Enumerator with a given
block
, and checks that the result is an Enumerable that has one or more elements.
Instance Method Details
#result!(&block) ⇒ Object
Iterates the Enumerator with a given block
, and checks that the result is not nil. Raises an exception if the result is nil. Otherwise, returns the non-nil result.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/mini_sanity/results.rb', line 55 def result!(&block) raise ArgumentError, "Enumerator#result! requires a block" unless block result = self.each(&block) if result.nil? raise MiniSanity::Error.new("Nil result from Enumerator with block", { "Enumerator" => self.inspect, "Block" => MiniSanity::Error.describe_block(&block), }) end result end |
#results!(&block) ⇒ Enumerable
Iterates the Enumerator with a given block
, and checks that the result is an Enumerable that has one or more elements. Raises an exception if this check fails. Otherwise, returns the Enumerable result.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/mini_sanity/results.rb', line 21 def results!(&block) raise ArgumentError, "Enumerator#results! requires a block" unless block results = self.each(&block) if !results.is_a?(Enumerable) raise MiniSanity::Error.new("Result from Enumerator with block is not an Enumerable", { "Enumerator" => self.inspect, "Block" => MiniSanity::Error.describe_block(&block), "Result" => results.inspect, }) elsif !results.any?{ true } raise MiniSanity::Error.new("No results from Enumerator with block", { "Enumerator" => self.inspect, "Block" => MiniSanity::Error.describe_block(&block), }) end results end |