class Shoulda::Matchers::ActiveModel::EnsureLengthOfMatcher

:nodoc:

def allows_length_of?(length, message)

def allows_length_of?(length, message)
  allows_value_of(string_of_length(length), message)
end

def allows_maximum_length?

def allows_maximum_length?
  if @options.key?(:maximum)
    allows_length_of?(@options[:maximum], @long_message)
  else
    true
  end
end

def allows_minimum_length?

def allows_minimum_length?
  if @options.key?(:minimum)
    allows_length_of?(@options[:minimum], @short_message)
  else
    true
  end
end

def description

def description
  description =  "ensure #{@attribute} has a length "
  if @options.key?(:minimum) && @options.key?(:maximum)
    if @options[:minimum] == @options[:maximum]
      description << "of exactly #{@options[:minimum]}"
    else
      description << "between #{@options[:minimum]} and #{@options[:maximum]}"
    end
  else
    description << "of at least #{@options[:minimum]}" if @options[:minimum]
    description << "of at most #{@options[:maximum]}" if @options[:maximum]
  end
  description
end

def disallows_higher_length?

def disallows_higher_length?
  if @options.key?(:maximum)
    disallows_length_of?(@options[:maximum] + 1, @long_message)
  else
    true
  end
end

def disallows_length_of?(length, message)

def disallows_length_of?(length, message)
  disallows_value_of(string_of_length(length), message)
end

def disallows_lower_length?

def disallows_lower_length?
  if @options.key?(:minimum)
    @options[:minimum] == 0 ||
      disallows_length_of?(@options[:minimum] - 1, @short_message)
  else
    true
  end
end

def initialize(attribute)

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

def is_at_least(length)

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

def is_at_most(length)

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

def is_equal_to(length)

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

def lower_bound_matches?

def lower_bound_matches?
  disallows_lower_length? && allows_minimum_length?
end

def matches?(subject)

def matches?(subject)
  super(subject)
  translate_messages!
  lower_bound_matches? && upper_bound_matches?
end

def string_of_length(length)

def string_of_length(length)
  'x' * length
end

def translate_messages!

def translate_messages!
  if Symbol === @short_message
    @short_message = default_error_message(@short_message,
                                           :model_name => @subject.class.to_s.underscore,
                                           :attribute => @attribute,
                                           :count => @options[:minimum])
  end
  if Symbol === @long_message
    @long_message = default_error_message(@long_message,
                                          :model_name => @subject.class.to_s.underscore,
                                          :attribute => @attribute,
                                          :count => @options[:maximum])
  end
end

def upper_bound_matches?

def upper_bound_matches?
  disallows_higher_length? && allows_maximum_length?
end

def with_long_message(message)

def with_long_message(message)
  if message
    @long_message = message
  end
  self
end

def with_short_message(message)

def with_short_message(message)
  if message
    @short_message = message
  end
  self
end