Class Index [+]

Quicksearch

DataMapper::Validations::LengthValidator

Public Class Methods

new(field_name, options) click to toggle source

Initialize a length validator

@param [Symbol] field_name

  the name of the field to validate

@param [Hash] options

  the validator options

@api semipublic

    # File lib/dm-validations/validators/length_validator.rb, line 14
14:       def initialize(field_name, options)
15:         super
16: 
17:         @equal = options[:is]      || options[:equals]
18:         @range = options[:within]  || options[:in]
19:         @min   = options[:minimum] || options[:min]
20:         @max   = options[:maximum] || options[:max]
21: 
22:         if @min && @max
23:           @range ||= @min..@max
24:         end
25:       end

Public Instance Methods

call(target) click to toggle source

Test the resource field for validity

@example when the resource field is valid

  validator.call(valid_resource)  # => true

@example when the resource field is not valid

  validator.call(invalid_resource)  # => false

@param [Resource] target

  the Resource to test

@return [Boolean]

  true if the field is valid, false if not

@api semipublic

    # File lib/dm-validations/validators/length_validator.rb, line 43
43:       def call(target)
44:         value = target.validation_property_value(field_name)
45:         return true if optional?(value)
46: 
47:         return true unless error_message = error_message_for(value)
48: 
49:         add_error(target, error_message, field_name)
50:         false
51:       end

Private Instance Methods

error_message_for(value) click to toggle source

Return the error messages for the value if it is invalid

@param [#] value

  the value to test

@return [String, nil]

  the error message if invalid, nil if not

@api private

    # File lib/dm-validations/validators/length_validator.rb, line 64
64:       def error_message_for(value)
65:         if error_message = send(validation_method, value_length(value.to_s))
66:           @options.fetch(:message, error_message)
67:         end
68:       end
validate_equals(length) click to toggle source

Validate the value length is equal to the expected length

@param [Integer] length

  the value length

@return [String, nil]

  the error message if invalid, nil if not

@api private

     # File lib/dm-validations/validators/length_validator.rb, line 113
113:       def validate_equals(length)
114:         return if length == @equal
115: 
116:         ValidationErrors.default_error_message(
117:           :wrong_length,
118:           humanized_field_name,
119:           @equal
120:         )
121:       end
validate_max(length) click to toggle source

Validate the maximum expected value length

@param [Integer] length

  the value length

@return [String, nil]

  the error message if invalid, nil if not

@api private

     # File lib/dm-validations/validators/length_validator.rb, line 171
171:       def validate_max(length)
172:         return if length <= @max
173: 
174:         ValidationErrors.default_error_message(
175:           :too_long,
176:           humanized_field_name,
177:           @max
178:         )
179:       end
validate_min(length) click to toggle source

Validate the minimum expected value length

@param [Integer] length

  the value length

@return [String, nil]

  the error message if invalid, nil if not

@api private

     # File lib/dm-validations/validators/length_validator.rb, line 152
152:       def validate_min(length)
153:         return if length >= @min
154: 
155:         ValidationErrors.default_error_message(
156:           :too_short,
157:           humanized_field_name,
158:           @min
159:         )
160:       end
validate_range(length) click to toggle source

Validate the value length is within expected range

@param [Integer] length

  the value length

@return [String, nil]

  the error message if invalid, nil if not

@api private

     # File lib/dm-validations/validators/length_validator.rb, line 132
132:       def validate_range(length)
133:         return if @range.include?(length)
134: 
135:         ValidationErrors.default_error_message(
136:           :length_between,
137:           humanized_field_name,
138:           @range.min,
139:           @range.max
140:         )
141:       end
validation_method() click to toggle source

Return the method to validate the value with

@return [Symbol]

  the validation method

@api private

    # File lib/dm-validations/validators/length_validator.rb, line 76
76:       def validation_method
77:         @validation_method ||=
78:           if    @equal then :validate_equals
79:           elsif @range then :validate_range
80:           elsif @min   then :validate_min
81:           elsif @max   then :validate_max
82:           end
83:       end
value_length(value) click to toggle source

Return the length in characters

@param [#] value

  the string to get the number of characters for

@return [Integer]

  the number of characters in the string

@api private

    # File lib/dm-validations/validators/length_validator.rb, line 94
94:       def value_length(value)
95:         value.to_str.length
96:       end
value_length(value) click to toggle source
     # File lib/dm-validations/validators/length_validator.rb, line 99
 99:         def value_length(value)
100:           value.to_str.scan(/./).size
101:         end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.