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.



271
272
273
274
275
276
# File 'lib/shoulda/matchers/active_model/validate_length_of_matcher.rb', line 271

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

Instance Method Details

#allow_nilObject



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

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

#does_not_match?(subject) ⇒ Boolean

Returns:

  • (Boolean)


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

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



278
279
280
281
282
# File 'lib/shoulda/matchers/active_model/validate_length_of_matcher.rb', line 278

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

#is_at_most(length) ⇒ Object



284
285
286
287
288
# File 'lib/shoulda/matchers/active_model/validate_length_of_matcher.rb', line 284

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

#is_equal_to(length) ⇒ Object



290
291
292
293
294
295
296
# File 'lib/shoulda/matchers/active_model/validate_length_of_matcher.rb', line 290

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)


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

def matches?(subject)
  super(subject)

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

#simple_descriptionObject



331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/shoulda/matchers/active_model/validate_length_of_matcher.rb', line 331

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



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

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

  self
end

#with_message(message) ⇒ Object



298
299
300
301
302
303
304
305
306
# File 'lib/shoulda/matchers/active_model/validate_length_of_matcher.rb', line 298

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



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

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

  self
end