module Toolchain::Validations::InstanceMethods

Public Instance Methods

errors() click to toggle source

@return [Toolchain::Validations::ValidationErrors]

# File lib/toolchain/validations.rb, line 78
def errors
  @errors ||= ValidationErrors.new
end
valid?() click to toggle source

Resets the ValidationErrors object, re-validates the current object and then determines whether or not this object is valid.

@note If invalid (returns `false`), errors will have been

added to the `object.errors` class.

@return [Boolean]

# File lib/toolchain/validations.rb, line 113
def valid?
  errors.reset
  validate
  errors.empty?
end
validate() click to toggle source

Runs all the validation operations and will apply error messages to the ValidationErrors object when validator errors occur.

# File lib/toolchain/validations.rb, line 85
def validate
  Helpers.each_validation(self.class) do |v|
    validate = true

    if v[:if].kind_of?(Proc) && v[:if].call(self) == false
      validate = false
    end

    if v[:unless].kind_of?(Proc) && v[:unless].call(self) == true
      validate = false
    end

    if validate
      v[:validator].new(self, v[:key_path], v[:data]).validate
    end
  end

  Helpers.each_validation(self.class, :custom_validations) { |v| send(v) }
end