Module: Insist::Predicates
Constant Summary collapse
- PREDICATE_METHOD_RE =
/\?$/
Instance Method Summary collapse
-
#method_missing(m, *args) ⇒ Object
Pass through any ‘foo?’ style method calls to the ‘value’ and fail if the the return is false.
- #respond_to?(method) ⇒ Boolean
Methods included from Assert
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args) ⇒ Object
Pass through any ‘foo?’ style method calls to the ‘value’ and fail if the the return is false.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/insist/predicates.rb', line 15 def method_missing(m, *args) # If this is a predicate method (ends in question mark) # call it on the value and assert truthiness. if PREDICATE_METHOD_RE.match(m.to_s) insist { value }.respond_to?(m) # call the method, like .empty?, result must be truthy. result = value.send(m, *args) assert(result, "#{value.inspect}##{m} expected to return a truthy " \ "value, but returned #{value.inspect}") return result else return super(m, *args) end end |
Instance Method Details
#respond_to?(method) ⇒ Boolean
8 9 10 11 |
# File 'lib/insist/predicates.rb', line 8 def respond_to?(method) assert(value.respond_to?(method), "#{value} does not respond to the '#{method}' method") end |