davber_couchrest_extended_document 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. data/LICENSE +176 -0
  2. data/README.md +250 -0
  3. data/Rakefile +69 -0
  4. data/THANKS.md +22 -0
  5. data/examples/model/example.rb +144 -0
  6. data/history.txt +165 -0
  7. data/lib/couchrest/casted_array.rb +39 -0
  8. data/lib/couchrest/casted_model.rb +53 -0
  9. data/lib/couchrest/extended_document.rb +262 -0
  10. data/lib/couchrest/mixins.rb +12 -0
  11. data/lib/couchrest/mixins/attribute_protection.rb +74 -0
  12. data/lib/couchrest/mixins/attributes.rb +75 -0
  13. data/lib/couchrest/mixins/callbacks.rb +534 -0
  14. data/lib/couchrest/mixins/class_proxy.rb +120 -0
  15. data/lib/couchrest/mixins/collection.rb +260 -0
  16. data/lib/couchrest/mixins/design_doc.rb +159 -0
  17. data/lib/couchrest/mixins/document_queries.rb +82 -0
  18. data/lib/couchrest/mixins/extended_attachments.rb +73 -0
  19. data/lib/couchrest/mixins/properties.rb +130 -0
  20. data/lib/couchrest/mixins/typecast.rb +174 -0
  21. data/lib/couchrest/mixins/views.rb +148 -0
  22. data/lib/couchrest/property.rb +96 -0
  23. data/lib/couchrest/support/couchrest.rb +19 -0
  24. data/lib/couchrest/support/rails.rb +42 -0
  25. data/lib/couchrest/validation.rb +246 -0
  26. data/lib/couchrest/validation/auto_validate.rb +156 -0
  27. data/lib/couchrest/validation/contextual_validators.rb +78 -0
  28. data/lib/couchrest/validation/validation_errors.rb +125 -0
  29. data/lib/couchrest/validation/validators/absent_field_validator.rb +74 -0
  30. data/lib/couchrest/validation/validators/confirmation_validator.rb +107 -0
  31. data/lib/couchrest/validation/validators/format_validator.rb +122 -0
  32. data/lib/couchrest/validation/validators/formats/email.rb +66 -0
  33. data/lib/couchrest/validation/validators/formats/url.rb +43 -0
  34. data/lib/couchrest/validation/validators/generic_validator.rb +120 -0
  35. data/lib/couchrest/validation/validators/length_validator.rb +139 -0
  36. data/lib/couchrest/validation/validators/method_validator.rb +89 -0
  37. data/lib/couchrest/validation/validators/numeric_validator.rb +109 -0
  38. data/lib/couchrest/validation/validators/required_field_validator.rb +114 -0
  39. data/lib/couchrest_extended_document.rb +23 -0
  40. data/spec/couchrest/attribute_protection_spec.rb +150 -0
  41. data/spec/couchrest/casted_extended_doc_spec.rb +79 -0
  42. data/spec/couchrest/casted_model_spec.rb +424 -0
  43. data/spec/couchrest/extended_doc_attachment_spec.rb +148 -0
  44. data/spec/couchrest/extended_doc_inherited_spec.rb +40 -0
  45. data/spec/couchrest/extended_doc_spec.rb +869 -0
  46. data/spec/couchrest/extended_doc_subclass_spec.rb +101 -0
  47. data/spec/couchrest/extended_doc_view_spec.rb +529 -0
  48. data/spec/couchrest/property_spec.rb +790 -0
  49. data/spec/fixtures/attachments/README +3 -0
  50. data/spec/fixtures/attachments/couchdb.png +0 -0
  51. data/spec/fixtures/attachments/test.html +11 -0
  52. data/spec/fixtures/more/article.rb +35 -0
  53. data/spec/fixtures/more/card.rb +22 -0
  54. data/spec/fixtures/more/cat.rb +22 -0
  55. data/spec/fixtures/more/course.rb +25 -0
  56. data/spec/fixtures/more/event.rb +8 -0
  57. data/spec/fixtures/more/invoice.rb +17 -0
  58. data/spec/fixtures/more/person.rb +9 -0
  59. data/spec/fixtures/more/question.rb +7 -0
  60. data/spec/fixtures/more/service.rb +12 -0
  61. data/spec/fixtures/more/user.rb +22 -0
  62. data/spec/fixtures/views/lib.js +3 -0
  63. data/spec/fixtures/views/test_view/lib.js +3 -0
  64. data/spec/fixtures/views/test_view/only-map.js +4 -0
  65. data/spec/fixtures/views/test_view/test-map.js +3 -0
  66. data/spec/fixtures/views/test_view/test-reduce.js +3 -0
  67. data/spec/spec.opts +5 -0
  68. data/spec/spec_helper.rb +49 -0
  69. data/utils/remap.rb +27 -0
  70. data/utils/subset.rb +30 -0
  71. metadata +225 -0
