Class: UseCaseValidations::EachValidator

Inherits:
Validator
  • Object
show all
Defined in:
lib/usecasing_validations/each_validator.rb

Instance Attribute Summary collapse

Attributes inherited from Validator

#base, #options

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ EachValidator

Returns a new instance of EachValidator.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
# File 'lib/usecasing_validations/each_validator.rb', line 7

def initialize(options)
  @attributes = Array(options.delete(:attributes))
  raise ArgumentError, ":attributes cannot be blank" if @attributes.empty?
  super
  check_validity!
end

Instance Attribute Details

#attributesObject (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? && options[:allow_nil]) || (Helpers._blank?(value) && options[: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.

Raises:

  • (NotImplementedError)


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