couchrest_extended_document 1.0.0.beta5
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +176 -0
- data/README.md +68 -0
- data/Rakefile +68 -0
- data/THANKS.md +19 -0
- data/examples/model/example.rb +144 -0
- data/history.txt +159 -0
- data/lib/couchrest/casted_array.rb +25 -0
- data/lib/couchrest/casted_model.rb +55 -0
- data/lib/couchrest/extended_document.rb +323 -0
- data/lib/couchrest/mixins/attribute_protection.rb +74 -0
- data/lib/couchrest/mixins/callbacks.rb +532 -0
- data/lib/couchrest/mixins/class_proxy.rb +120 -0
- data/lib/couchrest/mixins/collection.rb +260 -0
- data/lib/couchrest/mixins/design_doc.rb +127 -0
- data/lib/couchrest/mixins/document_queries.rb +82 -0
- data/lib/couchrest/mixins/extended_attachments.rb +73 -0
- data/lib/couchrest/mixins/properties.rb +162 -0
- data/lib/couchrest/mixins/validation.rb +245 -0
- data/lib/couchrest/mixins/views.rb +148 -0
- data/lib/couchrest/mixins.rb +11 -0
- data/lib/couchrest/property.rb +50 -0
- data/lib/couchrest/support/couchrest.rb +19 -0
- data/lib/couchrest/support/rails.rb +42 -0
- data/lib/couchrest/typecast.rb +175 -0
- data/lib/couchrest/validation/auto_validate.rb +156 -0
- data/lib/couchrest/validation/contextual_validators.rb +78 -0
- data/lib/couchrest/validation/validation_errors.rb +125 -0
- data/lib/couchrest/validation/validators/absent_field_validator.rb +74 -0
- data/lib/couchrest/validation/validators/confirmation_validator.rb +107 -0
- data/lib/couchrest/validation/validators/format_validator.rb +122 -0
- data/lib/couchrest/validation/validators/formats/email.rb +66 -0
- data/lib/couchrest/validation/validators/formats/url.rb +43 -0
- data/lib/couchrest/validation/validators/generic_validator.rb +120 -0
- data/lib/couchrest/validation/validators/length_validator.rb +139 -0
- data/lib/couchrest/validation/validators/method_validator.rb +89 -0
- data/lib/couchrest/validation/validators/numeric_validator.rb +109 -0
- data/lib/couchrest/validation/validators/required_field_validator.rb +114 -0
- data/lib/couchrest/validation.rb +245 -0
- data/lib/couchrest_extended_document.rb +21 -0
- data/spec/couchrest/attribute_protection_spec.rb +150 -0
- data/spec/couchrest/casted_extended_doc_spec.rb +79 -0
- data/spec/couchrest/casted_model_spec.rb +406 -0
- data/spec/couchrest/extended_doc_attachment_spec.rb +148 -0
- data/spec/couchrest/extended_doc_inherited_spec.rb +40 -0
- data/spec/couchrest/extended_doc_spec.rb +868 -0
- data/spec/couchrest/extended_doc_subclass_spec.rb +99 -0
- data/spec/couchrest/extended_doc_view_spec.rb +529 -0
- data/spec/couchrest/property_spec.rb +648 -0
- data/spec/fixtures/attachments/README +3 -0
- data/spec/fixtures/attachments/couchdb.png +0 -0
- data/spec/fixtures/attachments/test.html +11 -0
- data/spec/fixtures/more/article.rb +35 -0
- data/spec/fixtures/more/card.rb +22 -0
- data/spec/fixtures/more/cat.rb +22 -0
- data/spec/fixtures/more/course.rb +25 -0
- data/spec/fixtures/more/event.rb +8 -0
- data/spec/fixtures/more/invoice.rb +17 -0
- data/spec/fixtures/more/person.rb +9 -0
- data/spec/fixtures/more/question.rb +6 -0
- data/spec/fixtures/more/service.rb +12 -0
- data/spec/fixtures/more/user.rb +22 -0
- data/spec/fixtures/views/lib.js +3 -0
- data/spec/fixtures/views/test_view/lib.js +3 -0
- data/spec/fixtures/views/test_view/only-map.js +4 -0
- data/spec/fixtures/views/test_view/test-map.js +3 -0
- data/spec/fixtures/views/test_view/test-reduce.js +3 -0
- data/spec/spec.opts +5 -0
- data/spec/spec_helper.rb +49 -0
- data/utils/remap.rb +27 -0
- data/utils/subset.rb +30 -0
- metadata +200 -0
@@ -0,0 +1,156 @@
|
|
1
|
+
# Ported from dm-migrations
|
2
|
+
|
3
|
+
module CouchRest
|
4
|
+
|
5
|
+
class Property
|
6
|
+
# flag letting us know if we already checked the autovalidation settings
|
7
|
+
attr_accessor :autovalidation_check
|
8
|
+
@autovalidation_check = false
|
9
|
+
end
|
10
|
+
|
11
|
+
module Validation
|
12
|
+
module AutoValidate
|
13
|
+
|
14
|
+
# # Force the auto validation for the class properties
|
15
|
+
# # This feature is still not fully ported over,
|
16
|
+
# # test are lacking, so please use with caution
|
17
|
+
# def auto_validate!
|
18
|
+
# auto_validation = true
|
19
|
+
# end
|
20
|
+
|
21
|
+
# adds message for validator
|
22
|
+
def options_with_message(base_options, property, validator_name)
|
23
|
+
options = base_options.clone
|
24
|
+
opts = property.options
|
25
|
+
options[:message] = if opts[:messages]
|
26
|
+
if opts[:messages].is_a?(Hash) and msg = opts[:messages][validator_name]
|
27
|
+
msg
|
28
|
+
else
|
29
|
+
nil
|
30
|
+
end
|
31
|
+
elsif opts[:message]
|
32
|
+
opts[:message]
|
33
|
+
else
|
34
|
+
nil
|
35
|
+
end
|
36
|
+
options
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
##
|
41
|
+
# Auto-generate validations for a given property. This will only occur
|
42
|
+
# if the option :auto_validation is either true or left undefined.
|
43
|
+
#
|
44
|
+
# @details [Triggers]
|
45
|
+
# Triggers that generate validator creation
|
46
|
+
#
|
47
|
+
# :nullable => false
|
48
|
+
# Setting the option :nullable to false causes a
|
49
|
+
# validates_presence_of validator to be automatically created on
|
50
|
+
# the property
|
51
|
+
#
|
52
|
+
# :size => 20 or :length => 20
|
53
|
+
# Setting the option :size or :length causes a validates_length_of
|
54
|
+
# validator to be automatically created on the property. If the
|
55
|
+
# value is a Integer the validation will set :maximum => value if
|
56
|
+
# the value is a Range the validation will set :within => value
|
57
|
+
#
|
58
|
+
# :format => :predefined / lambda / Proc
|
59
|
+
# Setting the :format option causes a validates_format_of
|
60
|
+
# validator to be automatically created on the property
|
61
|
+
#
|
62
|
+
# :set => ["foo", "bar", "baz"]
|
63
|
+
# Setting the :set option causes a validates_within
|
64
|
+
# validator to be automatically created on the property
|
65
|
+
#
|
66
|
+
# Integer type
|
67
|
+
# Using a Integer type causes a validates_numericality_of
|
68
|
+
# validator to be created for the property. integer_only
|
69
|
+
# is set to true
|
70
|
+
#
|
71
|
+
# Float type
|
72
|
+
# Using a Integer type causes a validates_is_number
|
73
|
+
# validator to be created for the property. integer_only
|
74
|
+
# is set to false, and precision/scale match the property
|
75
|
+
#
|
76
|
+
#
|
77
|
+
# Messages
|
78
|
+
#
|
79
|
+
# :messages => {..}
|
80
|
+
# Setting :messages hash replaces standard error messages
|
81
|
+
# with custom ones. For instance:
|
82
|
+
# :messages => {:presence => "Field is required",
|
83
|
+
# :format => "Field has invalid format"}
|
84
|
+
# Hash keys are: :presence, :format, :length, :is_unique,
|
85
|
+
# :is_number, :is_primitive
|
86
|
+
#
|
87
|
+
# :message => "Some message"
|
88
|
+
# It is just shortcut if only one validation option is set
|
89
|
+
#
|
90
|
+
def auto_generate_validations(property)
|
91
|
+
return unless ((property.autovalidation_check != true) && self.auto_validation)
|
92
|
+
return if (property.options && (property.options.has_key?(:auto_validation) && !property.options[:auto_validation]) || property.read_only)
|
93
|
+
# value is set by the storage system
|
94
|
+
opts = {}
|
95
|
+
opts[:context] = property.options[:validates] if property.options.has_key?(:validates)
|
96
|
+
|
97
|
+
# presence
|
98
|
+
if opts[:allow_nil] == false
|
99
|
+
validates_presence_of property.name, options_with_message(opts, property, :presence)
|
100
|
+
end
|
101
|
+
|
102
|
+
# length
|
103
|
+
if property.type == String
|
104
|
+
# XXX: maybe length should always return a Range, with the min defaulting to 1
|
105
|
+
# 52 being the max set
|
106
|
+
len = property.options.fetch(:length, property.options.fetch(:size, 52))
|
107
|
+
if len.is_a?(Range)
|
108
|
+
opts[:within] = len
|
109
|
+
else
|
110
|
+
opts[:maximum] = len
|
111
|
+
end
|
112
|
+
validates_length_of property.name, options_with_message(opts, property, :length)
|
113
|
+
end
|
114
|
+
|
115
|
+
# format
|
116
|
+
if property.options.has_key?(:format)
|
117
|
+
opts[:with] = property.options[:format]
|
118
|
+
# validates_format property.name, opts
|
119
|
+
validates_format property.name, options_with_message(opts, property, :format)
|
120
|
+
end
|
121
|
+
|
122
|
+
# uniqueness validator
|
123
|
+
if property.options.has_key?(:unique)
|
124
|
+
value = property.options[:unique]
|
125
|
+
if value.is_a?(Array) || value.is_a?(Symbol)
|
126
|
+
# validates_is_unique property.name, :scope => Array(value)
|
127
|
+
validates_is_unique property.name, options_with_message({:scope => Array(value)}, property, :is_unique)
|
128
|
+
elsif value.is_a?(TrueClass)
|
129
|
+
# validates_is_unique property.name
|
130
|
+
validates_is_unique property.name, options_with_message({}, property, :is_unique)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
# within validator
|
135
|
+
if property.options.has_key?(:set)
|
136
|
+
validates_within property.name, options_with_message({:set => property.options[:set]}, property, :within)
|
137
|
+
end
|
138
|
+
|
139
|
+
# numeric validator
|
140
|
+
if "Integer" == property.type
|
141
|
+
opts[:integer_only] = true
|
142
|
+
validates_numericality_of property.name, options_with_message(opts, property, :is_number)
|
143
|
+
elsif Float == property.type
|
144
|
+
opts[:precision] = property.precision
|
145
|
+
opts[:scale] = property.scale
|
146
|
+
validates_numericality_of property.name, options_with_message(opts, property, :is_number)
|
147
|
+
end
|
148
|
+
|
149
|
+
# marked the property has checked
|
150
|
+
property.autovalidation_check = true
|
151
|
+
|
152
|
+
end
|
153
|
+
|
154
|
+
end # module AutoValidate
|
155
|
+
end # module Validation
|
156
|
+
end # module CouchRest
|
@@ -0,0 +1,78 @@
|
|
1
|
+
# Extracted from dm-validations 0.9.10
|
2
|
+
#
|
3
|
+
# Copyright (c) 2007 Guy van den Berg
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
# a copy of this software and associated documentation files (the
|
7
|
+
# "Software"), to deal in the Software without restriction, including
|
8
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
# the following conditions:
|
12
|
+
#
|
13
|
+
# The above copyright notice and this permission notice shall be
|
14
|
+
# included in all copies or substantial portions of the Software.
|
15
|
+
#
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
|
24
|
+
module CouchRest
|
25
|
+
module Validation
|
26
|
+
|
27
|
+
##
|
28
|
+
#
|
29
|
+
# @author Guy van den Berg
|
30
|
+
# @since 0.9
|
31
|
+
class ContextualValidators
|
32
|
+
|
33
|
+
def dump
|
34
|
+
contexts.each_pair do |key, context|
|
35
|
+
puts "Key=#{key} Context: #{context}"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# Get a hash of named context validators for the resource
|
40
|
+
#
|
41
|
+
# @return <Hash> a hash of validators <GenericValidator>
|
42
|
+
def contexts
|
43
|
+
@contexts ||= {}
|
44
|
+
end
|
45
|
+
|
46
|
+
# Return an array of validators for a named context
|
47
|
+
#
|
48
|
+
# @return <Array> An array of validators
|
49
|
+
def context(name)
|
50
|
+
contexts[name] ||= []
|
51
|
+
end
|
52
|
+
|
53
|
+
# Clear all named context validators off of the resource
|
54
|
+
#
|
55
|
+
def clear!
|
56
|
+
contexts.clear
|
57
|
+
end
|
58
|
+
|
59
|
+
# Execute all validators in the named context against the target
|
60
|
+
#
|
61
|
+
# @param <Symbol> named_context the context we are validating against
|
62
|
+
# @param <Object> target the resource that we are validating
|
63
|
+
# @return <Boolean> true if all are valid, otherwise false
|
64
|
+
def execute(named_context, target)
|
65
|
+
raise(ArgumentError, 'invalid context specified') if !named_context || (contexts.length > 0 && !contexts[named_context])
|
66
|
+
target.errors.clear!
|
67
|
+
result = true
|
68
|
+
context(named_context).each do |validator|
|
69
|
+
next unless validator.execute?(target)
|
70
|
+
result = false unless validator.call(target)
|
71
|
+
end
|
72
|
+
|
73
|
+
result
|
74
|
+
end
|
75
|
+
|
76
|
+
end # module ContextualValidators
|
77
|
+
end # module Validation
|
78
|
+
end # module CouchRest
|
@@ -0,0 +1,125 @@
|
|
1
|
+
# Extracted from dm-validations 0.9.10
|
2
|
+
#
|
3
|
+
# Copyright (c) 2007 Guy van den Berg
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
# a copy of this software and associated documentation files (the
|
7
|
+
# "Software"), to deal in the Software without restriction, including
|
8
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
# the following conditions:
|
12
|
+
#
|
13
|
+
# The above copyright notice and this permission notice shall be
|
14
|
+
# included in all copies or substantial portions of the Software.
|
15
|
+
#
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
|
24
|
+
module CouchRest
|
25
|
+
module Validation
|
26
|
+
|
27
|
+
##
|
28
|
+
#
|
29
|
+
# @author Guy van den Berg
|
30
|
+
# @since 0.9
|
31
|
+
class ValidationErrors
|
32
|
+
|
33
|
+
include Enumerable
|
34
|
+
|
35
|
+
@@default_error_messages = {
|
36
|
+
:absent => '%s must be absent',
|
37
|
+
:inclusion => '%s must be one of [%s]',
|
38
|
+
:invalid => '%s has an invalid format',
|
39
|
+
:confirmation => '%s does not match the confirmation',
|
40
|
+
:accepted => "%s is not accepted",
|
41
|
+
:nil => '%s must not be nil',
|
42
|
+
:blank => '%s must not be blank',
|
43
|
+
:length_between => '%s must be between %s and %s characters long',
|
44
|
+
:too_long => '%s must be less than %s characters long',
|
45
|
+
:too_short => '%s must be more than %s characters long',
|
46
|
+
:wrong_length => '%s must be %s characters long',
|
47
|
+
:taken => '%s is already taken',
|
48
|
+
:not_a_number => '%s must be a number',
|
49
|
+
:not_an_integer => '%s must be an integer',
|
50
|
+
:greater_than => '%s must be greater than %s',
|
51
|
+
:greater_than_or_equal_to => "%s must be greater than or equal to %s",
|
52
|
+
:equal_to => "%s must be equal to %s",
|
53
|
+
:less_than => '%s must be less than %s',
|
54
|
+
:less_than_or_equal_to => "%s must be less than or equal to %s",
|
55
|
+
:value_between => '%s must be between %s and %s',
|
56
|
+
:primitive => '%s must be of type %s'
|
57
|
+
}
|
58
|
+
|
59
|
+
# Holds a hash with all the default error messages that can be replaced by your own copy or localizations.
|
60
|
+
cattr_writer :default_error_messages
|
61
|
+
|
62
|
+
def self.default_error_message(key, field, *values)
|
63
|
+
field = field.to_s.humanize
|
64
|
+
@@default_error_messages[key] % [field, *values].flatten
|
65
|
+
end
|
66
|
+
|
67
|
+
# Clear existing validation errors.
|
68
|
+
def clear!
|
69
|
+
errors.clear
|
70
|
+
end
|
71
|
+
|
72
|
+
# Add a validation error. Use the field_name :general if the errors does
|
73
|
+
# not apply to a specific field of the Resource.
|
74
|
+
#
|
75
|
+
# @param <Symbol> field_name the name of the field that caused the error
|
76
|
+
# @param <String> message the message to add
|
77
|
+
def add(field_name, message)
|
78
|
+
(errors[field_name.to_sym] ||= []) << message
|
79
|
+
end
|
80
|
+
|
81
|
+
# Collect all errors into a single list.
|
82
|
+
def full_messages
|
83
|
+
errors.inject([]) do |list, pair|
|
84
|
+
list += pair.last
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
# Return validation errors for a particular field_name.
|
89
|
+
#
|
90
|
+
# @param <Symbol> field_name the name of the field you want an error for
|
91
|
+
def on(field_name)
|
92
|
+
errors_for_field = errors[field_name.to_sym]
|
93
|
+
errors_for_field.blank? ? nil : errors_for_field
|
94
|
+
end
|
95
|
+
|
96
|
+
def each
|
97
|
+
errors.map.each do |k, v|
|
98
|
+
next if v.blank?
|
99
|
+
yield(v)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def empty?
|
104
|
+
entries.empty?
|
105
|
+
end
|
106
|
+
|
107
|
+
# Return size of errors hash
|
108
|
+
#
|
109
|
+
# Allows us to play nicely with Rails' helpers
|
110
|
+
def count
|
111
|
+
errors.size
|
112
|
+
end
|
113
|
+
|
114
|
+
def method_missing(meth, *args, &block)
|
115
|
+
errors.send(meth, *args, &block)
|
116
|
+
end
|
117
|
+
|
118
|
+
private
|
119
|
+
def errors
|
120
|
+
@errors ||= {}
|
121
|
+
end
|
122
|
+
|
123
|
+
end # class ValidationErrors
|
124
|
+
end # module Validation
|
125
|
+
end # module CouchRest
|
@@ -0,0 +1,74 @@
|
|
1
|
+
# Extracted from dm-validations 0.9.10
|
2
|
+
#
|
3
|
+
# Copyright (c) 2007 Guy van den Berg
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
# a copy of this software and associated documentation files (the
|
7
|
+
# "Software"), to deal in the Software without restriction, including
|
8
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
# the following conditions:
|
12
|
+
#
|
13
|
+
# The above copyright notice and this permission notice shall be
|
14
|
+
# included in all copies or substantial portions of the Software.
|
15
|
+
#
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
|
24
|
+
module CouchRest
|
25
|
+
module Validation
|
26
|
+
|
27
|
+
##
|
28
|
+
#
|
29
|
+
# @author Guy van den Berg
|
30
|
+
class AbsentFieldValidator < GenericValidator
|
31
|
+
|
32
|
+
def initialize(field_name, options={})
|
33
|
+
super
|
34
|
+
@field_name, @options = field_name, options
|
35
|
+
end
|
36
|
+
|
37
|
+
def call(target)
|
38
|
+
value = target.send(field_name)
|
39
|
+
return true if (value.nil? || (value.respond_to?(:empty?) && value.empty?))
|
40
|
+
|
41
|
+
error_message = @options[:message] || ValidationErrors.default_error_message(:absent, field_name)
|
42
|
+
add_error(target, error_message, field_name)
|
43
|
+
|
44
|
+
return false
|
45
|
+
end
|
46
|
+
end # class AbsentFieldValidator
|
47
|
+
|
48
|
+
module ValidatesAbsent
|
49
|
+
|
50
|
+
##
|
51
|
+
#
|
52
|
+
# @example [Usage]
|
53
|
+
#
|
54
|
+
# class Page
|
55
|
+
#
|
56
|
+
# property :unwanted_attribute, String
|
57
|
+
# property :another_unwanted, String
|
58
|
+
# property :yet_again, String
|
59
|
+
#
|
60
|
+
# validates_absent :unwanted_attribute
|
61
|
+
# validates_absent :another_unwanted, :yet_again
|
62
|
+
#
|
63
|
+
# # a call to valid? will return false unless
|
64
|
+
# # all three attributes are blank
|
65
|
+
# end
|
66
|
+
#
|
67
|
+
def validates_absent(*fields)
|
68
|
+
opts = opts_from_validator_args(fields)
|
69
|
+
add_validator_to_context(opts, fields, CouchRest::Validation::AbsentFieldValidator)
|
70
|
+
end
|
71
|
+
|
72
|
+
end # module ValidatesAbsent
|
73
|
+
end # module Validation
|
74
|
+
end # module CouchRest
|
@@ -0,0 +1,107 @@
|
|
1
|
+
# Extracted from dm-validations 0.9.10
|
2
|
+
#
|
3
|
+
# Copyright (c) 2007 Guy van den Berg
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
# a copy of this software and associated documentation files (the
|
7
|
+
# "Software"), to deal in the Software without restriction, including
|
8
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
# the following conditions:
|
12
|
+
#
|
13
|
+
# The above copyright notice and this permission notice shall be
|
14
|
+
# included in all copies or substantial portions of the Software.
|
15
|
+
#
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
|
24
|
+
module CouchRest
|
25
|
+
module Validation
|
26
|
+
|
27
|
+
##
|
28
|
+
#
|
29
|
+
# @author Guy van den Berg
|
30
|
+
# @since 0.9
|
31
|
+
class ConfirmationValidator < GenericValidator
|
32
|
+
|
33
|
+
def initialize(field_name, options = {})
|
34
|
+
super
|
35
|
+
@options = options
|
36
|
+
@field_name, @confirm_field_name = field_name, (options[:confirm] || "#{field_name}_confirmation").to_sym
|
37
|
+
@options[:allow_nil] = true unless @options.has_key?(:allow_nil)
|
38
|
+
end
|
39
|
+
|
40
|
+
def call(target)
|
41
|
+
unless valid?(target)
|
42
|
+
error_message = @options[:message] || ValidationErrors.default_error_message(:confirmation, field_name)
|
43
|
+
add_error(target, error_message, field_name)
|
44
|
+
return false
|
45
|
+
end
|
46
|
+
|
47
|
+
return true
|
48
|
+
end
|
49
|
+
|
50
|
+
def valid?(target)
|
51
|
+
field_value = target.send(field_name)
|
52
|
+
return true if @options[:allow_nil] && field_value.nil?
|
53
|
+
return false if !@options[:allow_nil] && field_value.nil?
|
54
|
+
|
55
|
+
confirm_value = target.instance_variable_get("@#{@confirm_field_name}")
|
56
|
+
field_value == confirm_value
|
57
|
+
end
|
58
|
+
|
59
|
+
end # class ConfirmationValidator
|
60
|
+
|
61
|
+
module ValidatesIsConfirmed
|
62
|
+
|
63
|
+
##
|
64
|
+
# Validates that the given attribute is confirmed by another attribute.
|
65
|
+
# A common use case scenario is when you require a user to confirm their
|
66
|
+
# password, for which you use both password and password_confirmation
|
67
|
+
# attributes.
|
68
|
+
#
|
69
|
+
# @option :allow_nil<Boolean> true/false (default is true)
|
70
|
+
# @option :confirm<Symbol> the attribute that you want to validate
|
71
|
+
# against (default is firstattr_confirmation)
|
72
|
+
#
|
73
|
+
# @example [Usage]
|
74
|
+
#
|
75
|
+
# class Page < Hash
|
76
|
+
# include CouchRest::ExtendedModel
|
77
|
+
# include CouchRest::Validations
|
78
|
+
#
|
79
|
+
# property :password, String
|
80
|
+
# property :email, String
|
81
|
+
# attr_accessor :password_confirmation
|
82
|
+
# attr_accessor :email_repeated
|
83
|
+
#
|
84
|
+
# validates_confirmation_of :password
|
85
|
+
# validates_confirmation_of :email, :confirm => :email_repeated
|
86
|
+
#
|
87
|
+
# # a call to valid? will return false unless:
|
88
|
+
# # password == password_confirmation
|
89
|
+
# # and
|
90
|
+
# # email == email_repeated
|
91
|
+
#
|
92
|
+
def validates_confirmation_of(*fields)
|
93
|
+
opts = opts_from_validator_args(fields)
|
94
|
+
add_validator_to_context(opts, fields, CouchRest::Validation::ConfirmationValidator)
|
95
|
+
end
|
96
|
+
|
97
|
+
def validates_is_confirmed(*fields)
|
98
|
+
warn "[DEPRECATION] `validates_is_confirmed` is deprecated. Please use `validates_confirmation_of` instead."
|
99
|
+
validates_confirmation_of(*fields)
|
100
|
+
end
|
101
|
+
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
end # module ValidatesIsConfirmed
|
106
|
+
end # module Validation
|
107
|
+
end # module CouchRest
|
@@ -0,0 +1,122 @@
|
|
1
|
+
# Extracted from dm-validations 0.9.10
|
2
|
+
#
|
3
|
+
# Copyright (c) 2007 Guy van den Berg
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
# a copy of this software and associated documentation files (the
|
7
|
+
# "Software"), to deal in the Software without restriction, including
|
8
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
# the following conditions:
|
12
|
+
#
|
13
|
+
# The above copyright notice and this permission notice shall be
|
14
|
+
# included in all copies or substantial portions of the Software.
|
15
|
+
#
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
|
24
|
+
require 'pathname'
|
25
|
+
require Pathname(__FILE__).dirname.expand_path + 'formats/email'
|
26
|
+
require Pathname(__FILE__).dirname.expand_path + 'formats/url'
|
27
|
+
|
28
|
+
module CouchRest
|
29
|
+
module Validation
|
30
|
+
|
31
|
+
##
|
32
|
+
#
|
33
|
+
# @author Guy van den Berg
|
34
|
+
# @since 0.9
|
35
|
+
class FormatValidator < GenericValidator
|
36
|
+
|
37
|
+
FORMATS = {}
|
38
|
+
include CouchRest::Validation::Format::Email
|
39
|
+
include CouchRest::Validation::Format::Url
|
40
|
+
|
41
|
+
def initialize(field_name, options = {}, &b)
|
42
|
+
super(field_name, options)
|
43
|
+
@field_name, @options = field_name, options
|
44
|
+
@options[:allow_nil] = false unless @options.has_key?(:allow_nil)
|
45
|
+
end
|
46
|
+
|
47
|
+
def call(target)
|
48
|
+
value = target.validation_property_value(field_name)
|
49
|
+
return true if @options[:allow_nil] && value.nil?
|
50
|
+
|
51
|
+
validation = @options[:as] || @options[:with]
|
52
|
+
|
53
|
+
raise "No such predefined format '#{validation}'" if validation.is_a?(Symbol) && !FORMATS.has_key?(validation)
|
54
|
+
validator = validation.is_a?(Symbol) ? FORMATS[validation][0] : validation
|
55
|
+
|
56
|
+
valid = case validator
|
57
|
+
when Proc then validator.call(value)
|
58
|
+
when Regexp then value =~ validator
|
59
|
+
else
|
60
|
+
raise UnknownValidationFormat, "Can't determine how to validate #{target.class}##{field_name} with #{validator.inspect}"
|
61
|
+
end
|
62
|
+
|
63
|
+
return true if valid
|
64
|
+
|
65
|
+
error_message = @options[:message] || ValidationErrors.default_error_message(:invalid, field_name)
|
66
|
+
|
67
|
+
field = field_name.to_s.humanize
|
68
|
+
error_message = error_message.call(field, value) if error_message.respond_to?(:call)
|
69
|
+
|
70
|
+
add_error(target, error_message, field_name)
|
71
|
+
|
72
|
+
false
|
73
|
+
end
|
74
|
+
|
75
|
+
#class UnknownValidationFormat < StandardError; end
|
76
|
+
|
77
|
+
end # class FormatValidator
|
78
|
+
|
79
|
+
module ValidatesFormat
|
80
|
+
|
81
|
+
##
|
82
|
+
# Validates that the attribute is in the specified format. You may use the
|
83
|
+
# :as (or :with, it's an alias) option to specify the pre-defined format
|
84
|
+
# that you want to validate against. You may also specify your own format
|
85
|
+
# via a Proc or Regexp passed to the the :as or :with options.
|
86
|
+
#
|
87
|
+
# @option :allow_nil<Boolean> true/false (default is true)
|
88
|
+
# @option :as<Format, Proc, Regexp> the pre-defined format, Proc or Regexp to validate against
|
89
|
+
# @option :with<Format, Proc, Regexp> an alias for :as
|
90
|
+
#
|
91
|
+
# @details [Pre-defined Formats]
|
92
|
+
# :email_address (format is specified in DataMapper::Validation::Format::Email)
|
93
|
+
# :url (format is specified in DataMapper::Validation::Format::Url)
|
94
|
+
#
|
95
|
+
# @example [Usage]
|
96
|
+
#
|
97
|
+
# class Page
|
98
|
+
#
|
99
|
+
# property :email, String
|
100
|
+
# property :zip_code, String
|
101
|
+
#
|
102
|
+
# validates_format_of :email, :as => :email_address
|
103
|
+
# validates_format_of :zip_code, :with => /^\d{5}$/
|
104
|
+
#
|
105
|
+
# # a call to valid? will return false unless:
|
106
|
+
# # email is formatted like an email address
|
107
|
+
# # and
|
108
|
+
# # zip_code is a string of 5 digits
|
109
|
+
#
|
110
|
+
def validates_format_of(*fields)
|
111
|
+
opts = opts_from_validator_args(fields)
|
112
|
+
add_validator_to_context(opts, fields, CouchRest::Validation::FormatValidator)
|
113
|
+
end
|
114
|
+
|
115
|
+
def validates_format(*fields)
|
116
|
+
warn "[DEPRECATION] `validates_format` is deprecated. Please use `validates_format_of` instead."
|
117
|
+
validates_format_of(*fields)
|
118
|
+
end
|
119
|
+
|
120
|
+
end # module ValidatesFormat
|
121
|
+
end # module Validation
|
122
|
+
end # module CouchRest
|