aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model/validator.rb
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2009-12-23 01:37:19 +0100
committerJosé Valim <jose.valim@gmail.com>2009-12-23 01:38:15 +0100
commit279067639f319f3b4bbcaf90c26f286e96df2c77 (patch)
tree84ea6824ddc154c526d6d1094f20904cc5a25c81 /activemodel/lib/active_model/validator.rb
parent977a5c43b160d8aa8d1b87bb0feb54db85fe203c (diff)
downloadrails-279067639f319f3b4bbcaf90c26f286e96df2c77.tar.gz
rails-279067639f319f3b4bbcaf90c26f286e96df2c77.tar.bz2
rails-279067639f319f3b4bbcaf90c26f286e96df2c77.zip
validates_each uses a BlockValidator.
Diffstat (limited to 'activemodel/lib/active_model/validator.rb')
-rw-r--r--activemodel/lib/active_model/validator.rb22
1 files changed, 20 insertions, 2 deletions
diff --git a/activemodel/lib/active_model/validator.rb b/activemodel/lib/active_model/validator.rb
index 342c4691ff..8c9f9c7fb3 100644
--- a/activemodel/lib/active_model/validator.rb
+++ b/activemodel/lib/active_model/validator.rb
@@ -51,7 +51,6 @@ module ActiveModel #:nodoc:
# @my_custom_field = options[:field_name] || :first_name
# end
# end
- #
class Validator
attr_reader :options
@@ -64,6 +63,11 @@ module ActiveModel #:nodoc:
end
end
+ # EachValidator is a validator which iterates through the attributes given
+ # in the options hash invoking the validate_each method passing in the
+ # record, attribute and value.
+ #
+ # All ActiveModel validations are built on top of this Validator.
class EachValidator < Validator
attr_reader :attributes
@@ -81,11 +85,25 @@ module ActiveModel #:nodoc:
end
end
- def validate_each(record)
+ def validate_each(record, attribute, value)
raise NotImplementedError
end
def check_validity!
end
end
+
+ # BlockValidator is a special EachValidator which receives a block on initialization
+ # and call this block for each attribute being validated. +validates_each+ uses this
+ # Validator.
+ class BlockValidator < EachValidator
+ def initialize(options, &block)
+ @block = block
+ super
+ end
+
+ def validate_each(record, attribute, value)
+ @block.call(record, attribute, value)
+ end
+ end
end