public this repo is viewable by everyone
Description: Extras for DataMapper, including bridges to DataObjects::Migrations and Merb::DataMapper
Homepage: http://datamapper.org
Clone URL: git://github.com/sam/dm-more.git
Renamed validators following more logical convention

* Renaming of validator methods:
 => validates_present
 => validates_absent
 => validates_is_confirmed
 => validates_is_accepted
 => validates_format
 => validates_length
 => validates_within
 => validates_is_number
 => validates_with_method
 => validates_is_unique

* Miscellaneous whitespace cleanup,
  formatting of comments. Added basic
  comments for validation methods that
  don't yet have them (needs to be
  expanded still).
myabc (author)
3 days ago
commit  1569d96fa9c302ce85b327c5d9fd6fc9c20b1756
tree    71280dcbcc2f3feb7f07a0816ee4af18b6fc457d
parent  f331f4c3fc6c52f69790feecfee0bb804a66296c
...
51
52
53
54
55
56
57
58
59
 
 
 
 
 
 
60
61
 
62
63
 
64
65
66
67
68
...
51
52
53
 
 
 
 
 
 
54
55
56
57
58
59
60
 
61
62
 
63
64
 
65
66
67
0
@@ -51,18 +51,17 @@ module DataMapper
0
       def self.included(base)
0
         base.extend(ClassMethods)
0
         base.class_eval do
0
- include DataMapper::Validate::ValidatesPresenceOf
0
- include DataMapper::Validate::ValidatesAbsenceOf
0
- include DataMapper::Validate::ValidatesConfirmationOf
0
- include DataMapper::Validate::ValidatesAcceptanceOf
0
- include DataMapper::Validate::ValidatesFormatOf
0
- include DataMapper::Validate::ValidatesLengthOf
0
+ include DataMapper::Validate::ValidatesPresent
0
+ include DataMapper::Validate::ValidatesAbsent
0
+ include DataMapper::Validate::ValidatesIsConfirmed
0
+ include DataMapper::Validate::ValidatesIsAccepted
0
+ include DataMapper::Validate::ValidatesFormat
0
+ include DataMapper::Validate::ValidatesLength
0
          include DataMapper::Validate::ValidatesWithin
0
- include DataMapper::Validate::ValidatesNumericalnesOf
0
+ include DataMapper::Validate::ValidatesIsNumber
0
          include DataMapper::Validate::ValidatesWithMethod
0
- include DataMapper::Validate::ValidatesUniquenessOf
0
+ include DataMapper::Validate::ValidatesIsUnique
0
          include DataMapper::Validate::AutoValidate
0
-
0
         end
0
       end
0
       
...
1
2
3
 
4
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
35
36
37
38
39
40
 
 
 
 
 
 
...
1
2
 
3
4
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
35
36
37
38
39
0
@@ -1,40 +1,39 @@
0
 module DataMapper
0
   module Validate
0
-
0
+
0
     class AbsentFieldValidator < GenericValidator
0
 
0
       def initialize(field_name, options={})
0
         super
0
         @field_name, @options = field_name, options
0
       end
0
-
0
+
0
       def call(target)
0
         field_value = target.attribute_get(field_name).blank?
0
         return true if field_value
0
-
0
+
0
         error_message = @options[:message] || "%s must be absent".t(DataMapper::Inflection.humanize(@field_name))
0
         add_error(target, error_message , @field_name)
0
-
0
+
0
         return false
0
- end
0
- end
0
-
0
- module ValidatesAbsenceOf
0
+ end
0
+ end # class AbsentFieldValidator
0
+
0
+ module ValidatesAbsent
0
       def self.included(base)
0
         base.extend(ClassMethods)
0
       end
0
-
0
+
0
       module ClassMethods
0
 
0
         # Validate the absence of a field
0
         #
0
- def validates_absence_of(*fields)
0
+ def validates_absent(*fields)
0
           opts = opts_from_validator_args(fields)
0
           add_validator_to_context(opts, fields, DataMapper::Validate::AbsentFieldValidator)
0
- end
0
- end
0
-
0
- end
0
-
0
- end
0
-end
0
+ end
0
+ end # module ClassMethods
0
+
0
+ end # module ValidatesAbsent
0
+ end # module Validate
0
+end # module DataMapper
...
6
7
8
9
 
10
11
12
...
15
16
17
18
 
19
20
21
22
23
24
25
 
