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

Public Instance Methods

is_at_least(length) click to toggle source
# File lib/shoulda/matchers/active_model/validate_length_of_matcher.rb, line 236
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 242
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 248
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 303
def matches?(subject)
  super(subject)
  translate_messages!
  lower_bound_matches? && upper_bound_matches?
end
simple_description() click to toggle source
# File lib/shoulda/matchers/active_model/validate_length_of_matcher.rb, line 284
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 275
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 256
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 266
def with_short_message(message)
  if message
    @expects_custom_validation_message = true
    @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 370
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 362
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 354
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 346
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 374
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 337
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 329
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 378
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 311
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 333
def upper_bound_matches?
  disallows_higher_length? && allows_maximum_length?
end