Class: Shoulda::Matchers::ActiveModel::ValidateLengthOfMatcher

Inherits:
ValidationMatcher show all
Includes:
Helpers
Defined in:
lib/shoulda/matchers/active_model/validate_length_of_matcher.rb

Instance Attribute Summary

Attributes included from Qualifiers::IgnoringInterferenceByWriter

#ignore_interference_by_writer

Instance Method Summary collapse

Methods included from Helpers

#default_error_message, #format_validation_errors, #pretty_error_messages

Methods inherited from ValidationMatcher

#allow_blank, #description, #expects_custom_validation_message?, #expects_strict?, #failure_message, #failure_message_when_negated, #failure_reason, #on, #strict

Methods included from Qualifiers::IgnoringInterferenceByWriter

#ignoring_interference_by_writer

Constructor Details

#initialize(attribute) ⇒ ValidateLengthOfMatcher

Returns a new instance of ValidateLengthOfMatcher.



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

def initialize(attribute)
  super(attribute)
  @options = {}
  @short_message = nil
  @long_message = nil
end

Instance Method Details

#allow_nilObject



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

def allow_nil
  @options[:allow_nil] = true
  self
end

#as_arrayObject



327
328
329
330
# File 'lib/shoulda/matchers/active_model/validate_length_of_matcher.rb', line 327

def as_array
  @options[:array] = true
  self
end

#does_not_match?(subject) ⇒ Boolean

Returns:

  • (Boolean)


413
414
415
416
417
418
419
420
# File 'lib/shoulda/matchers/active_model/validate_length_of_matcher.rb', line 413

def does_not_match?(subject)
  super(subject)

  lower_bound_does_not_match? ||
    upper_bound_does_not_match? ||
    allow_nil_does_not_match? ||
    allow_blank_does_not_match?
end

#is_at_least(length) ⇒ Object



332
333
334
335
336
# File 'lib/shoulda/matchers/active_model/validate_length_of_matcher.rb', line 332

def is_at_least(length)
  @options[:minimum] = length
  @short_message ||= :too_short
  self
end

#is_at_most(length) ⇒ Object



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

def is_at_most(length)
  @options[:maximum] = length
  @long_message ||= :too_long
  self
end

#is_equal_to(length) ⇒ Object



344
345
346
347
348
349
350
# File 'lib/shoulda/matchers/active_model/validate_length_of_matcher.rb', line 344

def is_equal_to(length)
  @options[:minimum] = length
  @options[:maximum] = length
  @short_message ||= :wrong_length
  @long_message ||= :wrong_length
  self
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


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

def matches?(subject)
  super(subject)

  lower_bound_matches? &&
    upper_bound_matches? &&
    allow_nil_matches? &&
    allow_blank_matches?
end

#simple_descriptionObject



385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/shoulda/matchers/active_model/validate_length_of_matcher.rb', line 385

def simple_description
  description = "validate that the length of :#{@attribute}"

  if @options.key?(:minimum) && @options.key?(:maximum)
    if @options[:minimum] == @options[:maximum]
      description << " is #{@options[:minimum]}"
    else
      description << " is between #{@options[:minimum]}"
      description << " and #{@options[:maximum]}"
    end
  elsif @options.key?(:minimum)
    description << " is at least #{@options[:minimum]}"
  elsif @options.key?(:maximum)
    description << " is at most #{@options[:maximum]}"
  end

  description
end

#with_long_message(message) ⇒ Object



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

def with_long_message(message)
  if message
    @expects_custom_validation_message = true
    @long_message = message
  end

  self
end

#with_message(message) ⇒ Object



352
353
354
355
356
357
358
359
360
# File 'lib/shoulda/matchers/active_model/validate_length_of_matcher.rb', line 352

def with_message(message)
  if message
    @expects_custom_validation_message = true
    @short_message = message
    @long_message = message
  end

  self
end

#with_short_message(message) ⇒ Object



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

def with_short_message(message)
  if message
    @expects_custom_validation_message = true
    @short_message = message
  end

  self
end