Class Index [+]

Quicksearch

DataMapper::Validations::AutoValidations

Attributes

disable_auto_validations[R]

TODO: why are there 3 entry points to this ivar? #, #, #

Public Class Methods

generate_for_property(property) click to toggle source

Auto-generate validations for a given property. This will only occur if the option :auto_validation is either true or left undefined.

  Triggers that generate validator creation

  :required => true
      Setting the option :required to true causes a
      validates_presence_of validator to be automatically created on
      the property

  :length => 20
      Setting the option :length causes a validates_length_of
      validator to be automatically created on the property. If the
      value is a Integer the validation will set :maximum => value
      if the value is a Range the validation will set
      :within => value

  :format => :predefined / lambda / Proc
      Setting the :format option causes a validates_format_of
      validator to be automatically created on the property

  :set => ["foo", "bar", "baz"]
      Setting the :set option causes a validates_within
      validator to be automatically created on the property

  Integer type
      Using a Integer type causes a validates_numericality_of
      validator to be created for the property.  integer_only
      is set to true

  BigDecimal or Float type
      Using a Integer type causes a validates_numericality_of
      validator to be created for the property.  integer_only
      is set to false, and precision/scale match the property

  Messages

  :messages => {..}
      Setting :messages hash replaces standard error messages
      with custom ones. For instance:
      :messages => {:presence => "Field is required",
                    :format => "Field has invalid format"}
      Hash keys are: :presence, :format, :length, :is_unique,
                     :is_number, :is_primitive

  :message => "Some message"
      It is just shortcut if only one validation option is set

@api private

     # File lib/dm-validations/auto_validate.rb, line 101
101:       def self.generate_for_property(property)
102:         return if (property.model.disabled_auto_validations? ||
103:                    skip_auto_validation_for?(property))
104: 
105:         # all auto-validations (aside from presence) should skip
106:         # validation when the value is nil
107:         opts = { :allow_nil => true }
108: 
109:         if property.options.key?(:validates)
110:           opts[:context] = property.options[:validates]
111:         end
112: 
113:         infer_presence_validation_for(property, opts.dup)
114:         infer_length_validation_for(property, opts.dup)
115:         infer_format_validation_for(property, opts.dup)
116:         infer_uniqueness_validation_for(property, opts.dup)
117:         infer_within_validation_for(property, opts.dup)
118:         infer_type_validation_for(property, opts.dup)
119:       end

Private Class Methods

infer_format_validation_for(property, options) click to toggle source

@api private

     # File lib/dm-validations/auto_validate.rb, line 170
170:       def self.infer_format_validation_for(property, options)
171:         return unless property.options.key?(:format)
172: 
173:         options[:with] = property.options[:format]
174: 
175:         validation_options = options_with_message(options, property, :format)
176:         property.model.validates_format_of property.name, validation_options
177:       end
infer_length_validation_for(property, options) click to toggle source

@api private

     # File lib/dm-validations/auto_validate.rb, line 151
151:       def self.infer_length_validation_for(property, options)
152:         return unless (property.kind_of?(DataMapper::Property::String) ||
153:                        property.kind_of?(DataMapper::Property::Text))
154: 
155:         length = property.options.fetch(:length, DataMapper::Property::String::DEFAULT_LENGTH)
156: 
157: 
158:         if length.is_a?(Range)
159:           raise ArgumentError, "Infinity is no valid upper bound for a length range" if length.last == Infinity
160:           options[:within]  = length
161:         else
162:           options[:maximum] = length
163:         end
164: 
165:         validation_options = options_with_message(options, property, :length)
166:         property.model.validates_length_of property.name, validation_options
167:       end
infer_presence_validation_for(property, options) click to toggle source

@api private

     # File lib/dm-validations/auto_validate.rb, line 138
138:       def self.infer_presence_validation_for(property, options)
139:         return if skip_presence_validation?(property)
140: 
141:         validation_options = options_with_message(options, property, :presence)
142:         property.model.validates_presence_of property.name, validation_options
143:       end
infer_type_validation_for(property, options) click to toggle source

@api private

     # File lib/dm-validations/auto_validate.rb, line 206
