public
Fork of sam/dm-more
Description: Extras for DataMapper, including bridges to DataObjects::Migrations and Merb::DataMapper
Homepage: http://datamapper.org
Clone URL: git://github.com/myabc/dm-more.git
YARD conversion for dm-validations plugin

* Initial work to convert to YARD documentation
  standard. Documentation still needs further
  expansion.
* Some spelling, whitespace fixes.
myabc (author)
Sun May 18 03:25:22 -0700 2008
commit  016229a324e20da48cd7e032ba5e8745130a38fd
tree    8d7743fc2315d9cb07333cdde1bcf041f0b74acd
parent  ca892b7b78586260330713774216f7a04764ad6b
...
1
2
 
 
3
4
5
 
6
7
8
 
9
10
11
12
13
14
15
 
 
16
17
18
...
29
30
31
32
33
 
 
34
35
36
37
38
39
 
 
 
 
40
41
42
43
44
45
46
 
 
 
47
48
49
50
51
52
 
 
 
 
 
53
54
55
56
57
 
58
59
60
61
62
63
64
 
 
65
66
67
68
69
70
 
 
 
71
72
73
 
...
 
 
1
2
3
4
 
5
6
7
 
8
9
10
11
12
13
 
 
14
15
16
17
18
...
29
30
31
 
 
32
33
34
35
 
 
 
 
36
37
38
39
40
41
42
43
 
 
 
44
45
46
47
48
 
 
 
 
49
50
51
52
53
54
55
56
57
 
58
59
60
61
62
63
 
 
64
65
66
67
68
 
 
 
69
70
71
72
 
73
74
0
@@ -1,18 +1,18 @@
0
-This is a DataMapper plugin that provides validations for DataMapper
0
-model classes.
0
+This is a DataMapper plugin that provides validations for DataMapper model
0
+classes.
0
 
0
 == Setup
0
-DataMapper validation capabilities are automatically available when you
0
+DataMapper validation capabilities are automatically available when you
0
 'require dm-validations' into your application.
0
 
0
-More specifically, DataMapper::Validate is automatically included into
0
+More specifically, DataMapper::Validate is automatically included into
0
 DataMapper::Resource when you require dm-validations.
0
 
0
 == Specifying Model Validations
0
 There are two primary ways to implement validations for your models:
0
 
0
-1) Placing validation methods with properties as params in your class
0
- definitions like:
0
+1) Placing validation methods with properties as params in your class
0
+ definitions like:
0
    - validates_length :name
0
    - validates_length [:name, :description]
0
 
0
@@ -29,44 +29,45 @@ An example class with validations declared:
0
     validates_length :name
0
   end
0
 
0
-See all of the DataMapper::Validate module's XYZValidator(s) to learn
0
-about the complete collections of validators available to you
0
+See all of the DataMapper::Validate module's XYZValidator(s) to learn about the
0
+complete collections of validators available to you.
0
 
0
 == Validating
0
-DataMapper validations, when included, do not alter the default
0
-save/create/update process for a model. In order to ensure that your
0
-model is valid before saving, the simplest approach is to call
0
-valid? on the model before trying to save. For example:
0
+DataMapper validations, when included, do not alter the default
0
+save/create/update process for a model. In order to ensure that your model is
0
+valid before saving, the simplest approach is to call valid? on the model before
0
+trying to save. For example:
0
 
0
   my_account = Account.new(:name => "Jose")
0
   my_account.save if my_account.valid?
0
 
0
-In addition to the valid? method, there is also an all_valid? method
0
-that recursively walks both the current object and its associated
0
-objects and returns its true/false result for the entire walk.
0
+In addition to the valid? method, there is also an all_valid? method that
0
+recursively walks both the current object and its associated objects and returns
0
+its true/false result for the entire walk.
0
 
0
 == Working with Validation Errors
0
-If your validators find errors in your model, they will populate
0
-the DataMapper::Validate::ValidationErrors object that is available
0
-through each of your models via calls to your model's errors method.
0
-For example:
0
+If your validators find errors in your model, they will populate the
0
+DataMapper::Validate::ValidationErrors object that is available through each of
0
+your models via calls to your model's errors method.
0
+
0
+For example:
0
 
