module Insist::Predicates

Constants

PREDICATE_METHOD_RE

Public Instance Methods

is_a?(klass) click to toggle source

Fails if the value.is_a?(klass) returns false.

insist { “hurray” }.is_a?(Number)

# File lib/insist/predicates.rb, line 19
def is_a?(klass)
  assert(value.is_a?(klass), "#{value.class} is not a #{klass}")
end
method_missing(m, *args) click to toggle source

Pass through any ‘foo?’ style method calls to the ‘value’ and fail if the the return is false.

Calls superclass method
# File lib/insist/predicates.rb, line 25
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.class}{#{value.inspect}}##{m}(#{args.join(",")}) " \
           "expected to return a truthy value, but returned #{result}")
    return result
  else
    return super(m, *args)
  end
end
respond_to?(method) click to toggle source

Fails if the value does not respond to a method.

insist { “hurray” }.respond_to?(:size)

# File lib/insist/predicates.rb, line 11
def respond_to?(method)
  assert(value.respond_to?(method),
         "#{value.class} does not respond to the '#{method}' method")
end