Sha256: 77705e5a568c3c55e89c3198063eb5690022022ada962245aa0f6b25149c2a14

Contents?: true

Size: 1.27 KB

Versions: 67

Compression:

Stored size: 1.27 KB

Contents

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

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

  # Fails if the value does not respond to a method.
  # 
  # insist { "hurray" }.respond_to?(:size)
  def respond_to?(method)
    assert(value.respond_to?(method),
           "#{value.class} does not respond to the '#{method}' method")
  end # def respond_to?

  # Fails if the value.is_a?(klass) returns false.
  # 
  # insist { "hurray" }.is_a?(Number)
  def is_a?(klass)
    assert(value.is_a?(klass), "#{value.class} is not a #{klass}")
  end

  # 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.class}{#{value.inspect}}##{m}(#{args.join(",")}) " \
             "expected to return a truthy value, but returned #{result}")
      return result
    else
      return super(m, *args)
    end
  end # def method_missing
end # module Insist::Predicates

Version data entries

67 entries across 63 versions & 18 rubygems

Version Path
able-neo4j-1.0.0 vendor/bundle/jruby/1.9/gems/insist-1.0.0/lib/insist/predicates.rb
logstash-input-beats-2.0.2 vendor/jruby/1.9/gems/logstash-codec-json-2.0.3/vendor/gems/insist-1.0.0/lib/insist/predicates.rb
logstash-input-beats-2.0.2 vendor/jruby/1.9/gems/insist-1.0.0/lib/insist/predicates.rb
logstash-codec-json-2.0.3 vendor/gems/insist-1.0.0/lib/insist/predicates.rb
logstash-input-beats-0.9.2 vendor/jruby/1.9/gems/insist-1.0.0/lib/insist/predicates.rb
logstash-input-beats-0.9.1 vendor/jruby/1.9/gems/insist-1.0.0/lib/insist/predicates.rb
insist-1.0.0 lib/insist/predicates.rb