Class: ArrayValidatorBase
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- ArrayValidatorBase
- Defined in:
- lib/active_model_validators_ex/array_validator_base.rb
Direct Known Subclasses
Instance Method Summary collapse
- #custom_validations(record, attribute, value) ⇒ Object
-
#initialize(options) ⇒ ArrayValidatorBase
constructor
A new instance of ArrayValidatorBase.
- #validate_each(record, attribute, value) ⇒ Object
Constructor Details
#initialize(options) ⇒ ArrayValidatorBase
Returns a new instance of ArrayValidatorBase.
2 3 4 5 6 7 |
# File 'lib/active_model_validators_ex/array_validator_base.rb', line 2 def initialize() [:allow_nil] ||= false [:allow_blank] ||= false super() end |
Instance Method Details
#custom_validations(record, attribute, value) ⇒ Object
32 33 34 |
# File 'lib/active_model_validators_ex/array_validator_base.rb', line 32 def custom_validations(record, attribute, value) raise 'override this method to perform custom validations' end |
#validate_each(record, attribute, value) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/active_model_validators_ex/array_validator_base.rb', line 9 def validate_each(record, attribute, value) # TODO: extract this to a more generic class instad of ArrayValidatorBase return if ([:allow_blank] && value.blank?) || ([:allow_nil] && value.nil?) if ![:allow_blank] && value.blank? record.errors.add(attribute, :blank, ) return end if ![:allow_nil] && value.nil? record.errors.add(attribute, :nil, ) return end unless value.is_a? Array record.errors.add(attribute, :array, ) return end custom_validations(record, attribute, value) end |