Class: UseCaseValidations::EachValidator
- Defined in:
- lib/usecasing_validations/each_validator.rb
Direct Known Subclasses
Validations::FormatValidator, Validations::LengthValidator, Validations::NumericalityValidator, Validations::PresenceValidator, Validations::UniquenessValidator
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
Attributes inherited from Validator
Instance Method Summary collapse
- #check_validity! ⇒ Object
-
#initialize(options) ⇒ EachValidator
constructor
A new instance of EachValidator.
- #validate(record) ⇒ Object
-
#validate_each(record, attribute, value) ⇒ Object
Override this method in subclasses with the validation logic, adding errors to the records
errors
array where necessary.
Constructor Details
#initialize(options) ⇒ EachValidator
Returns a new instance of EachValidator.
7 8 9 10 11 12 |
# File 'lib/usecasing_validations/each_validator.rb', line 7 def initialize() @attributes = Array(.delete(:attributes)) raise ArgumentError, ":attributes cannot be blank" if @attributes.empty? super check_validity! end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
5 6 7 |
# File 'lib/usecasing_validations/each_validator.rb', line 5 def attributes @attributes end |
Instance Method Details
#check_validity! ⇒ Object
28 |
# File 'lib/usecasing_validations/each_validator.rb', line 28 def check_validity!; end |
#validate(record) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/usecasing_validations/each_validator.rb', line 14 def validate(record) attributes.each do |attribute| value = record.respond_to?(attribute) ? record.send(attribute) : nil next if (value.nil? && [:allow_nil]) || (Helpers._blank?(value) && [:allow_blank]) validate_each(record, attribute, value) end end |
#validate_each(record, attribute, value) ⇒ Object
Override this method in subclasses with the validation logic, adding errors to the records errors
array where necessary.
24 25 26 |
# File 'lib/usecasing_validations/each_validator.rb', line 24 def validate_each(record, attribute, value) raise NotImplementedError, "Subclasses must implement a validate_each(record, attribute, value) method" end |