def validates_length_of(*attrs)
options = {:too_long => ActiveRecord::Errors.default_error_messages[:too_long],
:too_short => ActiveRecord::Errors.default_error_messages[:too_short],
:wrong_length => ActiveRecord::Errors.default_error_messages[:wrong_length]}.merge(DEFAULT_VALIDATION_OPTIONS)
options.update(attrs.pop.symbolize_keys) if attrs.last.is_a?(Hash)
range_options = ALL_RANGE_OPTIONS & options.keys
case range_options.size
when 0
raise ArgumentError, 'Range unspecified. Specify the :within, :maximum, :minimum, or :is option.'
when 1
else
raise ArgumentError, 'Too many range options specified. Choose only one.'
end
option = range_options.first
option_value = options[range_options.first]
validity_checks = { :is => "==", :minimum => ">=", :maximum => "<=" }
message_options = { :is => :wrong_length, :minimum => :too_short, :maximum => :too_long }
case option
when :within, :in
raise ArgumentError, ':within must be a Range' unless option_value.is_a?(Range)
(options_without_range = options.dup).delete(option)
(options_with_minimum = options_without_range.dup).store(:minimum, option_value.begin)
validates_length_of attrs, options_with_minimum
(options_with_maximum = options_without_range.dup).store(:maximum, option_value.end)
validates_length_of attrs, options_with_maximum
when :is, :minimum, :maximum
raise ArgumentError, ":#{option} must be a nonnegative Integer" unless option_value.is_a?(Integer) and option_value >= 0
message = options[:message] || options[message_options[option]]
message = (message % option_value) rescue message
validates_each(attrs, options) do |record, attr, value|
record.errors.add(attr, message) if value.nil? or !value.size.method(validity_checks[option])[option_value]
end
end
end