Sha256: 0efd82a70c659c249e65061d8ecad107b7b703cd152e9e6075315fdfb5184675

Contents?: true

Size: 1.84 KB

Versions: 2

Compression:

Stored size: 1.84 KB

Contents

# -*- encoding : utf-8 -*-
module Dao
  module Validations
    class Error < Dao::Error
      attr_accessor :errors

      def initialize(*args, &block)
        @errors = args.shift if args.first.respond_to?(:full_messages)
        super(*args, &block)
      end

      def object
        @errors.object if(@errors and @errors.respond_to?(:object))
      end
    end

    Dao.load('validations/callback.rb')
    Dao.load('validations/common.rb')
    Dao.load('validations/validator.rb')

    ClassMethods = proc do
      def validator
        @validator ||= Validator.mixin(self)
      end

      def validator=(validator)
        @validator = validator
      end

      %w(
        validations
        validates
        validates_each
      ).each do |method|
        module_eval <<-__, __FILE__, __LINE__
          def self.#{ method }(*args, &block)
            validator.#{ method }(*args, &block)
          end
        __
      end
    end

    InstanceMethods = proc do
      def validator
        @validator ||= Validator.mixin(self)
      end

      def validator=(validator)
        @validator = validator
      end

      %w(
        validations
        validates
        validates_each
        validated?
        validated!
        validate
        validate!
        run_validations
        run_validations!
        valid!
        valid?
        forcing_validity?
        errors
        status
      ).each do |method|
        module_eval <<-__, __FILE__, __LINE__
          def #{ method }(*args, &block)
            validator.#{ method }(*args, &block)
          end
        __
      end
    end

    def Validations.included(other)
      other.send(:instance_eval, &ClassMethods)
      other.send(:class_eval, &InstanceMethods)
      other.send(:include, Common)
      other.send(:extend, Common)
      super
    end

  end

  Dao.load('validations/instance.rb')
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dao-4.4.4 lib/dao/validations.rb
dao-4.4.2 lib/dao/validations.rb