Skip to content

Commit

Permalink
updated docs for not_naughty
Browse files Browse the repository at this point in the history
  • Loading branch information
boof committed Feb 13, 2008
1 parent d645783 commit 1fbd837
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 12 deletions.
2 changes: 1 addition & 1 deletion model_plugins/not_naughty/lib/not_naughty/builder.rb
Expand Up @@ -3,7 +3,7 @@ module NotNaughty
# == Builder that builds
#
# With this you get syntactical sugar for all descendants of Validation, see
# validates for details.
# validates for examples.
module Builder

# Observer method that creates Validation builder methods.
Expand Down
2 changes: 1 addition & 1 deletion model_plugins/not_naughty/lib/not_naughty/errors.rb
Expand Up @@ -24,7 +24,7 @@ def full_messages
end
end

# Returns an array of evaluated error messages for given attribute/
# Returns an array of evaluated error messages for given attribute.
def on(attribute)
@errors[attribute].map do |message|
eval(message.inspect.delete('\\') % attribute)
Expand Down
7 changes: 3 additions & 4 deletions model_plugins/not_naughty/lib/not_naughty/validation.rb
Expand Up @@ -44,10 +44,9 @@ def self.inherited(descendant) #:nodoc:
# Validation.new TempValidation, :temp, :if => :water?
#
# The last one also notifies all Observers of Validation (see
# ValidationBuilder#update). If ValidationBuilder#update is called because
# <Name>Validation is inherited from Validation the ValidationBuilder gets
# the method validates_<name>_of and so does the classes that included the
# Builder.
# Builder#update). If Builder#update is called because <Name>Validation
# is inherited from Validation the ValidationBuilder gets the method
# validates_<name>_of and so does the classes that included the Builder.
def self.new(*params, &block)
attributes = if params.first.is_a? Class and params.first < self
klass = params.shift
Expand Down
@@ -1,4 +1,21 @@
module NotNaughty

# == Validates acceptance of obj's attribute against a fixed value.
#
# Unless the validation succeeds an error hash (:attribute => :message)
# is added to the obj's instance of Errors.
#
# <b>Options:</b>
# <tt>:accept</tt>:: object that that'll check via a <tt>:match</tt> call
# <tt>:message</tt>:: see NotNaughty::Errors for details
# <tt>:if</tt>:: see NotNaughty::Validation::Condition for details
# <tt>:unless</tt>:: see NotNaughty::Validation::Condition for details
#
# <b>Example:</b>
#
# obj = 'abc'
# def obj.errors() @errors ||= NotNauthy::Errors.new end
#
class AcceptanceValidation < Validation

def initialize(opts, attributes)
Expand Down
@@ -1,7 +1,26 @@
module NotNaughty

# == Validates confirmaton of obj's attribute via <tt>:eql?</tt> method against the _appropiate_ confirmation attribute.
#
# Unless the validation succeeds an error hash (:attribute => :message)
# is added to the obj's instance of Errors.
#
# <b>Options:</b>
# <tt>:message</tt>:: see NotNaughty::Errors for details
# <tt>:if</tt>:: see NotNaughty::Validation::Condition for details
# <tt>:unless</tt>:: see NotNaughty::Validation::Condition for details
#
# <b>Example:</b>
#
# obj = 'abc'
# def obj.errors() @errors ||= NotNauthy::Errors.new end
# def obj.to_s_confirmation() '123 end
#
# ConfirmationValidation.new({}, :to_s).call obj, :to_s, 'abc'
# obj.errors.on(:to_s).any? # => true
class ConfirmationValidation < Validation

def initialize(opts, attributes)
def initialize(opts, attributes) #:nodoc:
__message = opts[:message] || '#{"%s".humanize} could not be confirmed.'

if opts[:allow_blank] or opts[:allow_nil]
Expand Down
@@ -1,7 +1,29 @@
module NotNaughty

# == Validates format of obj's attribute via the <tt>:match</tt> method.
#
# Unless the validation succeeds an error hash (:attribute => :message)
# is added to the obj's instance of Errors.
#
# <b>Options:</b>
# <tt>:with</tt>:: object that that'll check via a <tt>:match</tt> call
# <tt>:message</tt>:: see NotNaughty::Errors for details
# <tt>:if</tt>:: see NotNaughty::Validation::Condition for details
# <tt>:unless</tt>:: see NotNaughty::Validation::Condition for details
#
# <b>Example:</b>
#
# obj = 'abc'
# def obj.errors() @errors ||= NotNauthy::Errors.new end
#
# FormatValidation.new({:with => /[a-z]/}, :to_s).call obj, :to_s, 'abc'
# obj.errors.on(:to_s).any? # => false
#
# FormatValidation.new({:with => /[A-Z]/}, :to_s).call obj, :to_s, 'abc'
# obj.errors.on(:to_s) # => ["Format of to_s does not match."]
class FormatValidation < Validation

