class Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher

@private

Constants

NON_NUMERIC_VALUE
NUMERIC_NAME

Public Class Methods

new(attribute) click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 257
def initialize(attribute)
  @attribute = attribute
  @submatchers = []

  add_disallow_value_matcher
end

Public Instance Methods

description() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 316
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 300
def even
  even_number_matcher = NumericalityMatchers::EvenNumberMatcher.new(@attribute)
  add_submatcher(even_number_matcher)
  self
end
failure_message() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 320
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 325
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 279
def is_equal_to(value)
  add_submatcher(NumericalityMatchers::ComparisonMatcher.new(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 269
def is_greater_than(value)
  add_submatcher(NumericalityMatchers::ComparisonMatcher.new(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 274
def is_greater_than_or_equal_to(value)
  add_submatcher(NumericalityMatchers::ComparisonMatcher.new(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 284
def is_less_than(value)
  add_submatcher(NumericalityMatchers::ComparisonMatcher.new(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 289
def is_less_than_or_equal_to(value)
  add_submatcher(NumericalityMatchers::ComparisonMatcher.new(value, :<=).for(@attribute))
  self
end
matches?(subject) click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 311
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 294
def odd
  odd_number_matcher = NumericalityMatchers::OddNumberMatcher.new(@attribute)
  add_submatcher(odd_number_matcher)
  self
end
only_integer() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 264
def only_integer
  add_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 306
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 332
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 340
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 360
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 369
def comparison_descriptions
  description_array = submatcher_comparison_descriptions
  description_array.empty? ? '' : ' which are ' + submatcher_comparison_descriptions.join(' and ')
end
failing_submatchers() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 356
def failing_submatchers
  @failing_submatchers ||= @submatchers.select { |matcher| !matcher.matches?(@subject) }
end
submatcher_allowed_types() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 365
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 374
def submatcher_comparison_descriptions
  @submatchers.inject([]){|m, s| m << s.comparison_description if s.respond_to?(:comparison_description); m }
end
submatcher_failure_messages_for_should() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 348
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 352
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 344
def submatchers_match?
  failing_submatchers.empty?
end