Sha256: f23f29300e2563765f83f59b58127f6b259598f677775e9cff8987c7fe04b199

Contents?: true

Size: 1.09 KB

Versions: 2

Compression:

Stored size: 1.09 KB

Contents

module ContextValidations::Model
  def self.included(base)
    base.class_eval do
      reset_callbacks(:validate)
    end

    base._validators.keys.each do |key|
      base._validators.delete(key)
    end
  end

  # The collection of validations assigned to this model instance
  #
  # @return [Array]
  def validations
    @validations ||= []
  end

  # Use to set the validations collection assigned to this model instance
  #
  # Pass an array of validator instances
  #
  # @param [[ActiveMode::Validations::Validator]]
  def validations=(validations)
    @validations = validations.flatten
  end

  protected

  def run_validations!
    Array.wrap(validations).each do |validator|
      if validator.respond_to?(:setup)
        validator.setup(self.class)
      end
      validator.validate(self)
    end
    errors.empty?
  end

  private

  def _validators
    validations.inject({}) do |hash, validator|
      attribute = validator.attributes.first
      if hash.key?(attribute)
        hash[attribute] << validator
      else
        hash[attribute] = [validator]
      end
      hash
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
context_validations-0.1.1 lib/context_validations/model.rb
context_validations-0.1.0 lib/context_validations/model.rb