0
   my_account = Account.new(:name => "Jose")
0
   if my_account.valid?
0
     my_account.save
0
- else
0
+ else
0
     my_account.errors.each do |e|
0
       puts e
0
     end
0
   end
0
 
0
-See DataMapper::Validate::ValidationErrors for all you can do
0
-with your model's errors method.
0
+See DataMapper::Validate::ValidationErrors for all you can do with your model's
0
+errors method.
0
 
0
 == Contextual Validations
0
 
0
-DataMapper Validations also provide a means of grouping your
0
-validations into contexts. This enables you to run different
0
-sets of validations under ... different contexts.
0
+DataMapper Validations also provide a means of grouping your validations into
0
+contexts. This enables you to run different sets of validations under ...
0
+different contexts.
0
 
0
-TO BE ADDED... For now, see
0
\ No newline at end of file
0
+TO BE ADDED... For now, see
...
1
2
3
 
 
 
 
4
5
6
...
1
2
3
4
5
6
7
8
9
10
0
@@ -1,6 +1,10 @@
0
 module DataMapper
0
   module Validate
0
 
0
+ ##
0
+ #
0
+ # @author Guy van den Berg
0
+ # @since 0.9
0
     class AbsentFieldValidator < GenericValidator
0
 
0
       def initialize(field_name, options={})
...
1
2
3
 
 
 
 
4
5
6
...
1
2
3
4
5
6
7
8
9
10
0
@@ -1,6 +1,10 @@
0
 module DataMapper
0
   module Validate
0
 
0
+ ##
0
+ #
0
+ # @author Martin Kihlgren
0
+ # @since 0.9
0
     class AcceptanceValidator < GenericValidator
0
 
0
       def self.default_message_for_field(field_name)
...
5
6
7
8
 
 
9
10
 
11
12
13
14
15
 
16
17
18
19
20
21
 
22
23
24
25
 
26
27
28
29
30
 
31
32
33
...
5
6
7
 
8
9
10
 
11
12
13
14
15
 
16
17
18
19
20
21
 
22
23
24
25
 
26
27
28
29
30
 
31
32
33
34
0
@@ -5,29 +5,30 @@ module DataMapper
0
       # Auto-generate validations for a given property. This will only occur
0
       # if the option :auto_validation is either true or left undefined.
0
       #
0
- # ==== Triggers that generate validator creation
0
+ # @details [Triggers]
0
+ # Triggers that generate validator creation
0
       #
0
- # :nullable => false
0
+ # :nullable => false
0
       # Setting the option :nullable to false causes a
0
       # validates_presence_of validator to be automatically created on
0
       # the property
0
       #
0
- # :size => 20 or :length => 20
0
+ # :size => 20 or :length => 20
0
       # Setting the option :size or :length causes a validates_length_of
0
       # validator to be automatically created on the property. If the
0
       # value is a Integer the validation will set :maximum => value if
0
       # the value is a Range the validation will set :within => value
0
       #
0
- # :format => :predefined / lambda / Proc
0
+ # :format => :predefined / lambda / Proc
0
       # Setting the :format option causes a validates_format_of
0
       # validator to be automatically created on the property
0
       #
0
- # Integer type
0
+ # Integer type
0
       # Using a Integer type causes a validates_is_number
0
       # validator to be created for the property. integer_only
0
       # is set to true
0
       #
0
- # BigDecimal or Float type
0
+ # BigDecimal or Float type
0
       # Using a Integer type causes a validates_is_number
0
       # validator to be created for the property. integer_only
0
       # is set to false, and precision/scale match the property
...
1
2
3
 
 
 
 
4
5
6
...
1
2
3
4
5
6
7
8
9
10
0
@@ -1,6 +1,10 @@
0
 module DataMapper
0
   module Validate
0
 
0
+ ##
0
+ #
0
+ # @author Guy van den Berg
0
+ # @since 0.9
0
     class ConfirmationValidator < GenericValidator
