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
Calls superclass method
Shoulda::Matchers::ActiveModel::Qualifiers::IgnoringInterferenceByWriter::new
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 315 def initialize(attribute) super @attribute = attribute @submatchers = [] @diff_to_compare = DEFAULT_DIFF_TO_COMPARE @expects_custom_validation_message = false @expects_to_allow_nil = false @expects_strict = false @allowed_type_adjective = nil @allowed_type_name = 'number' @context = nil @expected_message = nil end
Public Instance Methods
allow_nil()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 345 def allow_nil @expects_to_allow_nil = true 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 436 def description ValidationMatcher::BuildDescription.call(self, simple_description) end
even()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 366 def even prepare_submatcher( NumericalityMatchers::EvenNumberMatcher.new(self, @attribute) ) self end
expects_custom_validation_message?()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 404 def expects_custom_validation_message? @expects_custom_validation_message end
expects_strict?()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 334 def expects_strict? @expects_strict end
expects_to_allow_nil?()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 355 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 440 def failure_message overall_failure_message.dup.tap do |message| message << "\n" message << failure_message_for_first_failing_submatcher end end
failure_message_when_negated()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 447 def failure_message_when_negated overall_failure_message_when_negated.dup.tap do |message| if submatcher_failure_message_when_negated.present? raise "hmm, this needs to be implemented." message << "\n" message << Shoulda::Matchers.word_wrap( submatcher_failure_message_when_negated, indent: 2 ) end end end
given_numeric_column?()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 460 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 383 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 373 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 378 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 388 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 393 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 413 def matches?(subject) @subject = subject @number_of_submatchers = @submatchers.size add_disallow_value_matcher qualify_submatchers first_failing_submatcher.nil? end
odd()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 359 def odd prepare_submatcher( NumericalityMatchers::OddNumberMatcher.new(self, @attribute) ) self end
on(context)
click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 408 def on(context) @context = context self end
only_integer()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 338 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 423 def simple_description description = '' description << "validate that :#{@attribute} looks like " description << Shoulda::Matchers::Util.a_or_an(full_allowed_type) if comparison_descriptions.present? description << ' ' + comparison_descriptions end description end
strict()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 329 def strict @expects_strict = true self end
with_message(message)
click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 398 def with_message(message) @expects_custom_validation_message = true @expected_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 499 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 519 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 483 def attribute_is_active_record_column? columns_hash.key?(@attribute.to_s) end
column_type()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 487 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 491 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 606 def comparison_descriptions description_array = submatcher_comparison_descriptions description_array.empty? ? '' : 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 513 def comparison_matcher_for(value, operator) NumericalityMatchers::ComparisonMatcher. new(self, value, operator). for(@attribute) end
failure_message_for_first_failing_submatcher()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 583 def failure_message_for_first_failing_submatcher submatcher = first_failing_submatcher if number_of_submatchers_for_failure_message > 1 submatcher_description = submatcher.simple_description. sub(/\bvalidate that\b/, 'validates'). sub(/\bdisallow\b/, 'disallows'). sub(/\ballow\b/, 'allows') submatcher_message = "In checking that #{model.name} #{submatcher_description}, " + submatcher.failure_message[0].downcase + submatcher.failure_message[1..-1] else submatcher_message = submatcher.failure_message end Shoulda::Matchers.word_wrap(submatcher_message, indent: 2) end
first_failing_submatcher()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 569 def first_failing_submatcher @_failing_submatchers ||= @submatchers.detect do |submatcher| !submatcher.matches?(@subject) end end
full_allowed_type()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 602 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 563 def has_been_qualified? @submatchers.any? do |submatcher| submatcher.class.parent == NumericalityMatchers end end
model()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 467 def model @subject.class end
number_of_submatchers_for_failure_message()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 555 def number_of_submatchers_for_failure_message if has_been_qualified? @submatchers.size - 1 else @submatchers.size end end
overall_failure_message()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 471 def overall_failure_message Shoulda::Matchers.word_wrap( "#{model.name} did not properly #{description}." ) end
overall_failure_message_when_negated()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 477 def overall_failure_message_when_negated Shoulda::Matchers.word_wrap( "Expected #{model.name} not to #{description}, but it did." ) end
prepare_submatcher(submatcher)
click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 508 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 535 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
submatcher_comparison_descriptions()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 611 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_message()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 575 def submatcher_failure_message first_failing_submatcher.failure_message end
submatcher_failure_message_when_negated()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb, line 579 def submatcher_failure_message_when_negated first_failing_submatcher.failure_message_when_negated end