Module: DataMapper::Validate::AutoValidate
Public Visibility
Public Instance Method Summary
| #auto_generate_validations(property) |
Auto-generate validations for a given property. |
|---|
Public Instance Method Details
auto_generate_validations
public
auto_generate_validations(property)
Auto-generate validations for a given property. This will only occur if the option :auto_validation is either true or left undefined.
[View source]
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
# File 'dm-more/dm-validations/lib/dm-validations/auto_validate.rb', line 36 def auto_generate_validations(property) property.options[:auto_validation] = true unless property.options.has_key?(:auto_validation) return unless property.options[:auto_validation] # a serial property is allowed to be nil too, because the # value is set by the storage system opts = { :allow_nil => property.nullable? || property.serial? } opts[:context] = property.options[:validates] if property.options.has_key?(:validates) # presence unless opts[:allow_nil] validates_present property.name, opts end # length if property.type == String #len = property.length # XXX: maybe length should always return a Range, with the min defaulting to 1 len = property.options.fetch(:length, property.options.fetch(:size, DataMapper::Property::DEFAULT_LENGTH)) if len.is_a?(Range) opts[:within] = len else opts[:maximum] = len end validates_length property.name, opts end # format if property.options.has_key?(:format) opts[:with] = property.options[:format] validates_format property.name, opts end # uniqueness validator if property.options.has_key?(:unique) value = property.options[:unique] scope = [] scope.concat(value) if value.is_a?(Array) scope << value if value.is_a?(Symbol) opts = {} opts[:scope] = scope if scope.length > 0 if value.is_a?(TrueClass) || scope.length > 0 validates_is_unique property.name, opts end end # numeric validator if Integer == property.type opts[:integer_only] = true validates_is_number property.name, opts elsif BigDecimal == property.type || Float == property.type opts[:precision] = property.precision opts[:scale] = property.scale validates_is_number property.name, opts end end