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
Calls superclass method
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 366 def initialize(attribute) super @submatchers = [] @diff_to_compare = DEFAULT_DIFF_TO_COMPARE @expects_to_allow_nil = false @allowed_type_adjective = nil @allowed_type_name = 'number' add_disallow_non_numeric_value_matcher end
Public Instance Methods
allow_nil()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 384 def allow_nil @expects_to_allow_nil = true prepare_submatcher(allow_value_matcher(nil, :not_a_number)) self end
does_not_match?(subject)
click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 450 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 401 def even prepare_submatcher( NumericalityMatchers::EvenNumberMatcher.new(self, attribute), ) self end
expects_to_allow_nil?()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 390 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 470 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 477 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 485 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 418 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 408 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 413 def is_greater_than_or_equal_to(value) prepare_submatcher(comparison_matcher_for(value, :>=).for(attribute)) self end
is_in(range)
click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 438 def is_in(range) prepare_submatcher( NumericalityMatchers::RangeMatcher.new(self, attribute, range), ) self end
is_less_than(value)
click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 423 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 428 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 433 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 445 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 394 def odd prepare_submatcher( NumericalityMatchers::OddNumberMatcher.new(self, attribute), ) self end
only_integer()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 377 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 455 def simple_description String.new.tap do |description| description << "validate that :#{attribute} looks like " description << Shoulda::Matchers::Util.a_or_an(full_allowed_type) if range_description.present? description << " #{range_description}" end if comparison_descriptions.present? description << " #{comparison_descriptions}" end end end
Private Instance Methods
add_disallow_non_numeric_value_matcher()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 515 def add_disallow_non_numeric_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 535 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 499 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 619 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..] 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 503 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 507 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 644 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 529 def comparison_matcher_for(value, operator) 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 605 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 612 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 591 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 598 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 640 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 582 def has_been_qualified? @submatchers.any? { |submatcher| submatcher_qualified?(submatcher) } end
matches_or_does_not_match?(subject)
click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 492 def matches_or_does_not_match?(subject) @subject = subject @number_of_submatchers = @submatchers.size qualify_submatchers end
non_numeric_value()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 670 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 574 def number_of_submatchers_for_failure_message if has_been_qualified? @submatchers.size - 1 else @submatchers.size end end
prepare_submatcher(submatcher)
click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 524 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 554 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
range_description()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 662 def range_description range_submatcher = @submatchers.detect do |submatcher| submatcher.respond_to? :range_description end range_submatcher&.range_description end
submatcher_comparison_descriptions()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 653 def submatcher_comparison_descriptions @submatchers.inject([]) do |arr, submatcher| if submatcher.respond_to? :comparison_description arr << submatcher.comparison_description end arr end end
submatcher_qualified?(submatcher)
click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 586 def submatcher_qualified?(submatcher) Shoulda::Matchers::RailsShim.parent_of(submatcher.class) == NumericalityMatchers || submatcher.instance_of?(ComparisonMatcher) end