class Shoulda::Matchers::ActiveModel::ValidateLengthOfMatcher

@private

Public Class Methods

new(attribute) click to toggle source
Calls superclass method
# 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

Public Instance Methods

allow_nil() click to toggle source
# File lib/shoulda/matchers/active_model/validate_length_of_matcher.rb, line 357
def allow_nil
  @options[:allow_nil] = true
  self
end
as_array() click to toggle source
# 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) click to toggle source
Calls superclass method
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
Calls superclass method
# 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_description() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

allow_nil_does_not_match?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_length_of_matcher.rb, line 480
def allow_nil_does_not_match?
  expects_to_allow_nil? && disallows_value_of(nil)
end
allow_nil_matches?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_length_of_matcher.rb, line 476
def allow_nil_matches?
  !expects_to_allow_nil? || allows_value_of(nil)
end
allows_higher_length?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_length_of_matcher.rb, line 440
def allows_higher_length?
  @options.key?(:maximum) &&
    allows_length_of?(
      @options[:maximum] + 1,
      translated_long_message,
    )
end
allows_length_of?(length, message) click to toggle source
# File lib/shoulda/matchers/active_model/validate_length_of_matcher.rb, line 484
def allows_length_of?(length, message)
  allows_value_of(value_of_length(length), message)
end
allows_lower_length?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_length_of_matcher.rb, line 421
def allows_lower_length?
  @options.key?(:minimum) &&
    @options[:minimum] > 0 &&
    allows_length_of?(
      @options[:minimum] - 1,
      translated_short_message,
    )
end
allows_maximum_length?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_length_of_matcher.rb, line 466
def allows_maximum_length?
  !@options.key?(:maximum) ||
    allows_length_of?(@options[:maximum], translated_long_message)
end
allows_minimum_length?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_length_of_matcher.rb, line 456
def allows_minimum_length?
  !@options.key?(:minimum) ||
    allows_length_of?(@options[:minimum], translated_short_message)
end
array_column?() click to toggle source
Calls superclass method
# File lib/shoulda/matchers/active_model/validate_length_of_matcher.rb, line 502
def array_column?
  @options[:array] || super
end
association?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_length_of_matcher.rb, line 512
def association?
  association_reflection.present?
end
association_reflection() click to toggle source
# File lib/shoulda/matchers/active_model/validate_length_of_matcher.rb, line 516
def association_reflection
  model.try(:reflect_on_association, @attribute)
end
collection_association?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_length_of_matcher.rb, line 506
def collection_association?
  association? && [:has_many, :has_and_belongs_to_many].include?(
    association_reflection.macro,
  )
end
disallows_higher_length?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_length_of_matcher.rb, line 448
def disallows_higher_length?
  !@options.key?(:maximum) ||
    disallows_length_of?(
      @options[:maximum] + 1,
      translated_long_message,
    )
end
disallows_length_of?(length, message) click to toggle source
# File lib/shoulda/matchers/active_model/validate_length_of_matcher.rb, line 488
def disallows_length_of?(length, message)
  disallows_value_of(value_of_length(length), message)
end
disallows_lower_length?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_length_of_matcher.rb, line 430
def disallows_lower_length?
  !@options.key?(:minimum) ||
    @options[:minimum] == 0 ||
    (@options[:minimum] == 1 && expects_to_allow_blank?) ||
    disallows_length_of?(
      @options[:minimum] - 1,
      translated_short_message,
    )
end
disallows_maximum_length?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_length_of_matcher.rb, line 471
def disallows_maximum_length?
  @options.key?(:maximum) &&
    disallows_length_of?(@options[:maximum], translated_long_message)
end
disallows_minimum_length?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_length_of_matcher.rb, line 461
def disallows_minimum_length?
  @options.key?(:minimum) &&
    disallows_length_of?(@options[:minimum], translated_short_message)
end
expects_to_allow_nil?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_length_of_matcher.rb, line 401
def expects_to_allow_nil?
  @options[:allow_nil]
end
lower_bound_does_not_match?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_length_of_matcher.rb, line 409
def lower_bound_does_not_match?
  allows_lower_length? || disallows_minimum_length?
end
lower_bound_matches?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_length_of_matcher.rb, line 405
def lower_bound_matches?
  disallows_lower_length? && allows_minimum_length?
end
translated_long_message() click to toggle source
# File lib/shoulda/matchers/active_model/validate_length_of_matcher.rb, line 535
def translated_long_message
  @_translated_long_message ||=
    if @long_message.is_a?(Symbol)
      default_error_message(
        @long_message,
        model_name: @subject.class.to_s.underscore,
        instance: @subject,
        attribute: @attribute,
        count: @options[:maximum],
      )
    else
      @long_message
    end
end
translated_short_message() click to toggle source
# File lib/shoulda/matchers/active_model/validate_length_of_matcher.rb, line 520
def translated_short_message
  @_translated_short_message ||=
    if @short_message.is_a?(Symbol)
      default_error_message(
        @short_message,
        model_name: @subject.class.to_s.underscore,
        instance: @subject,
        attribute: @attribute,
        count: @options[:minimum],
      )
    else
      @short_message
    end
end
upper_bound_does_not_match?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_length_of_matcher.rb, line 417
def upper_bound_does_not_match?
  allows_higher_length? || disallows_maximum_length?
end
upper_bound_matches?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_length_of_matcher.rb, line 413
def upper_bound_matches?
  disallows_higher_length? && allows_maximum_length?
end
value_of_length(length) click to toggle source
# File lib/shoulda/matchers/active_model/validate_length_of_matcher.rb, line 492
def value_of_length(length)
  if array_column?
    ['x'] * length
  elsif collection_association?
    Array.new(length) { association_reflection.klass.new }
  else
    'x' * length
  end
end