Class: Dinamo::Model::Validation::EachValidator

Inherits:
Validator
  • Object
show all
Defined in:
lib/dinamo/model/validator.rb

Direct Known Subclasses

PresenceValidator

Instance Attribute Summary collapse

Attributes inherited from Validator

#options

Instance Method Summary collapse

Constructor Details

#initialize(attributes: [], **options) ⇒ EachValidator

Returns a new instance of EachValidator.

Raises:

  • (ArgumentError)


22
23
24
25
26
# File 'lib/dinamo/model/validator.rb', line 22

def initialize(attributes: [], **options)
  @attributes = attributes
  raise ArgumentError, ":attributes cannot be blank" if @attributes.empty?
  super
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



20
21
22
# File 'lib/dinamo/model/validator.rb', line 20

def attributes
  @attributes
end

Instance Method Details

#validate(record) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/dinamo/model/validator.rb', line 28

def validate(record)
  attributes.each do |key, val|
    value = record[key]
    next if (value.nil? && options[:allow_nil]) ||
      (value.blank? && options[:allow_blank])
    validate_each(record, key, value)
  end
end

#validate_each(record, attribute, value) ⇒ Object

Raises:

  • (NotImplementedError)


37
38
39
# File 'lib/dinamo/model/validator.rb', line 37

def validate_each(record, attribute, value)
  raise NotImplementedError
end