Class Index [+]

Quicksearch

ActiveModel::Validations::LengthValidator

Constants

MESSAGES
CHECKS
DEFAULT_TOKENIZER
RESERVED_OPTIONS

Public Class Methods

new(options) click to toggle source
    # File lib/active_model/validations/length.rb, line 12
12:       def initialize(options)
13:         if range = (options.delete(:in) || options.delete(:within))
14:           raise ArgumentError, ":in and :within must be a Range" unless range.is_a?(Range)
15:           options[:minimum], options[:maximum] = range.begin, range.end
16:           options[:maximum] -= 1 if range.exclude_end?
17:         end
18: 
19:         super(options.reverse_merge(:tokenizer => DEFAULT_TOKENIZER))
20:       end

Public Instance Methods

check_validity!() click to toggle source
    # File lib/active_model/validations/length.rb, line 22
22:       def check_validity!
23:         keys = CHECKS.keys & options.keys
24: 
25:         if keys.empty?
26:           raise ArgumentError, 'Range unspecified. Specify the :within, :maximum, :minimum, or :is option.'
27:         end
28: 
29:         keys.each do |key|
30:           value = options[key]
31: 
32:           unless value.is_a?(Integer) && value >= 0
33:             raise ArgumentError, ":#{key} must be a nonnegative Integer"
34:           end
35:         end
36:       end
validate_each(record, attribute, value) click to toggle source
    # File lib/active_model/validations/length.rb, line 38
38:       def validate_each(record, attribute, value)
39:         value = options[:tokenizer].call(value) if value.kind_of?(String)
40: 
41:         CHECKS.each do |key, validity_check|
42:           next unless check_value = options[key]
43: 
44:           value ||= [] if key == :maximum
45: 
46:           next if value && value.size.send(validity_check, check_value)
47: 
48:           errors_options = options.except(*RESERVED_OPTIONS)
49:           errors_options[:count] = check_value
50: 
51:           default_message = options[MESSAGES[key]]
52:           errors_options[:message] ||= default_message if default_message
53: 
54:           record.errors.add(attribute, MESSAGES[key], errors_options)
55:         end
56:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.