0
 
0
       def initialize(field_name, options = {})
...
1
2
 
 
 
 
 
3
4
5
...
10
11
12
13
14
15
 
16
17
18
19
20
21
 
22
23
24
...
32
33
34
35
36
37
38
39
40
41
42
 
 
 
43
44
45
...
1
2
3
4
5
6
7
8
9
10
...
15
16
17
 
 
 
18
19
20
21
22
23
24
25
26
27
28
...
36
37
38
 
 
 
 
 
 
 
 
39
40
41
42
43
44
0
@@ -1,5 +1,10 @@
0
 module DataMapper
0
   module Validate
0
+
0
+ ##
0
+ #
0
+ # @author Guy van den Berg
0
+ # @since 0.9
0
     class ContextualValidators
0
 
0
       def dump
0
@@ -10,15 +15,14 @@ module DataMapper
0
 
0
       # Get a hash of named context validators for the resource
0
       #
0
- # ==== Returns
0
- # <Hash>:: a hash of validators <GenericValidator>
0
- #
0
+ # @return <Hash> a hash of validators <GenericValidator>
0
       def contexts
0
         @contexts ||= @contexts = {}
0
       end
0
 
0
       # Return an array of validators for a named context
0
       #
0
+ # @return <Array> An array of validators
0
       def context(name)
0
         contexts[name] = [] unless contexts.has_key?(name)
0
         contexts[name]
0
@@ -32,14 +36,9 @@ module DataMapper
0
 
0
       # Execute all validators in the named context against the target
0
       #
0
- # ==== Parameters
0
- #
0
- # named_context<Symbol>:: - the context we are validating against
0
- # target<Object>:: - the resource that we are validating
0
- #
0
- # ==== Return Value
0
- # <Boolean>:: - true if all is valid otherwise false
0
- #
0
+ # @param <Symbol> named_context the context we are validating against
0
+ # @param <Object> target the resource that we are validating
0
+ # @return <Boolean> true if all are valid, otherwise false
0
       def execute(named_context, target)
0
         target.errors.clear!
0
         result = true
...
3
4
5
 
 
 
 
6
7
8
...
3
4
5
6
7
8
9
10
11
12
0
@@ -3,6 +3,10 @@
0
 module DataMapper
0
   module Validate
0
 
0
+ ##
0
+ #
0
+ # @author Guy van den Berg
0
+ # @since 0.9
0
     class CustomValidator < GenericValidator
0
 
0
       def initialize(field_name, options = {}, &b)
...
6
7
8
 
 
 
 
9
10
11
...
6
7
8
9
10
11
12
13
14
15
0
@@ -6,6 +6,10 @@ require Pathname(__FILE__).dirname.expand_path + 'formats/email'
0
 module DataMapper
0
   module Validate
0
 
0
+ ##
0
+ #
0
+ # @author Guy van den Berg
0
+ # @since 0.9
0
     class FormatValidator < GenericValidator
0
 
0
       FORMATS = {}
...
1
2
3
4
5
6
 
 
7
 
8
9
10
...
17
18
19
20
21
22
 
 
 
23
24
25
26
27
 
 
 
28
29
30
...
33
34
35
36
37
38
39
40
41
42
 
43
 
 
 
44
45
46
...
52
53
54
55
56
57
58
59
60
 
 
61
62
63
...
1
2
3
 
 
 
4
5
6
7
8
9
10
...
17
18
19
 
 
 
20
21
22
23
 
 
 
 
24
25
26
27
28
29
...
32
33
34
 
 
 
 
 
 
 
35
36
37
38
39
40
41
42
...
48
49
50
 
 
 
 
 
 
51
52
53
54
55
0
@@ -1,10 +1,10 @@
0
 module DataMapper
0
   module Validate
0
 
0
- # A base class that all validators must be derived from. Child classes
0
- # must implement the abstract method call where the actual validation
0
- # occures
0
+ # A base class that all Validators must be derived from. Child classes must
0
+ # implement the abstract method call where the actual validation occurs.
0
     #
