class RSpec::Matchers::BuiltIn::Has

@api private Provides the implementation for `has_<predicate>`. Not intended to be instantiated directly.

Public Class Methods

new(method_name, *args, &block) click to toggle source
# File lib/rspec/matchers/built_in/has.rb, line 10
def initialize(method_name, *args, &block)
  @method_name, @args, @block = method_name, args, block
end

Public Instance Methods

description() click to toggle source

@api private @return [String]

# File lib/rspec/matchers/built_in/has.rb, line 42
def description
  [method_description, args_description].compact.join(' ')
end
does_not_match?(actual, &block) click to toggle source

@private

# File lib/rspec/matchers/built_in/has.rb, line 22
def does_not_match?(actual, &block)
  @actual = actual
  @block ||= block
  predicate_accessible? && !predicate_matches?
end
failure_message() click to toggle source

@api private @return [String]

# File lib/rspec/matchers/built_in/has.rb, line 30
def failure_message
  validity_message || "expected ##{predicate}#{failure_message_args_description} to return true, got false"
end
failure_message_when_negated() click to toggle source

@api private @return [String]

# File lib/rspec/matchers/built_in/has.rb, line 36
def failure_message_when_negated
  validity_message || "expected ##{predicate}#{failure_message_args_description} to return false, got true"
end
matches?(actual, &block) click to toggle source

@private

# File lib/rspec/matchers/built_in/has.rb, line 15
def matches?(actual, &block)
  @actual = actual
  @block ||= block
  predicate_accessible? && predicate_matches?
end
supports_block_expectations?() click to toggle source

@private

# File lib/rspec/matchers/built_in/has.rb, line 47
def supports_block_expectations?
  false
end

Private Instance Methods

args_description() click to toggle source
# File lib/rspec/matchers/built_in/has.rb, line 84
def args_description
  return nil if @args.empty?
  @args.map { |arg| arg.inspect }.join(', ')
end
failure_message_args_description() click to toggle source
# File lib/rspec/matchers/built_in/has.rb, line 89
def failure_message_args_description
  desc = args_description
  "(#{desc})" if desc
end
method_description() click to toggle source
# File lib/rspec/matchers/built_in/has.rb, line 80
def method_description
  @method_name.to_s.gsub('_', ' ')
end
predicate() click to toggle source
# File lib/rspec/matchers/built_in/has.rb, line 76
def predicate
  @predicate ||= :"has_#{@method_name.to_s.match(Matchers::HAS_REGEX).captures.first}?"
end
predicate_accessible?() click to toggle source
# File lib/rspec/matchers/built_in/has.rb, line 53
def predicate_accessible?
  !private_predicate? && predicate_exists?
end
predicate_exists?() click to toggle source
# File lib/rspec/matchers/built_in/has.rb, line 68
def predicate_exists?
  @actual.respond_to? predicate
end
predicate_matches?() click to toggle source
# File lib/rspec/matchers/built_in/has.rb, line 72
def predicate_matches?
  @actual.__send__(predicate, *@args, &@block)
end
private_predicate?() click to toggle source
# File lib/rspec/matchers/built_in/has.rb, line 59
def private_predicate?
  @actual.private_methods.include? predicate.to_s
end
validity_message() click to toggle source
# File lib/rspec/matchers/built_in/has.rb, line 94
def validity_message
  if private_predicate?
    "expected #{@actual} to respond to `#{predicate}` but `#{predicate}` is a private method"
  elsif !predicate_exists?
    "expected #{@actual} to respond to `#{predicate}`"
  end
end