Module: DataMapper::Validations::AutoValidations
- Included in:
- ClassMethods
- Defined in:
- lib/dm-validations/auto_validate.rb
Instance Attribute Summary collapse
-
#disable_auto_validations ⇒ Object
readonly
Returns the value of attribute disable_auto_validations.
Instance Method Summary collapse
-
#auto_generate_validations(property) ⇒ Object
Auto-generate validations for a given property.
-
#disabled_auto_validations? ⇒ TrueClass, FalseClass
(also: #auto_validations_disabled?)
Checks whether auto validations are currently disabled (see
disable_auto_validations
method that takes a block). - #infer_format_validation_for(property, options) ⇒ Object
- #infer_length_validation_for(property, options) ⇒ Object
- #infer_presence_validation_for(property, options) ⇒ Object
- #infer_type_validation_for(property, options) ⇒ Object
- #infer_uniqueness_validation_for(property, options) ⇒ Object
- #infer_within_validation_for(property, options) ⇒ Object
-
#options_with_message(base_options, property, validator_name) ⇒ Object
adds message for validator.
-
#skip_auto_validation_for?(property) ⇒ TrueClass, FalseClass
Checks whether or not property should be auto validated.
-
#without_auto_validations(&block) ⇒ Object
disables generation of validations for duration of given block.
Instance Attribute Details
#disable_auto_validations ⇒ Object (readonly)
Returns the value of attribute disable_auto_validations.
25 26 27 |
# File 'lib/dm-validations/auto_validate.rb', line 25 def disable_auto_validations @disable_auto_validations end |
Instance Method Details
#auto_generate_validations(property) ⇒ Object
Auto-generate validations for a given property. This will only occur if the option :auto_validation is either true or left undefined.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/dm-validations/auto_validate.rb', line 85 def auto_generate_validations(property) return if disabled_auto_validations? || skip_auto_validation_for?(property) # all auto-validations (aside from presence) should skip # validation when the value is nil opts = { :allow_nil => true } if property..key?(:validates) opts[:context] = property.[:validates] end infer_presence_validation_for(property, opts.dup) infer_length_validation_for(property, opts.dup) infer_format_validation_for(property, opts.dup) infer_uniqueness_validation_for(property, opts.dup) infer_within_validation_for(property, opts.dup) infer_type_validation_for(property, opts.dup) end |
#disabled_auto_validations? ⇒ TrueClass, FalseClass Also known as: auto_validations_disabled?
Checks whether auto validations are currently disabled (see disable_auto_validations
method that takes a block)
111 112 113 |
# File 'lib/dm-validations/auto_validate.rb', line 111 def disabled_auto_validations? @disable_auto_validations || false end |
#infer_format_validation_for(property, options) ⇒ Object
144 145 146 147 148 149 150 |
# File 'lib/dm-validations/auto_validate.rb', line 144 def infer_format_validation_for(property, ) return unless property..key?(:format) [:with] = property.[:format] validates_format_of property.name, (, property, :format) end |
#infer_length_validation_for(property, options) ⇒ Object
133 134 135 136 137 138 139 140 141 142 |
# File 'lib/dm-validations/auto_validate.rb', line 133 def infer_length_validation_for(property, ) return unless [ DataMapper::Property::String, DataMapper::Property::Text ].include?(property.class) case length = property..fetch(:length, DataMapper::Property::String::DEFAULT_LENGTH) when Range then [:within] = length else [:maximum] = length end validates_length_of property.name, (, property, :length) end |
#infer_presence_validation_for(property, options) ⇒ Object
127 128 129 130 131 |
# File 'lib/dm-validations/auto_validate.rb', line 127 def infer_presence_validation_for(property, ) return if skip_presence_validation?(property) validates_presence_of property.name, (, property, :presence) end |
#infer_type_validation_for(property, options) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/dm-validations/auto_validate.rb', line 173 def infer_type_validation_for(property, ) return if property.custom? if property.kind_of?(Property::Numeric) [:gte] = property.min if property.min [:lte] = property.max if property.max end if Integer == property.primitive [:integer_only] = true validates_numericality_of property.name, (, property, :is_number) elsif BigDecimal == property.primitive || Float == property.primitive [:precision] = property.precision [:scale] = property.scale validates_numericality_of property.name, (, property, :is_number) else # We only need this in the case we don't already # have a numeric validator, because otherwise # it will cause duplicate validation errors validates_primitive_type_of property.name, (, property, :is_primitive) end end |
#infer_uniqueness_validation_for(property, options) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/dm-validations/auto_validate.rb', line 152 def infer_uniqueness_validation_for(property, ) return unless property..key?(:unique) case value = property.[:unique] when Array, Symbol [:scope] = Array(value) validates_uniqueness_of property.name, (, property, :is_unique) when TrueClass validates_uniqueness_of property.name, (, property, :is_unique) end end |
#infer_within_validation_for(property, options) ⇒ Object
165 166 167 168 169 170 171 |
# File 'lib/dm-validations/auto_validate.rb', line 165 def infer_within_validation_for(property, ) return unless property..key?(:set) [:set] = property.[:set] validates_within property.name, (, property, :within) end |
#options_with_message(base_options, property, validator_name) ⇒ Object
adds message for validator
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/dm-validations/auto_validate.rb', line 12 def (, property, validator_name) = .clone opts = property. if opts.key?(:messages) [:message] = opts[:messages][validator_name] elsif opts.key?(:message) [:message] = opts[:message] end end |
#skip_auto_validation_for?(property) ⇒ TrueClass, FalseClass
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
123 124 125 |
# File 'lib/dm-validations/auto_validate.rb', line 123 def skip_auto_validation_for?(property) property..key?(:auto_validation) && !property.[:auto_validation] end |
#without_auto_validations(&block) ⇒ Object
disables generation of validations for duration of given block
29 30 31 32 33 |
# File 'lib/dm-validations/auto_validate.rb', line 29 def without_auto_validations(&block) @disable_auto_validations = true block.call @disable_auto_validations = false end |