Module: WhyValidationsSuckIn96::AttributeBasedValidation
- Included in:
- ValidatesAcceptance, ValidatesAssociated, ValidatesConfirmation, ValidatesDate, ValidatesEmail, ValidatesExclusion, ValidatesFormat, ValidatesInclusion, ValidatesLength, ValidatesNumericality, ValidatesPresence, ValidatesUniqueness, ValidatesUrl
- Defined in:
- lib/whyvalidationssuckin96/attribute_based_validation.rb
Overview
A mixin to help handle the most common case of validating a single attribute on an object. This module has a dependency on SkippableValidation that will most likely be removed in future releases, but is something to be aware of currently.
Instance Method Summary collapse
-
#attribute ⇒ Object
The attribute to validate against.
-
#attribute_value ⇒ Object
The value of the attribute to validate against.
-
#initialize(validatable, options = {}) ⇒ Object
An initializer for a validation that checks to see if the required options have been passed for attribute based validation to work as expected.
-
#validate ⇒ Object
A default validate implementation that skips on #nil?/#blank? attribute values if :allow_nil or :allow_blank have been set.
-
#validates? ⇒ true, ...
Performs the validation, returning true or false if the validation passes or fails, or nil if the validation will not run.
Instance Method Details
#attribute ⇒ Object
The attribute to validate against
23 24 25 |
# File 'lib/whyvalidationssuckin96/attribute_based_validation.rb', line 23 def attribute [:attribute] end |
#attribute_value ⇒ Object
The value of the attribute to validate against
28 29 30 31 32 33 34 |
# File 'lib/whyvalidationssuckin96/attribute_based_validation.rb', line 28 def attribute_value if [:array] validatable.send([:attribute])[@pos] else validatable.send([:attribute]) end end |
#initialize(validatable, options = {}) ⇒ Object
An initializer for a validation that checks to see if the required options have been passed for attribute based validation to work as expected.
16 17 18 19 20 |
# File 'lib/whyvalidationssuckin96/attribute_based_validation.rb', line 16 def initialize(validatable, = {}) raise(ArgumentError, "The attribute to validate must be specified as :attribute") unless [:attribute] @pos = 0 if [:array] super end |
#validate ⇒ Object
A default validate implementation that skips on #nil?/#blank? attribute values if :allow_nil or :allow_blank have been set.
38 39 40 41 |
# File 'lib/whyvalidationssuckin96/attribute_based_validation.rb', line 38 def validate skip if skip_on_empty? || skip_on_blank? || skip_on_nil? super end |
#validates? ⇒ true, ...
-
this method is uuuuuuuugly
Performs the validation, returning true or false if the validation passes or fails, or nil if the validation will not run.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/whyvalidationssuckin96/attribute_based_validation.rb', line 48 def validates? if [:array] return @passed = nil if skip_on_empty? return @passed = false if !skip_on_empty? && Array(validatable.send([:attribute])).empty? reset @pos = 0 statuses = [] while validating? status = super @pos += 1 statuses << status end @passed = statuses.all? else super end end |