26
27
28
 
29
30
31
...
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
 
 
 
 
 
...
6
7
8
 
9
10
11
12
...
15
16
17
 
18
19
20
21
22
23
24
 
25
26
27
 
28
29
30
31
...
33
34
35
 
 
36
 
 
37
38
39
40
41
42
 
43
44
45
 
46
47
48
49
50
51
 
 
 
 
 
 
 
 
 
52
53
54
55
56
0
@@ -6,7 +6,7 @@ module DataMapper
0
       def self.default_message_for_field(field_name)
0
         '%s is not accepted'.t(DataMapper::Inflection.humanize(field_name))
0
       end
0
-
0
+
0
       def initialize(field_name, options = {})
0
         super
0
         @options = options
0
@@ -15,17 +15,17 @@ module DataMapper
0
         @options[:accept] ||= ["1",1,"true",true,"t"]
0
         @options[:accept] = Array(@options[:accept])
0
       end
0
-
0
+
0
       def call(target)
0
         unless valid?(target)
0
           error_message = @options[:message] || DataMapper::Validate::AcceptanceValidator.default_message_for_field(@field_name)
0
           add_error(target, error_message , @field_name)
0
           return false
0
         end
0
-
0
+
0
         return true
0
       end
0
-
0
+
0
       def valid?(target)
0
         field_value = target.instance_variable_get("@#{@field_name}")
0
         return true if @options[:allow_nil] && field_value.nil?
0
@@ -33,27 +33,24 @@ module DataMapper
0
 
0
         @options[:accept].include?(field_value)
0
       end
0
-
0
- end
0
 
0
-
0
- module ValidatesAcceptanceOf
0
+ end # class AcceptanceValidator
0
+
0
+ module ValidatesIsAccepted
0
       def self.included(base)
0
         base.extend(ClassMethods)
0
       end
0
-
0
+
0
       module ClassMethods
0
         
0
- def validates_acceptance_of(*fields)
0
+ # Validates a field where another field must be accepted
0
+ #
0
+ def validates_is_accepted(*fields)
0
           opts = opts_from_validator_args(fields)
0
           add_validator_to_context(opts, fields, DataMapper::Validate::AcceptanceValidator)
0
         end
0
- end
0
-
0
- end
0
-
0
-
0
-
0
-
0
- end
0
-end
0
+ end # module ClassMethods
0
+
0
+ end # module ValidatesIsAccepted
0
+ end # module Validate
0
+end # module DataMapper
...
1
2
 
 
3
4
 
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
 
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
...
69
70
71
72
 
73
74
 
75
76
77
78
79
80
81
82
 
 
 
 
 
...
 
 
1
2
3
 
4
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
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
...
68
69
70
 
71
72
 
73
74
75
76
77
 
 
 
 
78
79
80
81
82
0
@@ -1,66 +1,65 @@
0
-module DataMapper
0
- module Validate
0
+module DataMapper
0
+ module Validate
0
     module AutoValidate
0
-
0
+
0
       def self.included(base)
0
         base.extend(ClassMethods)
0
       end
0
-
0
- module ClassMethods
0
-
0
- # Auto generate validations for a given property. This will only occur
0
- # if the option :auto_validations is true or undefined.
0
- #
0
+
0
+ module ClassMethods
0
+
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
         #
0
- # :nullable => false
0
- # Setting the option :nullable to false causes a validates_presence_of
0
- # validator to be automatically created on the property
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
- # Setting the option :size or :length causes a validates_length_of
0
- # validator to be automatically created on the property. If the value
0
- # is a Fixnum the validation will set :maximum => value if the value
0
- # is a Range the validation will set :within => value
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 Fixnum 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
- # Setting the :format option causes a validates_format_of validatior
0
- # to be automatically created on the property
0
- #
0
+ # Setting the :format option causes a validates_format_of
0
+ # validator to be automatically created on the property
0
         #
0
         def auto_generate_validations(property)
0
           property.options[:auto_validation] = true unless property.options.has_key?(:auto_validation)
0
           return unless property.options[:auto_validation]
0
-
0
+
0
           opts = {}
0
           opts[:context] = property.options[:validates] if property.options.has_key?(:validates)
0
-
0
+
0
           # presence
0
           if property.options.has_key?(:nullable) && !property.options[:nullable]
0
- validates_presence_of property.name, opts
0
+ validates_present property.name, opts
0
           end
