class Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher

Constants

NON_NUMERIC_VALUE

Public Class Methods

new(attribute) click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 27
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 86
def description
  "only allow #{allowed_types} values for #{@attribute}"
end
even() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 70
def even
  even_number_matcher = OddEvenNumberMatcher.new(@attribute, :even => true)
  add_submatcher(even_number_matcher)
  self
end
failure_message_for_should() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 90
def failure_message_for_should
  submatcher_failure_messages_for_should.last
end
failure_message_for_should_not() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 94
def failure_message_for_should_not
  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 49
def is_equal_to(value)
  add_submatcher(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 39
def is_greater_than(value)
  add_submatcher(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 44
def is_greater_than_or_equal_to(value)
  add_submatcher(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 54
def is_less_than(value)
  add_submatcher(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 59
def is_less_than_or_equal_to(value)
  add_submatcher(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 81
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 64
def odd
  odd_number_matcher = OddEvenNumberMatcher.new(@attribute, :odd => true)
  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 34
def only_integer
  add_submatcher(OnlyIntegerMatcher.new(@attribute))
  self
end
with_message(message) click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 76
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 100
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 108
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 128
def allowed_types
  allowed = ['numeric'] + submatcher_allowed_types
  allowed.join(', ')
end
failing_submatchers() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 124
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 133
def submatcher_allowed_types
  @submatchers.map(&:allowed_types).reject(&:empty?)
end
submatcher_failure_messages_for_should() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 116
def submatcher_failure_messages_for_should
  failing_submatchers.map(&:failure_message_for_should)
end
submatcher_failure_messages_for_should_not() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 120
def submatcher_failure_messages_for_should_not
  failing_submatchers.map(&:failure_message_for_should_not)
end
submatchers_match?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 112
def submatchers_match?
  failing_submatchers.empty?
end