0
+ # @author Guy van den Berg
0
     class GenericValidator
0
 
0
       attr_accessor :if_clause
0
@@ -17,14 +17,13 @@ module DataMapper
0
         @unless_clause = opts.has_key?(:unless) ? opts[:unless] : nil
0
       end
0
 
0
- # Add an error message to a target resource. If the error corrosponds
0
- # to a specific field of the resource, add it to that field, otherwise
0
- # add it as a :general message
0
+ # Add an error message to a target resource. If the error corresponds to a
0
+ # specific field of the resource, add it to that field, otherwise add it
0
+ # as a :general message.
0
       #
0
- # ==== Parameters
0
- # target<Object>:: the resource that has the error
0
- # message<String>:: a string message to add
0
- # field_name<Symbol> the name of the field that caused the error
0
+ # @param <Object> target the resource that has the error
0
+ # @param <String> message the message to add
0
+ # @param <Symbol> field_name the name of the field that caused the error
0
       #
0
       # TODO - should the field_name for a general message be :default???
0
       #
0
@@ -33,14 +32,11 @@ module DataMapper
0
       end
0
 
0
       # Call the validator. "call" is used so the operation is BoundMethod and
0
- # Block compatible. This must be implemented in all concreate classes
0
- #
0
- # ==== Parameters
0
- # target<Object>:: the resource that the validator must be called against"
0
- #
0
- # ==== Returns
0
- # <Boolean>:: TRUE if valid otherwise FALSE
0
+ # Block compatible. This must be implemented in all concrete classes.
0
       #
0
+ # @param <Object> target the resource that the validator must be called
0
+ # against
0
+ # @return <Boolean> TRUE if valid, otherwise FALSE
0
       def call(target)
0
         raise "DataMapper::Validate::GenericValidator::call must be overriden in #{self.class.to_s}"
0
       end
0
@@ -52,12 +48,8 @@ module DataMapper
0
 
0
       # Determine if this validator should be run against the target
0
       #
0
- # ==== Parameters
0
- # target<Object>:: the resource that we check against
0
- #
0
- # ==== Returns
0
- # <Boolean>:: TRUE if should be run otherwise FALSE
0
- #
0
+ # @param <Object> target the resource that we check against
0
+ # @return <Boolean> TRUE if should be run, otherwise FALSE
0
       def execute?(target)
0
         return true if self.if_clause.nil? && self.unless_clause.nil?
0
 
...
1
2
3
 
 
 
 
4
5
6
...
25
26
27
28
 
 
29
30
31
...
1
2
3
4
5
6
7
8
9
10
...
29
30
31
 
32
33
34
35
36
0
@@ -1,6 +1,10 @@
0
 module DataMapper
0
   module Validate
0
 
0
+ ##
0
+ #
0
+ # @author Guy van den Berg
0
+ # @since 0.9
0
     class LengthValidator < GenericValidator
0
 
0
       def initialize(field_name, options)
0
@@ -25,7 +29,8 @@ module DataMapper
0
 
0
         field_value = '' if field_value.nil?
0
 
0
- # HACK seems hacky to do this on every validation, probably should do this elsewhere?
0
+ # XXX: HACK seems hacky to do this on every validation, probably should
0
+ # do this elsewhere?
0
         field = DataMapper::Inflection.humanize(@field_name)
0
         min = @range ? @range.min : @min
0
         max = @range ? @range.max : @max
...
1
2
3
 
 
 
 
4
5
6
...
1
2
3
4
5
6
7
8
9
10
0
@@ -1,6 +1,10 @@
0
 module DataMapper
0
   module Validate
0
 
0
+ ##
0
+ #
0
+ # @author Guy van den Berg
0
+ # @since 0.9
0
     class MethodValidator < GenericValidator
0
 
0
       def initialize(method_name, options={})
...
1
2
3
 
 
 
 
4
5
6
...
1
2
3
4
5
6
7
8
9
10
0
@@ -1,6 +1,10 @@
0
 module DataMapper
0
   module Validate
0
 
