Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 35530e901086b8591f7d3ce29d27ea51061badb0 @artemk committed May 26, 2011
@@ -0,0 +1,26 @@
+
+require 'validatable/object_extension'
+require 'validatable/errors'
+require 'validatable/validatable_class_methods'
+require 'validatable/macros'
+require 'validatable/validatable_instance_methods'
+require 'validatable/included_validation'
+require 'validatable/child_validation'
+require 'validatable/understandable'
+require 'validatable/requireable'
+require 'validatable/validations/validation_base'
+require 'validatable/validations/validates_format_of'
+require 'validatable/validations/validates_presence_of'
+require 'validatable/validations/validates_acceptance_of'
+require 'validatable/validations/validates_confirmation_of'
+require 'validatable/validations/validates_length_of'
+require 'validatable/validations/validates_true_for'
+require 'validatable/validations/validates_numericality_of'
+require 'validatable/validations/validates_exclusion_of'
+require 'validatable/validations/validates_inclusion_of'
+require 'validatable/validations/validates_each'
+require 'validatable/validations/validates_associated'
+
+module Validatable
+ Version = '1.8.4'
+end
@@ -0,0 +1,15 @@
+module Validatable
+ class ChildValidation #:nodoc:
+ attr_accessor :attribute, :map, :should_validate_proc
+
+ def initialize(attribute, map, should_validate_proc)
+ @attribute = attribute
+ @map = map
+ @should_validate_proc = should_validate_proc
+ end
+
+ def should_validate?(instance)
+ instance.instance_eval &should_validate_proc
+ end
+ end
+end
@@ -0,0 +1,115 @@
+module Validatable
+ class Errors
+ include Enumerable
+
+ # Returns true if the specified +attribute+ has errors associated with it.
+ #
+ # class Company < ActiveRecord::Base
+ # validates_presence_of :name, :address, :email
+ # validates_length_of :name, :in => 5..30
+ # end
+ #
+ # company = Company.create(:address => '123 First St.')
+ # company.errors.invalid?(:name) # => true
+ # company.errors.invalid?(:address) # => false
+ def invalid?(attribute)
+ !@errors[attribute.to_sym].nil?
+ end
+
+ # Adds an error to the base object instead of any particular attribute. This is used
+ # to report errors that don't tie to any specific attribute, but rather to the object
+ # as a whole. These error messages don't get prepended with any field name when iterating
+ # with +each_full+, so they should be complete sentences.
+ def add_to_base(msg)
+ add(:base, msg)
+ end
+
+ # Returns errors assigned to the base object through +add_to_base+ according to the normal rules of <tt>on(attribute)</tt>.
+ def on_base
+ on(:base)
+ end
+
+ # call-seq: on(attribute)
+ #
+ # * Returns nil, if no errors are associated with the specified +attribute+.
+ # * Returns the error message, if one error is associated with the specified +attribute+.
+ # * Returns an array of error messages, if more than one error is associated with the specified +attribute+.
+ def on(attribute)
+ return nil if errors[attribute.to_sym].nil?
+ errors[attribute.to_sym].size == 1 ? errors[attribute.to_sym].first : errors[attribute.to_sym]
+ end
+
+ # Rails 3 API for errors, always return array.
+ def [](attribute)
+ errors[attribute.to_sym] || []
+ end
+
+ def add(attribute, message) #:nodoc:
+ errors[attribute.to_sym] = [] if errors[attribute.to_sym].nil?
+ errors[attribute.to_sym] << message
+ end
+
+ def merge!(errors) #:nodoc:
+ errors.each_pair{|k, v| add(k,v)}
+ self
+ end
+
+ # call-seq: replace(attribute)
+ #
+ # * Replaces the errors value for the given +attribute+
+ def replace(attribute, value)
+ errors[attribute.to_sym] = value
+ end
+
+ # call-seq: raw(attribute)
+ #
+ # * Returns an array of error messages associated with the specified +attribute+.
+ def raw(attribute)
+ errors[attribute.to_sym]
+ end
+
+ def errors #:nodoc:
+ @errors ||= {}
+ end
+
+ def count #:nodoc:
+ errors.values.flatten.size
+ end
+
+ # call-seq: full_messages -> an_array_of_messages
+ #
+ # Returns an array containing the full list of error messages.
+ def full_messages
+ full_messages = []
+
+ errors.each_key do |attribute|
+ errors[attribute].each do |msg|
+ next if msg.nil?
+
+ if attribute.to_s == "base"
+ full_messages << msg
+ else
+ full_messages << humanize(attribute.to_s) + " " + msg
+ end
+ end
+ end
+ full_messages
+ end
+
+ def humanize(lower_case_and_underscored_word) #:nodoc:
+ lower_case_and_underscored_word.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize
+ end
+
+ def to_json
+ errors.to_json
+ end
+
+ [:clear, :each, :each_pair, :empty?, :length, :size].each do |meth|
+
+ define_method meth do
+ errors.send(meth)
+ end
+
+ end
+ end
+end
@@ -0,0 +1,9 @@
+module Validatable
+ class IncludedValidation #:nodoc:
+ attr_accessor :attribute
+
+ def initialize(attribute)
+ @attribute = attribute
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit 35530e9

Please sign in to comment.