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.



288
289
290
291
292
293
294
# File 'lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb', line 288

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.



286
287
288
# File 'lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb', line 286

def diff_to_compare
  @diff_to_compare
end

#number_of_submatchersObject (readonly)

Returns the value of attribute number_of_submatchers.



286
287
288
# File 'lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb', line 286

def number_of_submatchers
  @number_of_submatchers
end

Instance Method Details

#allow_nilObject



296
297
298
299
300
# File 'lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb', line 296

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

#does_not_match?(subject) ⇒ Boolean

Returns:

  • (Boolean)


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

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)


302
303
304
# File 'lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb', line 302

def expects_to_allow_nil?
  @expects_to_allow_nil
end

#failure_messageObject



366
367
368
369
370
371
# File 'lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb', line 366

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



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

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)


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

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

#is_equal_to(value) ⇒ Object



316
317
318
319
# File 'lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb', line 316

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

#is_greater_than(value) ⇒ Object



306
307
308
309
# File 'lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb', line 306

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

#is_greater_than_or_equal_to(value) ⇒ Object



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

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

#is_less_than(value) ⇒ Object



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

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

#is_less_than_or_equal_to(value) ⇒ Object



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

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

#is_other_than(value) ⇒ Object



331
332
333
334
# File 'lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb', line 331

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

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


336
337
338
339
340
341
342
343
344
345
# File 'lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb', line 336

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



355
356
357
358
359
360
361
362
363
364
# File 'lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb', line 355

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