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 214
def initialize(attribute)
  super(attribute)
  @options = {}
  @short_message = nil
  @long_message = nil
end

Public Instance Methods

description() click to toggle source
# File lib/shoulda/matchers/active_model/validate_length_of_matcher.rb, line 263
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
is_at_least(length) click to toggle source
# File lib/shoulda/matchers/active_model/validate_length_of_matcher.rb, line 221
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 227
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 233
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 278
def matches?(subject)
  super(subject)
  translate_messages!
  lower_bound_matches? && upper_bound_matches?
end
with_long_message(message) click to toggle source
# File lib/shoulda/matchers/active_model/validate_length_of_matcher.rb, line 248
def with_long_message(message)
  if message
    @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 255
def with_message(message)
  if message
    @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 241
def with_short_message(message)
  if message
    @short_message = message
  end
  self
end

Private Instance Methods

allows_length_of?(length, message) click to toggle source
# File lib/shoulda/matchers/active_model/validate_length_of_matcher.rb, line 345
def allows_length_of?(length, message)
  allows_value_of(string_of_length(length), message)
end
allows_maximum_length?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_length_of_matcher.rb, line 337
def allows_maximum_length?
  if @options.key?(:maximum)
    allows_length_of?(@options[:maximum], @long_message)
  else
    true
  end
end
allows_minimum_length?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_length_of_matcher.rb, line 329
def allows_minimum_length?
  if @options.key?(:minimum)
    allows_length_of?(@options[:minimum], @short_message)
  else
    true
  end
end
disallows_higher_length?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_length_of_matcher.rb, line 321
def disallows_higher_length?
  if @options.key?(:maximum)
    disallows_length_of?(@options[:maximum] + 1, @long_message)
  else
    true
  end
end
disallows_length_of?(length, message) click to toggle source
# File lib/shoulda/matchers/active_model/validate_length_of_matcher.rb, line 349
def disallows_length_of?(length, message)
  disallows_value_of(string_of_length(length), message)
end
disallows_lower_length?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_length_of_matcher.rb, line 312
def disallows_lower_length?
  if @options.key?(:minimum)
    @options[:minimum] == 0 ||
      disallows_length_of?(@options[:minimum] - 1, @short_message)
  else
    true
  end
end
lower_bound_matches?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_length_of_matcher.rb, line 304
def lower_bound_matches?
  disallows_lower_length? && allows_minimum_length?
end
string_of_length(length) click to toggle source
# File lib/shoulda/matchers/active_model/validate_length_of_matcher.rb, line 353
def string_of_length(length)
  'x' * length
end
translate_messages!() click to toggle source
# File lib/shoulda/matchers/active_model/validate_length_of_matcher.rb, line 286
def translate_messages!
  if Symbol === @short_message
    @short_message = default_error_message(@short_message,
                                           model_name: @subject.class.to_s.underscore,
                                           instance: @subject,
                                           attribute: @attribute,
                                           count: @options[:minimum])
  end

  if Symbol === @long_message
    @long_message = default_error_message(@long_message,
                                          model_name: @subject.class.to_s.underscore,
                                          instance: @subject,
                                          attribute: @attribute,
                                          count: @options[:maximum])
  end
end
upper_bound_matches?() click to toggle source
# File lib/shoulda/matchers/active_model/validate_length_of_matcher.rb, line 308
def upper_bound_matches?
  disallows_higher_length? && allows_maximum_length?
end