0
-
0
-
0
+
0
           # length
0
           if property.type == String
0
             if property.options.has_key?(:length) || property.options.has_key?(:size)
0
               len = property.options.has_key?(:length) ? property.options[:length] : property.options[:size]
0
               opts[:within] = len if len.is_a?(Range)
0
- opts[:maximum] = len unless len.is_a?(Range)
0
- opts[:allow_nil] = property.options[:nullable] if property.options.has_key?(:nullable)
0
- validates_length_of property.name, opts
0
+ opts[:maximum] = len unless len.is_a?(Range)
0
+ opts[:allow_nil] = property.options[:nullable] if property.options.has_key?(:nullable)
0
+ validates_length property.name, opts
0
             else
0
               opts[:maximum] = 50 #default string size
0
- validates_length_of property.name, opts
0
+ validates_length property.name, opts
0
             end
0
           end
0
-
0
+
0
           # format
0
           if property.options.has_key?(:format)
0
             opts[:with] = property.options[:format]
0
- validates_format_of property.name, opts
0
+ validates_format property.name, opts
0
           end
0
-
0
+
0
           # uniqueness validator
0
           if property.options.has_key?(:unique)
0
             value = property.options[:unique]
0
@@ -69,14 +68,15 @@ module DataMapper
0
             scope << value if value.is_a?(Symbol)
0
             opts = {}
0
             opts[:scope] = scope if scope.length > 0
0
-
0
+
0
             if value.is_a?(TrueClass) || scope.length > 0
0
- validates_uniqueness_of property.name, opts
0
+ validates_is_unique property.name, opts
0
             end
0
           end
0
 
0
         end
0
- end
0
- end
0
- end
0
-end
0
+ end # module ClassMethods
0
+
0
+ end # module AutoValidate
0
+ end # module Validate
0
+end # module DataMapper
...
1
2
3
 
4
5
 
6
7
8
9
10
11
12
 
13
14
15
16
17
18
19
 
20
21
22
 
23
24
25
...
28
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
 
 
 
 
 
...
1
2
 
3
4
 
5
6
7
8
9
10
11
 
12
13
14
15
16
17
18
 
19
20
21
 
22
23
24
25
...
28
29
30
 
 
31
 
 
32
33
34
35
36
37
 
38
39
 
40
41
42
43
44
45
46
47
 
 
 
 
 
 
 
 
 
48
49
50
51
52
0
@@ -1,25 +1,25 @@
0
 module DataMapper
0
   module Validate
0
-
0
+
0
     class ConfirmationValidator < GenericValidator
0
-
0
+
0
       def initialize(field_name, options = {})
0
         super
0
         @options = options
0
         @field_name, @confirm_field_name = field_name, (options[:confirm] || "#{field_name}_confirmation").to_sym
0
         @options[:allow_nil] = true unless @options.has_key?(:allow_nil)
0
       end
0
-
0
+
0
       def call(target)
0
         unless valid?(target)
0
           error_message = @options[:message] || '%s does not match the confirmation'.t(DataMapper::Inflection.humanize(@field_name))
0
           add_error(target, error_message , @field_name)
0
           return false
0
         end
0
-
0
+
0
         return true
0
       end
0
-
0
+
0
       def valid?(target)
0
         field_value = target.instance_variable_get("@#{@field_name}")
0
         return true if @options[:allow_nil] && field_value.nil?
0
@@ -28,26 +28,25 @@ module DataMapper
0
         confirm_value = target.instance_variable_get("@#{@confirm_field_name}")
0
         field_value == confirm_value
0
       end
0
-
0
- end
0
 
0
-
0
- module ValidatesConfirmationOf
0
+ end # class ConfirmationValidator
0
+
0
+ module ValidatesIsConfirmed
0
       def self.included(base)
0
         base.extend(ClassMethods)
0
       end
0
-
0
+
0
       module ClassMethods
0
- def validates_confirmation_of(*fields)
0
+
0
+ # Validates a field when it is confirmed by another field with the same
0
+ # value
0
+ #
0
+ def validates_is_confirmed(*fields)
0
           opts = opts_from_validator_args(fields)
0
           add_validator_to_context(opts, fields, DataMapper::Validate::ConfirmationValidator)
0
         end
