class Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher

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 33
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 47
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 105
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 63
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 109
def failure_message
  submatcher_failure_messages_for_should.last
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 114
def failure_message_when_negated
  submatcher_failure_messages_for_should_not.last
end
is_equal_to(value) click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 80
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 70
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 75
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 85
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 90
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 100
def matches?(subject)
  @subject = subject
  submatchers_match?
end
odd() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 56
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 40
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 95
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 121
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 142
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 166
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 175
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 136
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 162
def failing_submatchers
  @failing_submatchers ||= @submatchers.select { |matcher| !matcher.matches?(@subject) }
end
prepare_submatcher(submatcher) click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 129
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 171
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 180
def submatcher_comparison_descriptions
  @submatchers.inject([]) do |arr, submatcher|
    if submatcher.respond_to? :comparison_description
      arr << submatcher.comparison_description
    end
    arr
  end
end
submatcher_failure_messages_for_should() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 154
def submatcher_failure_messages_for_should
  failing_submatchers.map(&:failure_message)
end
submatcher_failure_messages_for_should_not() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 158
def submatcher_failure_messages_for_should_not
  failing_submatchers.map(&:failure_message_when_negated)
end
submatchers_match?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 150
def submatchers_match?
  failing_submatchers.empty?
end
update_diff_to_compare(matcher) click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 146
def update_diff_to_compare(matcher)
  @diff_to_compare = [@diff_to_compare, matcher.diff_to_compare].max
end