Sha256: 8c9d82d3c48760503efb9c668e798dc655d8c3c9fd225ca43f99bf19270f92eb

Contents?: true

Size: 1.63 KB

Versions: 12

Compression:

Stored size: 1.63 KB

Contents

Dir.foreach(File.join(File.dirname(__FILE__), 'validators/')) { | file | require File.join(File.dirname(__FILE__), "validators/#{file}") if file =~ /\.rb$/ }

module Xeroizer
  module Record
    module ValidationHelper
      
      def self.included(base)
        base.extend(ClassMethods)
        base.send :include, InstanceMethods
      end
      
      module ClassMethods
        
        # Adds a validator config for each attribute specified in args.
        def validates_with_validator(validator, args)
          options = args.extract_options!
          self.validators ||= []
          args.flatten.each do | attribute |
            self.validators << validator.new(attribute, options)
          end
        end
        
        def validates_associated(*args)
          validates_with_validator(Validator::AssociatedValidator, args)
        end
        
        def validates_inclusion_of(*args)
          validates_with_validator(Validator::InclusionOfValidator, args)
        end

        def validates_presence_of(*args)
          validates_with_validator(Validator::PresenceOfValidator, args)
        end        
        
      end
      
      module InstanceMethods
      
        def valid?
          @errors = []
          if self.class.validators
            self.class.validators.each do | validator |
              validator.validate(self)
            end
          end
          @errors.size == 0
        end
        
        def errors_for(attribute)
          if errors.is_a?(Array)
            errors.find_all { | (attr, msg) | attr == attribute }.map { | (attr, msg) | msg }
          end
        end
        
      end
      
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
xeroizer-0.3.5 lib/xeroizer/record/validation_helper.rb
xeroizer-0.3.4 lib/xeroizer/record/validation_helper.rb
xeroizer-0.3.3 lib/xeroizer/record/validation_helper.rb
xeroizer-0.3.2 lib/xeroizer/record/validation_helper.rb
xeroizer-0.3.1 lib/xeroizer/record/validation_helper.rb
xeroizer-0.3.0 lib/xeroizer/record/validation_helper.rb
xeroizer-0.2.2 lib/xeroizer/record/validation_helper.rb
xeroizer-0.2.1 lib/xeroizer/record/validation_helper.rb
xeroizer-0.2.0 lib/xeroizer/record/validation_helper.rb
xeroizer-0.1.3 lib/xeroizer/record/validation_helper.rb
xeroizer-0.1.2 lib/xeroizer/record/validation_helper.rb
xeroizer-0.1.0 lib/xeroizer/record/validation_helper.rb