Class: Shoulda::Matchers::ActiveModel::ValidateComparisonOfMatcher

Inherits:
ValidationMatcher
  • Object
show all
Defined in:
lib/shoulda/matchers/active_model/validate_comparison_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) ⇒ ValidateComparisonOfMatcher

Returns a new instance of ValidateComparisonOfMatcher.



311
312
313
314
315
316
317
# File 'lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb', line 311

def initialize(attribute)
  super
  @submatchers = []
  @diff_to_compare = DEFAULT_DIFF_TO_COMPARE
  @expects_to_allow_nil = false
  @comparison_submatcher = false
end

Instance Attribute Details

#diff_to_compareObject (readonly)

Returns the value of attribute diff_to_compare.



309
310
311
# File 'lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb', line 309

def diff_to_compare
  @diff_to_compare
end

#number_of_submatchersObject (readonly)

Returns the value of attribute number_of_submatchers.



309
310
311
# File 'lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb', line 309

def number_of_submatchers
  @number_of_submatchers
end

Instance Method Details

#allow_nilObject



319
320
321
322
323
# File 'lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb', line 319

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

#does_not_match?(subject) ⇒ Boolean

Returns:

  • (Boolean)


370
371
372
373
374
375
376
# File 'lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb', line 370

def does_not_match?(subject)
  @subject = subject
  @number_of_submatchers = @submatchers.size

  qualify_submatchers
  first_submatcher_that_fails_to_not_match.nil?
end

#expects_to_allow_nil?Boolean

Returns:

  • (Boolean)


325
326
327
# File 'lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb', line 325

def expects_to_allow_nil?
  @expects_to_allow_nil
end

#failure_messageObject



389
390
391
392
393
394
# File 'lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb', line 389

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



396
397
398
399
400
401
402
# File 'lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb', line 396

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



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

def failure_reason
  raw_submatcher_failure_reason_for(
    first_submatcher_that_fails_to_match,
    :failure_message,
  )
end

#given_numeric_column?Boolean

Returns:

  • (Boolean)


411
412
413
414
# File 'lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb', line 411

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

#is_equal_to(value) ⇒ Object



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

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

#is_greater_than(value) ⇒ Object



329
330
331
332
# File 'lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb', line 329

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

#is_greater_than_or_equal_to(value) ⇒ Object



334
335
336
337
# File 'lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb', line 334

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

#is_less_than(value) ⇒ Object



344
345
346
347
# File 'lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb', line 344

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

#is_less_than_or_equal_to(value) ⇒ Object



349
350
351
352
# File 'lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb', line 349

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

#is_other_than(value) ⇒ Object



354
355
356
357
# File 'lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb', line 354

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

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


359
360
361
362
363
364
365
366
367
368
# File 'lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb', line 359

def matches?(subject)
  @subject = subject
  @number_of_submatchers = @submatchers.size
  unless @comparison_matcher
    raise(ArgumentError, "matcher isn't qualified with any comparison matcher")
  end

  qualify_submatchers
  first_submatcher_that_fails_to_match.nil?
end

#simple_descriptionObject



378
379
380
381
382
383
384
385
386
387
# File 'lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb', line 378

def simple_description
  String.new.tap do |description|
    description << "validate that :#{attribute} looks like "
    description << Shoulda::Matchers::Util.a_or_an(allowed_type_name)

    if comparison_descriptions.present?
      description << " #{comparison_descriptions}"
    end
  end
end