module Adama::Validator

Public Class Methods

new(**kwargs) click to toggle source

This module is meant to be prepended to another module Call the child class initializer first, this will set kwargs Then validate

Calls superclass method
# File lib/adama/validator.rb, line 30
def initialize(**kwargs)
  super(**kwargs)
  validate!
end
prepended(base) click to toggle source
# File lib/adama/validator.rb, line 3
def self.prepended(base)
  base.extend ClassMethods
end

Public Instance Methods

errors() click to toggle source
# File lib/adama/validator.rb, line 35
def errors
  @errors ||= {}
end
valid?() click to toggle source
# File lib/adama/validator.rb, line 39
def valid?
  @valid
end
validate!() click to toggle source

Iterate over the validators registered, and for each validator call `validate!` passing in the instance of the class this module was prepended to.

# File lib/adama/validator.rb, line 46
def validate!
  @valid = true
  self.class.validators.each do |validator|
    validator.validate! self
    merge_errors validator.errors
    @valid = validator.valid? && @valid
  end
end

Private Instance Methods

merge_errors(new_errors) click to toggle source

Create a uniform error output, per attribute

# File lib/adama/validator.rb, line 58
def merge_errors(new_errors)
  errors.merge!(new_errors) do |key, oldval, newval|
    (newval.is_a?(Array) ? (oldval + newval) : (oldval << newval)).uniq
  end
end