Sha256: ec993161c4a3ea44c587c9d8a53e3ae527ae011921f5bacec28569d5180653e4

Contents?: true

Size: 966 Bytes

Versions: 4

Compression:

Stored size: 966 Bytes

Contents

require "insist/namespace"
require "insist/assert"

module Insist::Predicates
  include Insist::Assert
  PREDICATE_METHOD_RE = /\?$/

  def respond_to?(method)
    assert(value.respond_to?(method),
           "#{value} does not respond to the '#{method}' method")
  end # def respond_to?

  # Pass through any 'foo?' style method calls to the 'value' 
  # and fail if the the return is false.
  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 # def method_missing
end # module Insist::Predicates

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
insist-0.0.6 lib/insist/predicates.rb
insist-0.0.5 lib/insist/predicates.rb
insist-0.0.4 lib/insist/predicates.rb
insist-0.0.3 lib/insist/predicates.rb