0
+ ##
0
+ #
0
+ # @author Guy van den Berg
0
+ # @since 0.9
0
     class NumericValidator < GenericValidator
0
 
0
       def initialize(field_name, options={})
...
1
2
3
 
 
 
 
4
5
6
...
1
2
3
4
5
6
7
8
9
10
0
@@ -1,6 +1,10 @@
0
 module DataMapper
0
   module Validate
0
 
0
+ ##
0
+ #
0
+ # @author Guy van den Berg
0
+ # @since 0.9
0
     class RequiredFieldValidator < GenericValidator
0
 
0
       def initialize(field_name, options={})
...
1
2
 
 
 
 
 
3
4
5
...
1
2
3
4
5
6
7
8
9
10
0
@@ -1,5 +1,10 @@
0
 module DataMapper
0
   module Validate
0
+
0
+ ##
0
+ #
0
+ # @author Guy van den Berg
0
+ # @since 0.9
0
     class UniquenessValidator < GenericValidator
0
 
0
       def initialize(field_name, options={})
...
1
2
 
 
 
 
 
3
4
5
...
11
12
13
14
15
 
 
16
17
18
19
 
 
20
21
22
...
30
31
32
33
34
 
35
36
37
...
1
2
3
4
5
6
7
8
9
10
...
16
17
18
 
 
19
20
21
 
 
 
22
23
24
25
26
...
34
35
36
 
 
37
38
39
40
0
@@ -1,5 +1,10 @@
0
 module DataMapper
0
   module Validate
0
+
0
+ ##
0
+ #
0
+ # @author Guy van den Berg
0
+ # @since 0.9
0
     class ValidationErrors
0
 
0
       def initialize
0
@@ -11,12 +16,11 @@ module DataMapper
0
         @errors.clear
0
       end
0
 
0
- # Add a validation error. Use the field_name :general if
0
- # the error doesn't apply to a specific field of the resource.
0
+ # Add a validation error. Use the field_name :general if the errors does
0
+ # not apply to a specific field of the Resource.
0
       #
0
- # ==== Parameters
0
- # field_name<Symbol>:: the name of the field that cause the error
0
- # message<String>:: the message to add
0
+ # @param <Symbol> field_name the name of the field that caused the error
0
+ # @param <String> message the message to add
0
       def add(field_name, message)
0
         @errors[field_name] << message
0
       end
0
@@ -30,8 +34,7 @@ module DataMapper
0
 
0
       # Return validation errors for a particular field_name.
0
       #
0
- # ==== Parameters
0
- # field_name<Symbol>:: the name of the field you want error on
0
+ # @param <Symbol> field_name the name of the field you want an error for
0
       def on(field_name)
0
         @errors[field_name].empty? ? nil : @errors[field_name]
0
       end
...
1
2
 
 
 
 
 
3
4
5
...
1
2
3
4
5
6
7
8
9
10
0
@@ -1,5 +1,10 @@
0
 module DataMapper
0
   module Validate
0
+
0
+ ##
0
+ #
0
+ # @author Guy van den Berg
0
+ # @since 0.9
0
     class WithinValidator < GenericValidator
0
 
0
       def initialize(field_name, options={})
...
1
2
3
4
 
5
6
7
...
9
10
11
12
 
13
14
15
...
1
2
3
 
4
5
6
7
...
9
10
11
 
12
13
14
15
0
@@ -1,7 +1,7 @@
0
 require 'pathname'
0
 require Pathname(__FILE__).dirname.expand_path + 'spec_helper'
0
 
0
-class Bill
0
+class Bill # :nodoc:
0
   include DataMapper::Resource
0
   property :id, Integer, :serial => true
0
   property :amount_1, String, :auto_validation => false
0
@@ -9,7 +9,7 @@ class Bill
0
   validates_is_number :amount_1, :amount_2
0
 end
0
 
0
-class Hillary
0
+class Hillary # :nodoc:
0
   include DataMapper::Resource
0
   property :id, Integer, :serial => true
0
   property :amount_1, Float, :auto_validation => false, :default => 0.01

Comments

    No one has commented yet.