class Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher

@private

Constants

DEFAULT_DIFF_TO_COMPARE
NON_NUMERIC_VALUE
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 289
def initialize(attribute)
  @attribute = attribute
  @submatchers = []
  @diff_to_compare = DEFAULT_DIFF_TO_COMPARE
  add_disallow_value_matcher
end

Public Instance Methods

allow_nil() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 303
def allow_nil
  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 361
def description
  "only allow #{allowed_types} for #{@attribute}#{comparison_descriptions}"
end
even() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 319
def even
  prepare_submatcher(
    NumericalityMatchers::EvenNumberMatcher.new(@attribute)
  )
  self
end
failure_message() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 365
def failure_message
  last_failing_submatcher.failure_message
end
Also aliased as: failure_message_for_should
failure_message_for_should()
Alias for: failure_message
failure_message_for_should_not()
failure_message_when_negated() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 370
def failure_message_when_negated
  last_failing_submatcher.failure_message_when_negated
end
is_equal_to(value) click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 336
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 326
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 331
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 341
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 346
def is_less_than_or_equal_to(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 356
def matches?(subject)
  @subject = subject
  failing_submatchers.empty?
end
odd() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 312
def odd
  prepare_submatcher(
    NumericalityMatchers::OddNumberMatcher.new(@attribute)
  )
  self
end
only_integer() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 296
def only_integer
  prepare_submatcher(
    NumericalityMatchers::OnlyIntegerMatcher.new(@attribute)
  )
  self
end
with_message(message) click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 351
def with_message(message)
  @submatchers.each { |matcher| matcher.with_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 377
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 398
def add_submatcher(submatcher)
  @submatchers << submatcher
end
allowed_types() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 423
def allowed_types
  allowed_array = submatcher_allowed_types
  allowed_array.empty? ? NUMERIC_NAME : allowed_array.join(', ')
end
comparison_descriptions() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 432
def comparison_descriptions
  description_array = submatcher_comparison_descriptions
  description_array.empty? ? '' : ' which are ' + submatcher_comparison_descriptions.join(' and ')
end
comparison_matcher_for(value, operator) click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 392
def comparison_matcher_for(value, operator)
  NumericalityMatchers::ComparisonMatcher
    .new(self, value, operator)
    .for(@attribute)
end
failing_submatchers() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 413
def failing_submatchers
  submatchers_and_results.
    select { |x| !x[:matched] }.
    map { |x| x[:matcher] }
end
last_failing_submatcher() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 419
def last_failing_submatcher
  failing_submatchers.last
end
prepare_submatcher(submatcher) click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 385
def prepare_submatcher(submatcher)
  add_submatcher(submatcher)
  if submatcher.respond_to?(:diff_to_compare)
    update_diff_to_compare(submatcher)
  end
end
submatcher_allowed_types() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 428
def submatcher_allowed_types
  @submatchers.inject([]){|m, s| m << s.allowed_type if s.respond_to?(:allowed_type); m }
end
submatcher_comparison_descriptions() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 437
def submatcher_comparison_descriptions
  @submatchers.inject([]) do |arr, submatcher|
    if submatcher.respond_to? :comparison_description
      arr << submatcher.comparison_description
    end
    arr
  end
end
submatchers_and_results() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 406
def submatchers_and_results
  @_submatchers_and_results ||=
    @submatchers.map do |matcher|
      { matcher: matcher, matched: matcher.matches?(@subject) }
    end
end
update_diff_to_compare(matcher) click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 402
def update_diff_to_compare(matcher)
  @diff_to_compare = [@diff_to_compare, matcher.diff_to_compare].max
end