0
- end
0
-
0
- end
0
-
0
-
0
-
0
-
0
- end
0
-end
0
+ end # module ClassMethods
0
+
0
+ end # module ValidatesIsConfirmed
0
+ end # module Validate
0
+end # module DataMapper
...
1
2
 
 
3
4
5
 
6
7
8
9
10
11
 
12
13
14
...
17
18
19
20
 
21
22
 
23
24
25
26
27
28
 
29
30
31
...
53
54
55
56
57
58
 
 
 
...
 
 
1
2
3
 
 
4
5
6
7
8
9
 
10
11
12
13
...
16
17
18
 
19
20
 
21
22
23
24
25
 
 
26
27
28
29
...
51
52
53
 
 
 
54
55
56
0
@@ -1,14 +1,13 @@
0
-module DataMapper
0
- module Validate
0
+module DataMapper
0
+ module Validate
0
     class ContextualValidators
0
-
0
-
0
+
0
       def dump
0
         contexts.each_pair do |key,context|
0
           puts "Key=#{key} Context: #{context}"
0
         end
0
       end
0
-
0
+
0
       # Get a hash of named context validators for the resource
0
       #
0
       # ==== Returns
0
@@ -17,15 +16,14 @@ module DataMapper
0
       def contexts
0
         @contexts ||= @contexts = {}
0
       end
0
-
0
+
0
       # Return an array of validators for a named context
0
- #
0
+ #
0
       def context(name)
0
         contexts[name] = [] unless contexts.has_key?(name)
0
         contexts[name]
0
       end
0
-
0
-
0
+
0
       # Clear all named context validators off of the resource
0
       #
0
       def clear!
0
@@ -53,6 +51,6 @@ module DataMapper
0
         return result
0
       end
0
 
0
- end
0
- end
0
-end
0
+ end # module ContextualValidators
0
+ end # module Validate
0
+end # module DataMapper
...
2
3
4
5
 
6
7
8
 
9
10
11
...
15
16
17
18
 
19
20
21
22
 
 
23
24
25
...
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
 
 
 
 
 
...
2
3
4
 
5
6
 
 
7
8
9
10
...
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
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
0
@@ -2,10 +2,9 @@
0
 
0
 module DataMapper
0
   module Validate
0
-
0
+
0
     class CustomValidator < GenericValidator
0
-
0
-
0
+
0
       def initialize(field_name, options = {}, &b)
0
         #super(field_name, options)
0
         #@field_name, @options = field_name, options
0
@@ -15,11 +14,11 @@ module DataMapper
0
       def call(target)
0
         #field_value = target.instance_variable_get("@#{@field_name}")
0
         #return true if @options[:allow_nil] && field_value.nil?
0
-
0
+
0
         #validation = (@options[:as] || @options[:with])
0
         #error_message = nil
0
-
0
- # Figure out what to use as the actual validator.
0
+
0
+ # Figure out what to use as the actual validator.
0
         # If a symbol is passed to :as, look up
0
         # the canned validation in FORMATS.
0
         #
0
@@ -33,42 +32,42 @@ module DataMapper
0
         #else
0
         # validation
0
         #end
0
-
0
+
0
         #valid = case validator
0
         #when Proc then validator.call(field_value)
0
         #when Regexp then validator =~ field_value
0
         #else raise UnknownValidationFormat, "Can't determine how to validate #{target.class}##{@field_name} with #{validator.inspect}"
0
- #end
0
-
0
+ #end
0
+
0
         #unless valid
0
         # field = Inflector.humanize(@field_name)
0
         # value = field_value
0
- #
0
+ #
0
         # error_message = @options[:message] || error_message || '%s is invalid'.t(field)
0
         # error_message = error_message.call(field, value) if Proc === error_message
0
- #
0
+ #
0
         # add_error(target, error_message , @field_name)
0
         #end
0
-
0
+
0
         #return valid
0
       end
0
-
0
+
0
       #class UnknownValidationFormat < StandardError; end
0
-
0
- end
0
-
0
+
0
+ end # class CustomValidator
0
+
0
     module ValidatesFormatOf
0
       def self.included(base)
0
         base.extend(ClassMethods)
0
       end
0
-
0
+
0
       module ClassMethods
0
         #def validates_format_of(*fields)
0
           #opts = opts_from_validator_args(fields)
0
           #add_validator_to_context(opts, fields, DataMapper::Validate::FormatValidator)
0
         #end
