class Shoulda::Matchers::ActiveModel::ValidateComparisonOfMatcher

@private

Constants

DEFAULT_DIFF_TO_COMPARE
NUMERIC_NAME

Attributes

diff_to_compare[R]
number_of_submatchers[R]

Public Class Methods

new(attribute) click to toggle source
Calls superclass method
# File lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb, line 288
def initialize(attribute)
  super
  @submatchers = []
  @diff_to_compare = DEFAULT_DIFF_TO_COMPARE
  @expects_to_allow_nil = false
  @comparison_submatcher = false
end

Public Instance Methods

allow_nil() click to toggle source
# File lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb, line 296
def allow_nil
  @expects_to_allow_nil = true
  prepare_submatcher(allow_value_matcher(nil))
  self
end
does_not_match?(subject) click to toggle source
# File lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb, line 347
def does_not_match?(subject)
  @subject = subject
  @number_of_submatchers = @submatchers.size

  qualify_submatchers
  first_submatcher_that_fails_to_not_match.nil?
end
expects_to_allow_nil?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb, line 302
def expects_to_allow_nil?
  @expects_to_allow_nil
end
failure_message() click to toggle source
# File lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb, line 366
def failure_message
  overall_failure_message.dup.tap do |message|
    message << "\n"
    message << failure_message_for_first_submatcher_that_fails_to_match
  end
end
failure_message_when_negated() click to toggle source
# File lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb, line 373
def failure_message_when_negated
  overall_failure_message_when_negated.dup.tap do |message|
    message << "\n"
    message <<
      failure_message_for_first_submatcher_that_fails_to_not_match
  end
end
given_numeric_column?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb, line 381
def given_numeric_column?
  attribute_is_active_record_column? &&
    [:integer, :float, :decimal].include?(column_type)
end
is_equal_to(value) click to toggle source
# File lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb, line 316
def is_equal_to(value)
  prepare_submatcher(comparison_matcher_for(value, :==).for(attribute))
  self
end
is_greater_than(value) click to toggle source
# File lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb, line 306
def is_greater_than(value)
  prepare_submatcher(comparison_matcher_for(value, :>).for(attribute))
  self
end
is_greater_than_or_equal_to(value) click to toggle source
# File lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb, line 311
def is_greater_than_or_equal_to(value)
  prepare_submatcher(comparison_matcher_for(value, :>=).for(attribute))
  self
end
is_less_than(value) click to toggle source
# File lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb, line 321
def is_less_than(value)
  prepare_submatcher(comparison_matcher_for(value, :<).for(attribute))
  self
end
is_less_than_or_equal_to(value) click to toggle source
# File lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb, line 326
def is_less_than_or_equal_to(value)
  prepare_submatcher(comparison_matcher_for(value, :<=).for(attribute))
  self
end
is_other_than(value) click to toggle source
# File lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb, line 331
def is_other_than(value)
  prepare_submatcher(comparison_matcher_for(value, :!=).for(attribute))
  self
end
matches?(subject) click to toggle source
# File lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb, line 336
def matches?(subject)
  @subject = subject
  @number_of_submatchers = @submatchers.size
  unless @comparison_matcher
    raise(ArgumentError, "matcher isn't qualified with any comparison matcher")
  end

  qualify_submatchers
  first_submatcher_that_fails_to_match.nil?
end
simple_description() click to toggle source
# File lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb, line 355
def simple_description
  String.new.tap do |description|
    description << "validate that :#{attribute} looks like "
    description << Shoulda::Matchers::Util.a_or_an(allowed_type_name)

    if comparison_descriptions.present?
      description << " #{comparison_descriptions}"
    end
  end
end

Private Instance Methods

add_submatcher(submatcher) click to toggle source
# File lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb, line 416
def add_submatcher(submatcher)
  @submatchers << submatcher
end
allowed_type_name() click to toggle source
# File lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb, line 522
def allowed_type_name
  'value'
end
attribute_is_active_record_column?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb, line 388
def attribute_is_active_record_column?
  columns_hash.key?(attribute.to_s)
