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 341
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 371
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 467
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 449
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 392
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 435
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 360
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 381
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 471
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 478
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 486
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 409
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 399
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 404
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_numericality_of_matcher.rb, line 414
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 419
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 424
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 444
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 385
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 439
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 364
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 454
def simple_description
  description = ''

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

  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 355
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 429
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 531
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 551
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 515
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 633
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..-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 519
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 523
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 658
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 545
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 619
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 626
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 605
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 612
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 654
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 598
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 493
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 676
def model
  @subject.class
end
non_numeric_value() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 680
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 590
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 501
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 508
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 540
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 570
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
submatcher_comparison_descriptions() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 667
def submatcher_comparison_descriptions
  @submatchers.inject([]) do |arr, submatcher|
    if submatcher.respond_to? :comparison_description
      arr << submatcher.comparison_description
    end
    arr
  end
end