0
- end
0
- end
0
-
0
- end
0
-end
0
+ end # module ClassMethods
0
+
0
+ end # module ValidatesFormatOf
0
+ end # module Validate
0
+end # module DataMapper
...
5
6
7
8
 
9
10
 
11
12
13
14
15
 
16
17
18
...
20
21
22
23
 
24
25
 
26
27
28
 
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
 
 
 
 
 
...
5
6
7
 
8
9
 
10
11
12
 
 
 
13
14
15
16
...
18
19
20
 
21
22
 
23
24
25
 
26
27
 
28
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
0
@@ -5,14 +5,12 @@ require Pathname(__FILE__).dirname.expand_path + 'formats/email'
0
 
0
 module DataMapper
0
   module Validate
0
-
0
+
0
     class FormatValidator < GenericValidator
0
-
0
+
0
       FORMATS = {}
0
       include DataMapper::Validate::Format::Email
0
-
0
-
0
-
0
+
0
       def initialize(field_name, options = {}, &b)
0
         super(field_name, options)
0
         @field_name, @options = field_name, options
0
@@ -20,48 +18,51 @@ module DataMapper
0
       end
0
 
0
       def call(target)
0
- value = target.validation_property_value(@field_name)
0
+ value = target.validation_property_value(@field_name)
0
         return true if @options[:allow_nil] && value.nil?
0
-
0
+
0
         validation = (@options[:as] || @options[:with])
0
 
0
- raise "No such predefined format '#{validation}'" if validation.is_a?(Symbol) && !FORMATS.has_key?(validation)
0
+ raise "No such predefined format '#{validation}'" if validation.is_a?(Symbol) && !FORMATS.has_key?(validation)
0
         validator = validation.is_a?(Symbol) ? FORMATS[validation][0] : validation
0
-
0
+
0
         field = DataMapper::Inflection.humanize(@field_name)
0
- error_message = @options[:message] || '%s has an invalid format'.t(field)
0
-
0
+ error_message = @options[:message] || '%s has an invalid format'.t(field)
0
+
0
         valid = case validator
0
- when Proc then validator.call(value)
0
+ when Proc then validator.call(value)
0
         when Regexp then validator =~ value
0
         else raise UnknownValidationFormat, "Can't determine how to validate #{target.class}##{@field_name} with #{validator.inspect}"
0
- end
0
-
0
- unless valid
0
+ end
0
+
0
+ unless valid
0
           error_message = @options[:message] || error_message || '%s is invalid'.t(field)
0
- error_message = error_message.call(field, value) if Proc === error_message
0
+ error_message = error_message.call(field, value) if Proc === error_message
0
           add_error(target, error_message , @field_name)
0
         end
0
-
0
+
0
         return valid
0
       end
0
-
0
+
0
       #class UnknownValidationFormat < StandardError; end
0
-
0
- end
0
-
0
- module ValidatesFormatOf
0
+
0
+ end # class FormatValidator
0
+
0
+ module ValidatesFormat
0
       def self.included(base)
0
         base.extend(ClassMethods)
0
       end
0
-
0
+
0
       module ClassMethods
0
- def validates_format_of(*fields)
0
+
0
+ # Validates the format of a field
0
+ #
0
+ def validates_format(*fields)
0
           opts = opts_from_validator_args(fields)
0
           add_validator_to_context(opts, fields, DataMapper::Validate::FormatValidator)
0
         end
0
- end
0
- end
0
-
0
- end
0
-end
0
+ end # module ClassMethods
0
+
0
+ end # module ValidatesFormat
0
+ end # module Validate
0
+end # module DataMapper
...
1
2
3
 
4
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
...
31
32
33
34
 
35
36
37
...
42
43
44
45
 
46
47
48
 
 
49
50
51
52
 
53
54
55
...
60
61
62
63
 
64
65
 
66
67
68
69
70
71
 
72
73
74
...
77
78
79
80
81
82
83
84
 
 
 
 
 
...
1
2
 
3
4
 
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
...
31
32
33
 
34
35
36
37
...
42
43
44
 
45
46
 
 
47
48
49
50
51
 
52
53
54
55
...
60
61
62
 
63
64
 
65
66
67
68
69
70
 
71
72
73
74
...
77
78
79
 
 
 
 
 
80
81
82
83
84
0
@@ -1,29 +1,29 @@
0
 module DataMapper
0
   module Validate
0
-
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
+ # must implement the abstract method call where the actual validation
0
     # occures
0