def initialize(opts, attributes)
def initialize(opts, attributes) #:nodoc:
(__format = opts[:with]).respond_to? :match or
raise ArgumentError, "#{__format.inspect} doesn't respond to :match"

Expand Down
@@ -1,7 +1,43 @@
module NotNaughty

# == Validates length of obj's attribute via the <tt>:length</tt> method.
#
# Unless the validation succeeds an error hash (:attribute => :message)
# is added to the obj's instance of Errors.
#
# <b>Options:</b>
# <tt>:message</tt>:: see NotNaughty::Errors for details
# <tt>:if</tt>:: see NotNaughty::Validation::Condition for details
# <tt>:unless</tt>:: see NotNaughty::Validation::Condition for details
#
# <b>Boundaries (by precendence):</b>
# <tt>:is</tt>:: valid length
# <tt>:within</tt>:: valid range of length
# <tt>:minimum</tt>:: maximum length
# <tt>:maximum</tt>:: minimum length
#
# If both, <tt>:minimum</tt> and <tt>:maximum</tt> are provided they're
# converted to :within. Each boundary type has its own default message:
# precise:: "Length of %s is not equal to #{__length}."
# range:: "Length of %s is not within #{__range.first} and #{__range.last}."
# lower:: "Length of %s is smaller than #{__boundary}."
# upper:: "Length of %s is greater than #{__boundary}."
#
# <b>Example:</b>
#
# obj = %w[a sentence with five words] #
# def obj.errors() @errors ||= NotNauthy::Errors.new end
#
# LengthValidation.new({:minimum => 4}, :to_a).
# call obj, :to_a, %w[a sentence with five words]
# obj.errors.on(:to_s).any? # => false
#
# LengthValidation.new({:within => 1..4}, :to_a).
# call obj, :to_a, %w[a sentence with five words]
# obj.errors.on(:to_s).any? # => true
class LengthValidation < Validation

def initialize(opts, attributes)
def initialize(opts, attributes) #:nodoc:

block = build_block opts

Expand All @@ -17,7 +53,7 @@ def initialize(opts, attributes)
end

protected
def build_block(opts)
def build_block(opts) #:nodoc:
if __length = opts[:is]
__message = opts[:message] ||
"Length of %s is not equal to #{__length}."
Expand Down
@@ -1,7 +1,33 @@
NotNaughty::Validation.load :format

module NotNaughty

# == Validates numericality of obj's attribute via an regular expression.
#
# Unless the validation succeeds an error hash (:attribute => :message)
# is added to the obj's instance of Errors.
#
# <b>Options:</b>
# <tt>:only_integer</tt>:: validates with <tt>/^[+-]?\d+$/</tt> (false)
# <tt>:message</tt>:: see NotNaughty::Errors for details
# <tt>:if</tt>:: see NotNaughty::Validation::Condition for details
# <tt>:unless</tt>:: see NotNaughty::Validation::Condition for details
#
# <b>Example:</b>
#
# obj = '-12.2' #
# def obj.errors() @errors ||= NotNauthy::Errors.new end
#
# NumericalityValidation.new({}, :to_s).call obj, :to_s, '-12.2'
# obj.errors.on(:to_s).any? # => false
#
# NumericalityValidation.new({:only_integer => true}, :to_s).
# call obj, :to_s, '-12.2'
#
# obj.errors.on(:to_s).any? # => true
class NumericalityValidation < FormatValidation

def initialize(opts, attributes)
def initialize(opts, attributes) #:nodoc:
opts[:with] = if opts[:only_integer]
opts[:message] ||= '#{"%s".humanize} is not an integer.'
/^[+-]?\d+$/
Expand Down
@@ -1,7 +1,25 @@
module NotNaughty

# == Validates presence of obj's attribute via the <tt>:blank?</tt> method.
#
# Unless the validation succeeds an error hash (:attribute => :message)
# is added to the obj's instance of Errors.
#
# <b>Options:</b>
# <tt>:message</tt>:: see NotNaughty::Errors for details
# <tt>:if</tt>:: see NotNaughty::Validation::Condition for details
# <tt>:unless</tt>:: see NotNaughty::Validation::Condition for details
#
# <b>Example:</b>
#
# obj = '' # blank? => true
# def obj.errors() @errors ||= NotNauthy::Errors.new end
#
# PresenceValidation.new({}, :to_s).call obj, :to_s, ''
# obj.errors.on(:to_s) # => ["To s is not present."]
class PresenceValidation < Validation

def initialize(opts, attributes)
def initialize(opts, attributes) #:nodoc:
__message = opts[:message] || '#{"%s".humanize} is not present.'

super opts, attributes do |o, a, v|
Expand Down

0 comments on commit 1fbd837

Please sign in to comment.