class Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher

@private

Constants

DEFAULT_DIFF_TO_COMPARE
NUMERIC_NAME

Attributes

diff_to_compare[R]

Public Class Methods

new(attribute) click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 368
def initialize(attribute)
  super
  @attribute = attribute
  @submatchers = []
  @diff_to_compare = DEFAULT_DIFF_TO_COMPARE
  @expects_custom_validation_message = false
  @expects_to_allow_nil = false
  @expects_strict = false
  @allowed_type_adjective = nil
  @allowed_type_name = 'number'
  @context = nil
  @expected_message = nil
end

Public Instance Methods

allow_nil() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 398
def allow_nil
  @expects_to_allow_nil = true
  prepare_submatcher(
    AllowValueMatcher.new(nil).
      for(@attribute).
      with_message(:not_a_number),
  )
  self
end
description() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 505
def description
  ValidationMatcher::BuildDescription.call(self, simple_description)
end
does_not_match?(subject) click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 483
def does_not_match?(subject)
  matches_or_does_not_match?(subject)
  first_submatcher_that_fails_to_not_match.nil?
end
even() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 419
def even
  prepare_submatcher(
    NumericalityMatchers::EvenNumberMatcher.new(self, @attribute),
  )
  self
end
expects_custom_validation_message?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 469
def expects_custom_validation_message?
  @expects_custom_validation_message
end
expects_strict?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 387
def expects_strict?
  @expects_strict
end
expects_to_allow_nil?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 408
def expects_to_allow_nil?
  @expects_to_allow_nil
end
failure_message() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 509
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_numericality_of_matcher.rb, line 516
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_numericality_of_matcher.rb, line 524
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_numericality_of_matcher.rb, line 436
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_numericality_of_matcher.rb, line 426
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_numericality_of_matcher.rb, line 431
def is_greater_than_or_equal_to(value)
  prepare_submatcher(comparison_matcher_for(value, :>=).for(@attribute))
  self
end
is_in(range) click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 456
def is_in(range)
  prepare_submatcher(
    NumericalityMatchers::RangeMatcher.new(self, @attribute, range),
  )
  self
end
is_less_than(value) click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 441
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_numericality_of_matcher.rb, line 446
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_numericality_of_matcher.rb, line 451
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_numericality_of_matcher.rb, line 478
def matches?(subject)
  matches_or_does_not_match?(subject)
  first_submatcher_that_fails_to_match.nil?
end
odd() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 412
def odd
  prepare_submatcher(
    NumericalityMatchers::OddNumberMatcher.new(self, @attribute),
  )
  self
end
on(context) click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 473
def on(context)
  @context = context
  self
end
only_integer() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 391
def only_integer
  prepare_submatcher(
    NumericalityMatchers::OnlyIntegerMatcher.new(self, @attribute),
  )
  self
end
simple_description() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 488
def simple_description
  description = ''

  description << "validate that :#{@attribute} looks like "
  description << Shoulda::Matchers::Util.a_or_an(full_allowed_type)

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

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

  description
end
strict() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 382
def strict
  @expects_strict = true
  self
end
with_message(message) click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 463
def with_message(message)
  @expects_custom_validation_message = true
  @expected_message = message
  self
end

Private Instance Methods

add_disallow_value_matcher() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 569
def add_disallow_value_matcher
  disallow_value_matcher = DisallowValueMatcher.
    new(non_numeric_value).
    for(@attribute).
    with_message(:not_a_number)

  add_submatcher(disallow_value_matcher)
end
add_submatcher(submatcher) click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 589
def add_submatcher(submatcher)
  if submatcher.respond_to?(:allowed_type_name)
    @allowed_type_name = submatcher.allowed_type_name
  end

  if submatcher.respond_to?(:allowed_type_adjective)
    @allowed_type_adjective = submatcher.allowed_type_adjective
  end

  if submatcher.respond_to?(:diff_to_compare)
    @diff_to_compare = [
      @diff_to_compare,
      submatcher.diff_to_compare,
    ].max
  end

  @submatchers << submatcher
end
attribute_is_active_record_column?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 553
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_numericality_of_matcher.rb, line 671
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_numericality_of_matcher.rb, line 557
def column_type
  columns_hash[@attribute.to_s].type
end
columns_hash() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 561
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_numericality_of_matcher.rb, line 696
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_numericality_of_matcher.rb, line 583
def comparison_matcher_for(value, operator)
  NumericalityMatchers::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_numericality_of_matcher.rb, line 657
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_numericality_of_matcher.rb, line 664
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_numericality_of_matcher.rb, line 643
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_numericality_of_matcher.rb, line 650
def first_submatcher_that_fails_to_not_match
  @_first_submatcher_that_fails_to_not_match ||=
    @submatchers.detect do |submatcher|
      !submatcher.does_not_match?(@subject)
    end
end
full_allowed_type() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 692
def full_allowed_type
  "#{@allowed_type_adjective} #{@allowed_type_name}".strip
end
has_been_qualified?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 636
def has_been_qualified?
  @submatchers.any? do |submatcher|
    Shoulda::Matchers::RailsShim.parent_of(submatcher.class) ==
      NumericalityMatchers
  end
end
matches_or_does_not_match?(subject) click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 531
def matches_or_does_not_match?(subject)
  @subject = subject
  @number_of_submatchers = @submatchers.size

  add_disallow_value_matcher
  qualify_submatchers
end
model() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 722
def model
  @subject.class
end
non_numeric_value() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 726
def non_numeric_value
  'abcd'
end
number_of_submatchers_for_failure_message() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 628
def number_of_submatchers_for_failure_message
  if has_been_qualified?
    @submatchers.size - 1
  else
    @submatchers.size
  end
end
overall_failure_message() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 539
def overall_failure_message
  Shoulda::Matchers.word_wrap(
    "Expected #{model.name} to #{description}, but this could not "\
    'be proved.',
  )
end
overall_failure_message_when_negated() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 546
def overall_failure_message_when_negated
  Shoulda::Matchers.word_wrap(
    "Expected #{model.name} not to #{description}, but this could not "\
    'be proved.',
  )
end
prepare_submatcher(submatcher) click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 578
def prepare_submatcher(submatcher)
  add_submatcher(submatcher)
  submatcher
end
qualify_submatchers() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 608
def qualify_submatchers
  @submatchers.each do |submatcher|
    if @expects_strict
      submatcher.strict(@expects_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
range_description() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 714
def range_description
  range_submatcher = @submatchers.detect do |submatcher|
    submatcher.respond_to? :range_description
  end

  range_submatcher&.range_description
end
submatcher_comparison_descriptions() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 705
def submatcher_comparison_descriptions
  @submatchers.inject([]) do |arr, submatcher|
    if submatcher.respond_to? :comparison_description
      arr << submatcher.comparison_description
    end
    arr
  end
end