class UseCaseValidations::EachValidator

Attributes

attributes[R]

Public Class Methods

new(options) click to toggle source
Calls superclass method
# 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

Public Instance Methods

check_validity!() click to toggle source
# File lib/usecasing_validations/each_validator.rb, line 28
def check_validity!; end
validate(record) click to toggle source
# 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) click to toggle source

Override this method in subclasses with the validation logic, adding errors to the records errors array where necessary.

# 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