Skip to content

Commit

Permalink
adding rdoc for recent changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jaycfields committed Apr 26, 2007
1 parent f3e9399 commit 75ba177
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
23 changes: 23 additions & 0 deletions README_TEMPLATE
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,29 @@ Similar to Rails, Validatable also supports conditional validation.
validates_format_of :name, :with => /.+/, :if => Proc.new { !name.nil? }
end
Person.new.valid? #=> true

Validatable also exposes an after_validate hook method.

class Person
include Validatable
validates_presence_of :name
attr_accessor :name
end

class ValidatesPresenceOf
after_validate do |result, instance, attribute|
instance.errors.add("#{attribute} can't be blank") unless result
end
end

person = Person.new
person.valid? #=> false
person.errors.on(:name) #=> "name can't be blank"

The after_validate hook yields the result of the validation being run,
the instance the validation was run on, and the attribute that was validate

In the above example the attribute "name" is appended to the message.

See the tests for more examples

Expand Down
14 changes: 10 additions & 4 deletions lib/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def validates_acceptance_of(*args)
#
# Configuration options:
#
# * case_sensitive - Whether or not to apply case-sensitivity on the comparison. Defaults to true.
# * message - The message to add to the errors collection when the validation fails
# * times - The number of times the validation applies
# * level - The level at which the validation should occur
Expand Down Expand Up @@ -171,7 +172,7 @@ def validates_true_for(*args)
end
end

# call-seq: include_validations_for(*args)
# call-seq: include_validations_for(attribute_to_validate, options = {})
#
# Validates the specified attributes.
# class Person
Expand All @@ -182,7 +183,7 @@ def validates_true_for(*args)
#
# class PersonPresenter
# include Validatable
# include_validations_for :person
# include_validations_for :person, :map => { :name => :namen }, :if => lambda { not person.nil? }
# attr_accessor :person
#
# def initialize(person)
Expand All @@ -192,9 +193,14 @@ def validates_true_for(*args)
#
# presenter = PersonPresenter.new(Person.new)
# presenter.valid? #=> false
# presenter.errors.on(:name) #=> "can't be blank"
# presenter.errors.on(:namen) #=> "can't be blank"
#
# The person attribute will be validated. If person is invalid the errors will be added to the PersonPresenter errors collection.
#
# Configuration options:
#
# * map - A hash that maps attributes of the child to attributes of the parent.
# * if - A block that when executed must return true of the validation will not occur.
def include_validations_for(attribute_to_validate, options = {})
children_to_validate << ChildValidation.new(attribute_to_validate, options[:map] || {}, options[:if] || lambda { true })
end
Expand Down Expand Up @@ -276,7 +282,7 @@ def validate(groups) #:nodoc:
end
end

def run_validation(validation)
def run_validation(validation) #:nodoc:
validation_result = validation.valid?(self)
self.errors.add(validation.attribute, validation.message) unless validation_result
validation.run_after_validate(validation_result, self, validation.attribute)
Expand Down
2 changes: 1 addition & 1 deletion lib/child_validation.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Validatable
class ChildValidation
class ChildValidation #:nodoc:
attr_accessor :attribute, :map, :should_validate_proc

def initialize(attribute, map, should_validate_proc)
Expand Down
2 changes: 1 addition & 1 deletion rakefile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
specification = Gem::Specification.new do |s|
s.name = "validatable"
s.summary = "Validatable is a library for adding validations."
s.version = "1.1.2"
s.version = "1.2.1"
s.author = 'Jay Fields'
s.description = "Validatable is a library for adding validations."
s.email = 'validatable-developer@rubyforge.org'
Expand Down

0 comments on commit 75ba177

Please sign in to comment.