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

Inherits:
Object
  • Object
show all
Includes:
Qualifiers::IgnoringInterferenceByWriter
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 included from Qualifiers::IgnoringInterferenceByWriter

#ignoring_interference_by_writer

Constructor Details

#initialize(attribute) ⇒ ValidateNumericalityOfMatcher

Returns a new instance of ValidateNumericalityOfMatcher.



341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb', line 341

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

Instance Attribute Details

#diff_to_compareObject (readonly)

Returns the value of attribute diff_to_compare.



339
340
341
# File 'lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb', line 339

def diff_to_compare
  @diff_to_compare
end

Instance Method Details

#allow_nilObject



371
372
373
374
375
376
377
378
379
# File 'lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb', line 371

def allow_nil
  @expects_to_allow_nil = true
  prepare_submatcher(
    AllowValueMatcher.new(nil).
      for(@attribute).
      with_message(:not_a_number),
  )
  self
end

#descriptionObject



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

def description
  ValidationMatcher::BuildDescription.call(self, simple_description)
end

#does_not_match?(subject) ⇒ Boolean

Returns:

  • (Boolean)


449
450
451
452
# File 'lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb', line 449

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

#evenObject



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

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

#expects_custom_validation_message?Boolean

Returns:

  • (Boolean)


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

def expects_custom_validation_message?
  @expects_custom_validation_message
end

#expects_strict?Boolean

Returns:

  • (Boolean)


360
361
362
# File 'lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb', line 360

def expects_strict?
  @expects_strict
end

#expects_to_allow_nil?Boolean

Returns:

  • (Boolean)


381
382
383
# File 'lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb', line 381

def expects_to_allow_nil?
  @expects_to_allow_nil
end

#failure_messageObject



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

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



478
479
480
481
482
483
484
# File 'lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb', line 478

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)


486
487
488
489
# File 'lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb', line 486

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

#is_equal_to(value) ⇒ Object



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

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

#is_greater_than(value) ⇒ Object



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

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

#is_greater_than_or_equal_to(value) ⇒ Object



404
405
406
407
# File 'lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb', line 404

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

#is_less_than(value) ⇒ Object



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

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

#is_less_than_or_equal_to(value) ⇒ Object



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

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

#is_other_than(value) ⇒ Object



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

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

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


444
445
446
447
# File 'lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb', line 444

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

#oddObject



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

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

#on(context) ⇒ Object



439
440
441
442
# File 'lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb', line 439

def on(context)
  @context = context
  self
end

#only_integerObject



364
365
366
367
368
369
# File 'lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb', line 364

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

#simple_descriptionObject



454
455
456
457
458
459
460
461
462
463
464
465
# File 'lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb', line 454

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

#strictObject



355
356
357
358
# File 'lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb', line 355

def strict
  @expects_strict = true
  self
end

#with_message(message) ⇒ Object



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

def with_message(message)
  @expects_custom_validation_message = true
  @expected_message = message
  self
end