@@ -0,0 +1,148 @@
1
+ module CouchRest
2
+ module Mixins
3
+ module Views
4
+
5
+ def self.included(base)
6
+ base.extend(ClassMethods)
7
+ end
8
+
9
+ module ClassMethods
10
+ # Define a CouchDB view. The name of the view will be the concatenation
11
+ # of <tt>by</tt> and the keys joined by <tt>_and_</tt>
12
+ #
13
+ # ==== Example views:
14
+ #
15
+ # class Post
16
+ # # view with default options
17
+ # # query with Post.by_date
18
+ # view_by :date, :descending => true
19
+ #
20
+ # # view with compound sort-keys
21
+ # # query with Post.by_user_id_and_date
22
+ # view_by :user_id, :date
23
+ #
24
+ # # view with custom map/reduce functions
25
+ # # query with Post.by_tags :reduce => true
26
+ # view_by :tags,
27
+ # :map =>
28
+ # "function(doc) {
29
+ # if (doc['couchrest-type'] == 'Post' && doc.tags) {
30
+ # doc.tags.forEach(function(tag){
31
+ # emit(doc.tag, 1);
32
+ # });
33
+ # }
34
+ # }",
35
+ # :reduce =>
36
+ # "function(keys, values, rereduce) {
37
+ # return sum(values);
38
+ # }"
39
+ # end
40
+ #
41
+ # <tt>view_by :date</tt> will create a view defined by this Javascript
42
+ # function:
43
+ #
44
+ # function(doc) {
45
+ # if (doc['couchrest-type'] == 'Post' && doc.date) {
46
+ # emit(doc.date, null);
47
+ # }
48
+ # }
49
+ #
50
+ # It can be queried by calling <tt>Post.by_date</tt> which accepts all
51
+ # valid options for CouchRest::Database#view. In addition, calling with
52
+ # the <tt>:raw => true</tt> option will return the view rows
53
+ # themselves. By default <tt>Post.by_date</tt> will return the
54
+ # documents included in the generated view.
55
+ #
56
+ # Calling with :database => [instance of CouchRest::Database] will
57
+ # send the query to a specific database, otherwise it will go to
58
+ # the model's default database (use_database)
59
+ #
60
+ # CouchRest::Database#view options can be applied at view definition
61
+ # time as defaults, and they will be curried and used at view query
62
+ # time. Or they can be overridden at query time.
63
+ #
64
+ # Custom views can be queried with <tt>:reduce => true</tt> to return
65
+ # reduce results. The default for custom views is to query with
66
+ # <tt>:reduce => false</tt>.
67
+ #
68
+ # Views are generated (on a per-model basis) lazily on first-access.
69
+ # This means that if you are deploying changes to a view, the views for
70
+ # that model won't be available until generation is complete. This can
71
+ # take some time with large databases. Strategies are in the works.
72
+ #
73
+ # To understand the capabilities of this view system more completely,
74
+ # it is recommended that you read the RSpec file at
75
+ # <tt>spec/couchrest/more/extended_doc_spec.rb</tt>.
76
+
77
+ def view_by(*keys)
78
+ opts = keys.pop if keys.last.is_a?(Hash)
79
+ opts ||= {}
80
+ ducktype = opts.delete(:ducktype)
81
+ unless ducktype || opts[:map]
82
+ opts[:guards] ||= []
83
+ opts[:guards].push "(doc[#{CouchRest.type_field}] == '#{self.to_s}')"
84
+ end
85
+ keys.push opts
86
+ design_doc.view_by(*keys)
87
+ req_design_doc_refresh
88
+ end
89
+
90
+ # returns stored defaults if there is a view named this in the design doc
91
+ def has_view?(view)
92
+ view = view.to_s
93
+ design_doc && design_doc['views'] && design_doc['views'][view]
94
+ end
95
+
96
+ # Dispatches to any named view.
97
+ def view(name, query={}, &block)
98
+ db = query.delete(:database) || database
99
+ refresh_design_doc(db)
100
+ query[:raw] = true if query[:reduce]
101
+ raw = query.delete(:raw)
102
+ fetch_view_with_docs(db, name, query, raw, &block)
103
+ end
104
+
105
+ private
106
+
107
+ def fetch_view_with_docs(db, name, opts, raw=false, &block)
108
+ if raw || (opts.has_key?(:include_docs) && opts[:include_docs] == false)
109
+ fetch_view(db, name, opts, &block)
110
+ else
111
+ begin
112
+ if block.nil?
113
+ collection_proxy_for(design_doc, name, opts.merge({:include_docs => true}))
114
+ else
115
+ view = fetch_view db, name, opts.merge({:include_docs => true}), &block
116
+ view['rows'].collect{|r|create_from_database(r['doc'])} if view['rows']
117
+ end
118
+ rescue
119
+ # fallback for old versions of couchdb that don't
120
+ # have include_docs support
121
+ view = fetch_view(db, name, opts, &block)
122
+ view['rows'].collect{|r|create_from_database(db.get(r['id']))} if view['rows']
123
+ end
124
+ end
125
+ end
126
+
127
+ def fetch_view(db, view_name, opts, &block)
128
+ raise "A view needs a database to operate on (specify :database option, or use_database in the #{self.class} class)" unless db
129
+ retryable = true
130
+ begin
131
+ design_doc.view_on(db, view_name, opts, &block)
132
+ # the design doc may not have been saved yet on this database
133
+ rescue RestClient::ResourceNotFound => e
134
+ if retryable
135
+ save_design_doc(db)
136
+ retryable = false
137
+ retry
138
+ else
139
+ raise e
140
+ end
141
+ end
142
+ end
143
+
144
+ end # module ClassMethods
145
+
146
+ end
147
+ end
148
+ end
@@ -0,0 +1,96 @@
1
+
2
+ require File.join(File.dirname(__FILE__), 'mixins', 'typecast')
3
+
4
+ module CouchRest
5
+
6
+ # Basic attribute support for adding getter/setter + validation
7
+ class Property
8
+
9
+ include ::CouchRest::Mixins::Typecast
10
+
11
+ attr_reader :name, :type, :read_only, :alias, :default, :casted, :init_method, :options
12
+
13
+ # Attribute to define.
14
+ # All Properties are assumed casted unless the type is nil.
15
+ def initialize(name, type = nil, options = {})
16
+ @name = name.to_s
17
+ @casted = true
18
+ parse_type(type)
19
+ parse_options(options)
20
+ self
21
+ end
22
+
23
+ def to_s
24
+ name
25
+ end
26
+
27
+ # Cast the provided value using the properties details.
28
+ def cast(parent, value)
29
+ return value unless casted
30
+ if type.is_a?(Array)
31
+ # Convert to array if it is not already
32
+ value = [value].compact unless value.is_a?(Array)
33
+ arr = value.collect { |data| cast_value(parent, data) }
34
+ # allow casted_by calls to be passed up chain by wrapping in CastedArray
35
+ value = type_class != String ? ::CouchRest::CastedArray.new(arr, self) : arr
36
+ value.casted_by = parent if value.respond_to?(:casted_by)
37
+ elsif !value.nil?
38
+ value = cast_value(parent, value)
39
+ end
40
+ value
41
+ end
42
+
43
+ # Cast an individual value, not an array
44
+ def cast_value(parent, value)
45
+ raise "An array inside an array cannot be casted, use CastedModel" if value.is_a?(Array)
46
+ value = typecast_value(value, self)
47
+ associate_casted_value_to_parent(parent, value)
48
+ end
49
+
50
+ def default_value
51
+ return if default.nil?
52
+ if default.class == Proc
53
+ default.call
54
+ else
55
+ Marshal.load(Marshal.dump(default))
56
+ end
57
+ end
58
+
59
+ # Always provide the basic type as a class. If the type
60
+ # is an array, the class will be extracted.
61
+ def type_class
62
+ return String unless casted # This is rubbish, to handle validations
63
+ return @type_class unless @type_class.nil?
64
+ base = @type.is_a?(Array) ? @type.first : @type
65
+ base = String if base.nil?
66
+ base = TrueClass if base.is_a?(String) && base.downcase == 'boolean'
67
+ @type_class = base.is_a?(Class) ? base : base.constantize
68
+ end
69
+
70
+ private
71
+
72
+ def associate_casted_value_to_parent(parent, value)
73
+ value.casted_by = parent if value.respond_to?(:casted_by)
74
+ value
75
+ end
76
+
77
+ def parse_type(type)
78
+ if type.nil?
79
+ @casted = false
80
+ @type = nil
81
+ else
82
+ @type = type
83
+ end
84
+ end
85
+
86
+ def parse_options(options)
87
+ @validation_format = options.delete(:format) if options[:format]
88
+ @read_only = options.delete(:read_only) if options[:read_only]
89
+ @alias = options.delete(:alias) if options[:alias]
90
+ @default = options.delete(:default) unless options[:default].nil?
91
+ @init_method = options[:init_method] ? options.delete(:init_method) : 'new'
92
+ @options = options
93
+ end
94
+
95
+ end
96
+ end
@@ -0,0 +1,19 @@
1
+
2
+ module CouchRest
3
+
4
+ class Database
5
+
6
+ alias :delete_old! :delete!
7
+ def delete!
8
+ clear_extended_doc_fresh_cache
9
+ delete_old!
10
+ end
11
+
12
+ # If the database is deleted, ensure that the design docs will be refreshed.
13
+ def clear_extended_doc_fresh_cache
14
+ ::CouchRest::ExtendedDocument.subclasses.each{|klass| klass.req_design_doc_refresh if klass.respond_to?(:req_design_doc_refresh)}
15
+ end
16
+
17
+ end
18
+
19
+ end
@@ -0,0 +1,42 @@
1
+ # This file contains various hacks for Rails compatibility.
2
+ class Hash
3
+ # Hack so that CouchRest::Document, which descends from Hash,
4
+ # doesn't appear to Rails routing as a Hash of options
5
+ def self.===(other)
6
+ return false if self == Hash && other.is_a?(CouchRest::Document)
7
+ super
8
+ end
9
+ end
10
+
11
+ CouchRest::Document.class_eval do
12
+ # Need this when passing doc to a resourceful route
13
+ alias_method :to_param, :id
14
+
15
+ # Hack so that CouchRest::Document, which descends from Hash,
16
+ # doesn't appear to Rails routing as a Hash of options
17
+ def is_a?(o)
18
+ return false if o == Hash
19
+ super
20
+ end
21
+ alias_method :kind_of?, :is_a?
22
+ end
23
+
24
+ CouchRest::CastedModel.class_eval do
25
+ # The to_param method is needed for rails to generate resourceful routes.
26
+ # In your controller, remember that it's actually the id of the document.
27
+ def id
28
+ return nil if base_doc.nil?
29
+ base_doc.id
30
+ end
31
+ alias_method :to_param, :id
32
+ end
33
+
34
+ require Pathname.new(File.dirname(__FILE__)).join('..', 'validation', 'validation_errors')
35
+
36
+ CouchRest::Validation::ValidationErrors.class_eval do
37
+ # Returns the total number of errors added. Two errors added to the same attribute will be counted as such.
38
+ # This method is called by error_messages_for
39
+ def count
40
+ errors.values.inject(0) { |error_count, errors_for_attribute| error_count + errors_for_attribute.size }
41
+ end
42
+ end
@@ -0,0 +1,246 @@
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
+ class Object
25
+ def validatable?
26
+ false
27
+ end
28
+ end
29
+
30
+ require 'pathname'
31
+
32
+ dir = File.join(Pathname(__FILE__).dirname.expand_path, 'validation')
33
+
34
+ require File.join(dir, 'validation_errors')
35
+ require File.join(dir, 'contextual_validators')
36
+ require File.join(dir, 'auto_validate')
37
+
38
+ require File.join(dir, 'validators', 'generic_validator')
39
+ require File.join(dir, 'validators', 'required_field_validator')
40
+ require File.join(dir, 'validators', 'absent_field_validator')
41
+ require File.join(dir, 'validators', 'format_validator')
42
+ require File.join(dir, 'validators', 'length_validator')
43
+ require File.join(dir, 'validators', 'numeric_validator')
44
+ require File.join(dir, 'validators', 'method_validator')
45
+ require File.join(dir, 'validators', 'confirmation_validator')
46
+
47
+ module CouchRest
48
+ module Validation
49
+
50
+ def self.included(base)
51
+ base.class_eval <<-EOS, __FILE__, __LINE__ + 1
52
+ extend CouchRest::InheritableAttributes
53
+ couchrest_inheritable_accessor(:auto_validation)
54
+
55
+ # Callbacks
56
+ define_callbacks :validate
57
+
58
+ # Turn off auto validation by default
59
+ self.auto_validation ||= false
60
+
61
+ # Force the auto validation for the class properties
62
+ # This feature is still not fully ported over,
63
+ # test are lacking, so please use with caution
64
+ def self.auto_validate!
65
+ self.auto_validation = true
66
+ end
67
+
68
+ # share the validations with subclasses
69
+ def self.inherited(subklass)
70
+ self.validators.contexts.each do |k, v|
71
+ subklass.validators.contexts[k] = v.dup
72
+ end
73
+ super
74
+ end
75
+ EOS
76
+
77
+ base.extend(ClassMethods)
78
+ base.class_eval <<-EOS, __FILE__, __LINE__ + 1
79
+ define_callbacks :validate
80
+ if method_defined?(:_run_save_callbacks)
81
+ set_callback :save, :before, :check_validations
82
+ end
83
+ EOS
84
+ base.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
85
+ def self.define_property(name, options={}, &block)
86
+ property = super
87
+ auto_generate_validations(property) unless property.nil?
88
+ end
89
+ RUBY_EVAL
90
+ end
91
+
92
+ # Ensures the object is valid for the context provided, and otherwise
93
+ # throws :halt and returns false.
94
+ #
95
+ def check_validations(context = :default)
96
+ throw(:halt, false) unless context.nil? || valid?(context)
97
+ end
98
+
99
+ # Return the ValidationErrors
100
+ #
101
+ def errors
102
+ @errors ||= ValidationErrors.new
103
+ end
104
+
105
+ # Mark this resource as validatable. When we validate associations of a
106
+ # resource we can check if they respond to validatable? before trying to
107
+ # recursivly validate them
108
+ #
109
+ def validatable?
110
+ true
111
+ end
112
+
113
+ # Alias for valid?(:default)
114
+ #
115
+ def valid_for_default?
116
+ valid?(:default)
117
+ end
118
+
119
+ # Check if a resource is valid in a given context
120
+ #
121
+ def valid?(context = :default)
122
+ recursive_valid?(self, context, true)
123
+ end
124
+
125
+ # checking on casted objects
126
+ def validate_casted_arrays
127
+ result = true
128
+ array_casted_properties = self.class.properties.select { |property| property.casted && property.type.instance_of?(Array) }
129
+ array_casted_properties.each do |property|
130
+ casted_values = self.send(property.name)
131
+ next unless casted_values.is_a?(Array) && casted_values.first.respond_to?(:valid?)
132
+ casted_values.each do |value|
133
+ result = (result && value.valid?) if value.respond_to?(:valid?)
134
+ end
135
+ end
136
+ result
137
+ end
138
+
139
+ # Do recursive validity checking
140
+ #
141
+ def recursive_valid?(target, context, state)
142
+ valid = state
143
+ target.each do |key, prop|
144
+ if prop.is_a?(Array)
145
+ prop.each do |item|
146
+ if item.validatable?
147
+ valid = recursive_valid?(item, context, valid) && valid
148
+ end
149
+ end
150
+ elsif prop.validatable?
151
+ valid = recursive_valid?(prop, context, valid) && valid
152
+ end
153
+ end
154
+ target._run_validate_callbacks do
155
+ target.class.validators.execute(context, target) && valid
156
+ end
157
+ end
158
+
159
+
160
+ def validation_property_value(name)
161
+ self.respond_to?(name, true) ? self.send(name) : nil
162
+ end
163
+
164
+ # Get the corresponding Object property, if it exists.
165
+ def validation_property(field_name)
166
+ properties.find{|p| p.name == field_name}
167
+ end
168
+
169
+ module ClassMethods
170
+ include CouchRest::Validation::ValidatesPresent
171
+ include CouchRest::Validation::ValidatesAbsent
172
+ include CouchRest::Validation::ValidatesIsConfirmed
173
+ # include CouchRest::Validation::ValidatesIsPrimitive
174
+ # include CouchRest::Validation::ValidatesIsAccepted
175
+ include CouchRest::Validation::ValidatesFormat
176
+ include CouchRest::Validation::ValidatesLength
177
+ # include CouchRest::Validation::ValidatesWithin
178
+ include CouchRest::Validation::ValidatesIsNumber
179
+ include CouchRest::Validation::ValidatesWithMethod
180
+ # include CouchRest::Validation::ValidatesWithBlock
181
+ # include CouchRest::Validation::ValidatesIsUnique
182
+ include CouchRest::Validation::AutoValidate
183
+
184
+ # Return the set of contextual validators or create a new one
185
+ #
186
+ def validators
187
+ @validations ||= ContextualValidators.new
188
+ end
189
+
190
+ # Clean up the argument list and return a opts hash, including the
191
+ # merging of any default opts. Set the context to default if none is
192
+ # provided. Also allow :context to be aliased to :on, :when & group
193
+ #
194
+ def opts_from_validator_args(args, defaults = nil)
195
+ opts = args.last.kind_of?(Hash) ? args.pop : {}
196
+ context = :default
197
+ context = opts[:context] if opts.has_key?(:context)
198
+ context = opts.delete(:on) if opts.has_key?(:on)
199
+ context = opts.delete(:when) if opts.has_key?(:when)
200
+ context = opts.delete(:group) if opts.has_key?(:group)
201
+ opts[:context] = context
202
+ opts.merge!(defaults) unless defaults.nil?
203
+ opts
204
+ end
205
+
206
+ # Given a new context create an instance method of
207
+ # valid_for_<context>? which simply calls valid?(context)
208
+ # if it does not already exist
209
+ #
210
+ def create_context_instance_methods(context)
211
+ name = "valid_for_#{context.to_s}?" # valid_for_signup?
212
+ if !self.instance_methods.include?(name)
213
+ class_eval <<-EOS, __FILE__, __LINE__ + 1
214
+ def #{name} # def valid_for_signup?
215
+ valid?('#{context.to_s}'.to_sym) # valid?('signup'.to_sym)
216
+ end # end
217
+ EOS
218
+ end
219
+ end
220
+
221
+ # Create a new validator of the given klazz and push it onto the
222
+ # requested context for each of the attributes in the fields list
223
+ #
224
+ def add_validator_to_context(opts, fields, klazz)
225
+ fields.each do |field|
226
+ validator = klazz.new(field.to_sym, opts)
227
+ if opts[:context].is_a?(Symbol)
228
+ unless validators.context(opts[:context]).include?(validator)
229
+ validators.context(opts[:context]) << validator
230
+ create_context_instance_methods(opts[:context])
231
+ end
232
+ elsif opts[:context].is_a?(Array)
233
+ opts[:context].each do |c|
234
+ unless validators.context(c).include?(validator)
235
+ validators.context(c) << validator
236
+ create_context_instance_methods(c)
237
+ end
238
+ end
239
+ end
240
+ end
241
+ end
242
+
243
+ end # module ClassMethods
244
+ end # module Validation
245
+
246
+ end # module CouchRest