206:       def self.infer_type_validation_for(property, options)
207:         return if property.respond_to?(:custom?) && property.custom?
208: 
209:         if property.kind_of?(Property::Numeric)
210:           options[:gte] = property.min if property.min
211:           options[:lte] = property.max if property.max
212:         end
213: 
214:         if Integer == property.primitive
215:           options[:integer_only] = true
216: 
217:           validation_options = options_with_message(options, property, :is_number)
218:           property.model.validates_numericality_of property.name, validation_options
219:         elsif (BigDecimal == property.primitive ||
220:                Float == property.primitive)
221:           options[:precision] = property.precision
222:           options[:scale]     = property.scale
223: 
224:           validation_options = options_with_message(options, property, :is_number)
225:           property.model.validates_numericality_of property.name, validation_options
226:         else
227:           # We only need this in the case we don't already
228:           # have a numeric validator, because otherwise
229:           # it will cause duplicate validation errors
230:           validation_options = options_with_message(options, property, :is_primitive)
231:           property.model.validates_primitive_type_of property.name, validation_options
232:         end
233:       end
infer_uniqueness_validation_for(property, options) click to toggle source

@api private

     # File lib/dm-validations/auto_validate.rb, line 180
180:       def self.infer_uniqueness_validation_for(property, options)
181:         return unless property.options.key?(:unique)
182: 
183:         case value = property.options[:unique]
184:           when Array, Symbol
185:             options[:scope] = Array(value)
186: 
187:             validation_options = options_with_message(options, property, :is_unique)
188:             property.model.validates_uniqueness_of property.name, validation_options
189:           when TrueClass
190:             validation_options = options_with_message(options, property, :is_unique)
191:             property.model.validates_uniqueness_of property.name, validation_options
192:         end
193:       end
infer_within_validation_for(property, options) click to toggle source

@api private

     # File lib/dm-validations/auto_validate.rb, line 196
196:       def self.infer_within_validation_for(property, options)
197:         return unless property.options.key?(:set)
198: 
199:         options[:set] = property.options[:set]
200: 
201:         validation_options = options_with_message(options, property, :within)
202:         property.model.validates_within property.name, validation_options
203:       end
options_with_message(base_options, property, validator_name) click to toggle source

adds message for validator

@api private

     # File lib/dm-validations/auto_validate.rb, line 238
238:       def self.options_with_message(base_options, property, validator_name)
239:         options = base_options.clone
240:         opts    = property.options
241: 
242:         if opts.key?(:messages)
243:           options[:message] = opts[:messages][validator_name]
244:         elsif opts.key?(:message)
245:           options[:message] = opts[:message]
246:         end
247: 
248:         options
249:       end
skip_auto_validation_for?(property) click to toggle source

Checks whether or not property should be auto validated. It is the case for properties with :auto_validation option given and it’s value evaluates to true

@return [TrueClass, FalseClass]

  true for properties with :auto_validation option that has
  positive value

@api private

     # File lib/dm-validations/auto_validate.rb, line 132
132:       def self.skip_auto_validation_for?(property)
133:         (property.options.key?(:auto_validation) &&
134:          !property.options[:auto_validation])
135:       end
skip_presence_validation?(property) click to toggle source

@api private

     # File lib/dm-validations/auto_validate.rb, line 146
146:       def self.skip_presence_validation?(property)
147:         property.allow_blank? || property.serial?
148:       end

Public Instance Methods

auto_validations_disabled?() click to toggle source

TODO: deprecate all but one of these 3 variants

disabled_auto_validations?() click to toggle source

Checks whether auto validations are currently disabled (see disable_auto_validations method that takes a block)

@return [TrueClass, FalseClass]

  true if auto validation is currently disabled

@api semipublic

    # File lib/dm-validations/auto_validate.rb, line 33
33:       def disabled_auto_validations?
34:         @disable_auto_validations || false
35:       end
Also aliased as: auto_validations_disabled?
without_auto_validations() click to toggle source

disables generation of validations for duration of given block

@api public

    # File lib/dm-validations/auto_validate.rb, line 44
44:       def without_auto_validations
45:         previous, @disable_auto_validations = @disable_auto_validations, true
46:         yield
47:       ensure
48:         @disable_auto_validations = previous
49:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.