Class: Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher

Inherits:
ValidationMatcher
  • Object
show all
Defined in:
lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb

Constant Summary collapse

NUMERIC_NAME =
'number'.freeze
DEFAULT_DIFF_TO_COMPARE =
1

Instance Attribute Summary collapse

Attributes included from Qualifiers::IgnoringInterferenceByWriter

#ignore_interference_by_writer

Instance Method Summary collapse

Methods inherited from ValidationMatcher

#allow_blank, #description, #expects_custom_validation_message?, #expects_strict?, #on, #strict, #with_message

Methods included from Qualifiers::IgnoringInterferenceByWriter

#ignoring_interference_by_writer

Constructor Details

#initialize(attribute) ⇒ ValidateNumericalityOfMatcher

Returns a new instance of ValidateNumericalityOfMatcher.



389
390
391
392
393
394
395
396
397
398
# File 'lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb', line 389

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_compareObject (readonly)

Returns the value of attribute diff_to_compare.



387
388
389
# File 'lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb', line 387

def diff_to_compare
  @diff_to_compare
end

Instance Method Details

#allow_nilObject



407
408
409
410
411
# File 'lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb', line 407

def allow_nil
  @expects_to_allow_nil = true
  prepare_submatcher(allow_value_matcher(nil, :not_a_number))
  self
end

#does_not_match?(subject) ⇒ Boolean

Returns:

  • (Boolean)


473
474
475
476
# File 'lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb', line 473

def does_not_match?(subject)
  matches_or_does_not_match?(subject)
  first_submatcher_that_fails_to_not_match.nil?
end

#evenObject



424
425
426
427
428
429
# File 'lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb', line 424

def even
  prepare_submatcher(
    NumericalityMatchers::EvenNumberMatcher.new(self, attribute),
  )
  self
end

#expects_to_allow_nil?Boolean

Returns:

  • (Boolean)


413
414
415
# File 'lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb', line 413

def expects_to_allow_nil?
  @expects_to_allow_nil
end

#failure_messageObject



493
494
495
496
497
498
# File 'lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb', line 493

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_negatedObject



500
501
502
503
504
505
506
# File 'lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb', line 500

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

#failure_reasonObject



508
509
510
511
512
513
# File 'lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb', line 508

def failure_reason
  raw_submatcher_failure_reason_for(
    first_submatcher_that_fails_to_match,
    :failure_message,
  )
end

#given_numeric_column?Boolean

Returns:

  • (Boolean)


515
516
517
518
# File 'lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb', line 515

def given_numeric_column?
  attribute_is_active_record_column? &&
    [:integer, :float, :decimal].include?(column_type)
end

#is_equal_to(value) ⇒ Object



441
442
443
444
# File 'lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb', line 441

def is_equal_to(value)
  prepare_submatcher(comparison_matcher_for(value, :==).for(attribute))
  self
end

#is_greater_than(value) ⇒ Object



431
432
433
434
# File 'lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb', line 431

def is_greater_than(value)
  prepare_submatcher(comparison_matcher_for(value, :>).for(attribute))
  self
end

#is_greater_than_or_equal_to(value) ⇒ Object



436
437
438
439
# File 'lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb', line 436

def is_greater_than_or_equal_to(value)
  prepare_submatcher(comparison_matcher_for(value, :>=).for(attribute))
  self
end

#is_in(range) ⇒ Object



461
462
463
464
465
466
# File 'lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb', line 461

def is_in(range)
  prepare_submatcher(
    NumericalityMatchers::RangeMatcher.new(self, attribute, range),
  )
  self
end

#is_less_than(value) ⇒ Object



446
447
448
449
# File 'lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb', line 446

def is_less_than(value)
  prepare_submatcher(comparison_matcher_for(value, :<).for(attribute))
  self
end

#is_less_than_or_equal_to(value) ⇒ Object



451
452
453
454
# File 'lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb', line 451

def is_less_than_or_equal_to(value)
  prepare_submatcher(comparison_matcher_for(value, :<=).for(attribute))
  self
end

#is_other_than(value) ⇒ Object



456
457
458
459
# File 'lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb', line 456

def is_other_than(value)
  prepare_submatcher(comparison_matcher_for(value, :!=).for(attribute))
  self
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


468
469
470
471
# File 'lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb', line 468

def matches?(subject)
  matches_or_does_not_match?(subject)
  first_submatcher_that_fails_to_match.nil?
end

#oddObject



417
418
419
420
421
422
# File 'lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb', line 417

def odd
  prepare_submatcher(
    NumericalityMatchers::OddNumberMatcher.new(self, attribute),
  )
  self
end

#only_integerObject



400
401
402
403
404
405
# File 'lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb', line 400

def only_integer
  prepare_submatcher(
    NumericalityMatchers::OnlyIntegerMatcher.new(self, attribute),
  )
  self
end

#simple_descriptionObject



478
479
480
481
482
483
484
485
486
487
488
489
490
491
# File 'lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb', line 478

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