diff options
Diffstat (limited to 'activemodel/lib/active_model/validator.rb')
-rw-r--r-- | activemodel/lib/active_model/validator.rb | 57 |
1 files changed, 27 insertions, 30 deletions
diff --git a/activemodel/lib/active_model/validator.rb b/activemodel/lib/active_model/validator.rb index 163124d531..c5ed8d22d3 100644 --- a/activemodel/lib/active_model/validator.rb +++ b/activemodel/lib/active_model/validator.rb @@ -5,9 +5,9 @@ require 'active_support/core_ext/object/blank' module ActiveModel #:nodoc: # == Active Model Validator - # - # A simple base class that can be used along with - # +ActiveModel::Validations::ClassMethods.validates_with+ + # + # A simple base class that can be used along with + # ActiveModel::Validations::ClassMethods.validates_with # # class Person # include ActiveModel::Validations @@ -42,7 +42,7 @@ module ActiveModel #:nodoc: # end # end # - # To cause a validation error, you must add to the <tt>record<tt>'s errors directly + # To cause a validation error, you must add to the <tt>record</tt>'s errors directly # from within the validators message # # class MyValidator < ActiveModel::Validator @@ -61,31 +61,31 @@ module ActiveModel #:nodoc: # @my_custom_field = options[:field_name] || :first_name # end # end - # + # # The easiest way to add custom validators for validating individual attributes - # is with the convenient ActiveModel::EachValidator for example: - # + # is with the convenient <tt>ActiveModel::EachValidator</tt>. For example: + # # class TitleValidator < ActiveModel::EachValidator # def validate_each(record, attribute, value) # record.errors[attribute] << 'must be Mr. Mrs. or Dr.' unless ['Mr.', 'Mrs.', 'Dr.'].include?(value) # end # end - # + # # This can now be used in combination with the +validates+ method - # (see ActiveModel::Validations::ClassMethods.validates for more on this) - # + # (see <tt>ActiveModel::Validations::ClassMethods.validates</tt> for more on this) + # # class Person # include ActiveModel::Validations # attr_accessor :title - # - # validates :title, :presence => true, :title => true + # + # validates :title, :presence => true # end - # + # # Validator may also define a +setup+ instance method which will get called - # with the class that using that validator as it's argument. This can be - # useful when there are prerequisites such as an attr_accessor being present + # with the class that using that validator as its argument. This can be + # useful when there are prerequisites such as an +attr_accessor+ being present # for example: - # + # # class MyValidator < ActiveModel::Validator # def setup(klass) # klass.send :attr_accessor, :custom_attribute @@ -94,13 +94,11 @@ module ActiveModel #:nodoc: # # This setup method is only called when used with validation macros or the # class level <tt>validates_with</tt> method. - # + # class Validator attr_reader :options - # Returns the kind of the validator. - # - # == Examples + # Returns the kind of the validator. Examples: # # PresenceValidator.kind # => :presence # UniquenessValidator.kind # => :uniqueness @@ -122,18 +120,18 @@ module ActiveModel #:nodoc: # Override this method in subclasses with validation logic, adding errors # to the records +errors+ array where necessary. def validate(record) - raise NotImplementedError + raise NotImplementedError, "Subclasses must implement a validate(record) method." end end - # EachValidator is a validator which iterates through the attributes given - # in the options hash invoking the validate_each method passing in the + # +EachValidator+ is a validator which iterates through the attributes given + # in the options hash invoking the <tt>validate_each</tt> method passing in the # record, attribute and value. # - # All Active Model validations are built on top of this Validator. + # All Active Model validations are built on top of this validator. class EachValidator < Validator attr_reader :attributes - + # Returns a new validator instance. All options will be available via the # +options+ reader, however the <tt>:attributes</tt> option will be removed # and instead be made available through the +attributes+ reader. @@ -158,19 +156,18 @@ module ActiveModel #:nodoc: # Override this method in subclasses with the validation logic, adding # errors to the records +errors+ array where necessary. def validate_each(record, attribute, value) - raise NotImplementedError + raise NotImplementedError, "Subclasses must implement a validate_each(record, attribute, value) method" end # Hook method that gets called by the initializer allowing verification # that the arguments supplied are valid. You could for example raise an - # ArgumentError when invalid options are supplied. + # +ArgumentError+ when invalid options are supplied. 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. + # +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 |