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
logstash-output-scalyr-0.2.1.beta vendor/bundle/jruby/2.5.0/gems/insist-1.0.0/lib/insist/predicates.rb
logstash-output-scalyr-0.2.0 vendor/bundle/jruby/2.5.0/gems/insist-1.0.0/lib/insist/predicates.rb
logstash-output-scalyr-0.2.0.beta vendor/bundle/jruby/2.5.0/gems/insist-1.0.0/lib/insist/predicates.rb
logstash-output-scalyr-0.1.26.beta vendor/bundle/jruby/2.5.0/gems/insist-1.0.0/lib/insist/predicates.rb
logstash-output-scalyr-0.1.25.beta vendor/bundle/jruby/2.5.0/gems/insist-1.0.0/lib/insist/predicates.rb
logstash-output-scalyr-0.1.24.beta vendor/bundle/jruby/2.5.0/gems/insist-1.0.0/lib/insist/predicates.rb
logstash-output-scalyr-0.1.23.beta vendor/bundle/jruby/2.5.0/gems/insist-1.0.0/lib/insist/predicates.rb
logstash-output-scalyr-0.1.22.beta vendor/bundle/jruby/2.5.0/gems/insist-1.0.0/lib/insist/predicates.rb
logstash-output-scalyr-0.1.21.beta vendor/bundle/jruby/2.5.0/gems/insist-1.0.0/lib/insist/predicates.rb
logstash-output-scalyr-0.1.20.beta vendor/bundle/jruby/2.5.0/gems/insist-1.0.0/lib/insist/predicates.rb
logstash-output-scalyr-0.1.19.beta vendor/bundle/jruby/2.5.0/gems/insist-1.0.0/lib/insist/predicates.rb
logstash-output-scalyr-0.1.18.beta vendor/bundle/jruby/2.5.0/gems/insist-1.0.0/lib/insist/predicates.rb
logstash-output-scalyr-0.1.17.beta vendor/bundle/jruby/2.5.0/gems/insist-1.0.0/lib/insist/predicates.rb
logstash-output-scalyr-0.1.16.beta vendor/bundle/jruby/2.5.0/gems/insist-1.0.0/lib/insist/predicates.rb
logstash-output-scalyr-0.1.15.beta vendor/bundle/jruby/2.5.0/gems/insist-1.0.0/lib/insist/predicates.rb
logstash-output-scalyr-0.1.14.beta vendor/bundle/jruby/2.5.0/gems/insist-1.0.0/lib/insist/predicates.rb
logstash-output-scalyr-0.1.13 vendor/bundle/jruby/2.5.0/gems/insist-1.0.0/lib/insist/predicates.rb
logstash-output-scalyr-0.1.12 vendor/bundle/jruby/2.5.0/gems/insist-1.0.0/lib/insist/predicates.rb
logstash-output-scalyr-0.1.11.beta vendor/bundle/jruby/2.5.0/gems/insist-1.0.0/lib/insist/predicates.rb
logstash-output-scalyr-0.1.10.beta vendor/bundle/jruby/2.5.0/gems/insist-1.0.0/lib/insist/predicates.rb