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.



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

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_nilObject



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

Returns:

  • (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

#evenObject



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

Returns:

  • (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_messageObject



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_negatedObject



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

Returns:

  • (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

Returns:

  • (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

#oddObject



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_integerObject



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_descriptionObject



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