Class: Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher
Constant Summary
collapse
- NUMERIC_NAME =
'number'.freeze
- DEFAULT_DIFF_TO_COMPARE =
1
Instance Attribute Summary collapse
#ignore_interference_by_writer
Instance Method Summary
collapse
#allow_blank, #description, #expects_custom_validation_message?, #expects_strict?, #on, #strict, #with_message
#ignoring_interference_by_writer
Constructor Details
Returns a new instance of ValidateNumericalityOfMatcher.
366
367
368
369
370
371
372
373
374
375
|
# 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
|
Instance Attribute Details
#diff_to_compare ⇒ Object
Returns the value of attribute diff_to_compare.
364
365
366
|
# File 'lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb', line 364
def diff_to_compare
@diff_to_compare
end
|
Instance Method Details
#allow_nil ⇒ Object
384
385
386
387
388
|
# 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) ⇒ Boolean
450
451
452
453
|
# 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 ⇒ Object
401
402
403
404
405
406
|
# 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? ⇒ Boolean
390
391
392
|
# 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 ⇒ Object
470
471
472
473
474
475
|
# 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 ⇒ Object
477
478
479
480
481
482
483
|
# 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? ⇒ Boolean
485
486
487
488
|
# 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) ⇒ Object
418
419
420
421
|
# 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) ⇒ Object
408
409
410
411
|
# 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) ⇒ Object
413
414
415
416
|
# 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) ⇒ Object
438
439
440
441
442
443
|
# 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) ⇒ Object
423
424
425
426
|
# 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) ⇒ Object
428
429
430
431
|
# 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) ⇒ Object
433
434
435
436
|
# 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) ⇒ Boolean
445
446
447
448
|
# 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 ⇒ Object
394
395
396
397
398
399
|
# 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 ⇒ Object
377
378
379
380
381
382
|
# 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 ⇒ Object
455
456
457
458
459
460
461
462
463
464
465
466
467
468
|
# 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
|