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, #on, #strict

Methods included from Qualifiers::IgnoringInterferenceByWriter

#ignoring_interference_by_writer

Constructor Details

#initialize(attribute) ⇒ ValidateLengthOfMatcher

Returns a new instance of ValidateLengthOfMatcher.



297
298
299
300
301
302
# File 'lib/shoulda/matchers/active_model/validate_length_of_matcher.rb', line 297

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

Instance Method Details

#allow_nilObject



357
358
359
360
# File 'lib/shoulda/matchers/active_model/validate_length_of_matcher.rb', line 357

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

#as_arrayObject



304
305
306
307
# File 'lib/shoulda/matchers/active_model/validate_length_of_matcher.rb', line 304

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

#does_not_match?(subject) ⇒ Boolean

Returns:

  • (Boolean)


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

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



309
310
311
312
313
# File 'lib/shoulda/matchers/active_model/validate_length_of_matcher.rb', line 309

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

#is_at_most(length) ⇒ Object



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

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

#is_equal_to(length) ⇒ Object



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

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)


381
382
383
384
385
386
387
388
# File 'lib/shoulda/matchers/active_model/validate_length_of_matcher.rb', line 381

def matches?(subject)
  super(subject)

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

#simple_descriptionObject



362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/shoulda/matchers/active_model/validate_length_of_matcher.rb', line 362

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



348
349
350
351
352
353
354
355
# File 'lib/shoulda/matchers/active_model/validate_length_of_matcher.rb', line 348

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

  self
end

#with_message(message) ⇒ Object



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

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



339
340
341
342
343
344
345
346
# File 'lib/shoulda/matchers/active_model/validate_length_of_matcher.rb', line 339

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

  self
end