end
build_submatcher_failure_message_for( submatcher, failure_message_method ) click to toggle source
# File lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb, line 484
def build_submatcher_failure_message_for(
  submatcher,
  failure_message_method
)
  failure_message = submatcher.public_send(failure_message_method)
  submatcher_description = submatcher.simple_description.
    sub(/\bvalidate that\b/, 'validates').
    sub(/\bdisallow\b/, 'disallows').
    sub(/\ballow\b/, 'allows')
  submatcher_message =
    if number_of_submatchers_for_failure_message > 1
      "In checking that #{model.name} #{submatcher_description}, " +
        failure_message[0].downcase +
        failure_message[1..]
    else
      failure_message
    end

  Shoulda::Matchers.word_wrap(submatcher_message, indent: 2)
end
column_type() click to toggle source
# File lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb, line 392
def column_type
  columns_hash[attribute.to_s].type
end
columns_hash() click to toggle source
# File lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb, line 396
def columns_hash
  if subject.class.respond_to?(:columns_hash)
    subject.class.columns_hash
  else
    {}
  end
end
comparison_descriptions() click to toggle source
# File lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb, line 505
def comparison_descriptions
  description_array = submatcher_comparison_descriptions
  if description_array.empty?
    ''
  else
    submatcher_comparison_descriptions.join(' and ')
  end
end
comparison_matcher_for(value, operator) click to toggle source
# File lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb, line 409
def comparison_matcher_for(value, operator)
  @comparison_matcher = true
  ComparisonMatcher.
    new(self, value, operator).
    for(attribute)
end
failure_message_for_first_submatcher_that_fails_to_match() click to toggle source
# File lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb, line 470
def failure_message_for_first_submatcher_that_fails_to_match
  build_submatcher_failure_message_for(
    first_submatcher_that_fails_to_match,
    :failure_message,
  )
end
failure_message_for_first_submatcher_that_fails_to_not_match() click to toggle source
# File lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb, line 477
def failure_message_for_first_submatcher_that_fails_to_not_match
  build_submatcher_failure_message_for(
    first_submatcher_that_fails_to_not_match,
    :failure_message_when_negated,
  )
end
first_submatcher_that_fails_to_match() click to toggle source
# File lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb, line 456
def first_submatcher_that_fails_to_match
  @_first_submatcher_that_fails_to_match ||=
    @submatchers.detect do |submatcher|
      !submatcher.matches?(subject)
    end
end
first_submatcher_that_fails_to_not_match() click to toggle source
# File lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb, line 463
def first_submatcher_that_fails_to_not_match
  @_first_submatcher_that_fails_to_not_match ||=
    @submatchers.detect do |submatcher|
      submatcher.matches?(subject)
    end
end
has_been_qualified?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb, line 448
def has_been_qualified?
  @submatchers.any? { |submatcher| submatcher_qualified?(submatcher) }
end
non_numeric_value() click to toggle source
# File lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb, line 526
def non_numeric_value
  'abcd'
end
number_of_submatchers_for_failure_message() click to toggle source
# File lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb, line 440
def number_of_submatchers_for_failure_message
  if has_been_qualified?
    number_of_submatchers - 1
  else
    number_of_submatchers
  end
end
prepare_submatcher(submatcher) click to toggle source
# File lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb, line 404
def prepare_submatcher(submatcher)
  add_submatcher(submatcher)
  submatcher
end
qualify_submatchers() click to toggle source
# File lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb, line 420
def qualify_submatchers
  @submatchers.each do |submatcher|
    if @expects_strict
      submatcher.strict
    end

    if @expected_message.present?
      submatcher.with_message(@expected_message)
    end

    if @context
      submatcher.on(@context)
    end

    submatcher.ignoring_interference_by_writer(
      ignore_interference_by_writer,
    )
  end
end
submatcher_comparison_descriptions() click to toggle source
# File lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb, line 514
def submatcher_comparison_descriptions
  @submatchers.inject([]) do |arr, submatcher|
    arr << if submatcher.respond_to? :comparison_description
             submatcher.comparison_description
           end
  end
end
submatcher_qualified?(submatcher) click to toggle source
# File lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb, line 452
def submatcher_qualified?(submatcher)
  submatcher.instance_of?(ComparisonMatcher)
end