jchris-couchrest 0.2.2 → 0.7.99

Sign up to get free protection for your applications and to get access to all the features.
Files changed (97) hide show
  1. data/README +39 -0
  2. data/Rakefile +3 -54
  3. data/lib/couchrest.rb +39 -161
  4. data/lib/database.rb +83 -0
  5. data/script/couchdir +58 -0
  6. data/script/couchview +158 -0
  7. data/spec/couchrest_spec.rb +86 -0
  8. data/spec/database_spec.rb +407 -0
  9. metadata +15 -138
  10. data/LICENSE +0 -176
  11. data/README.md +0 -93
  12. data/THANKS.md +0 -18
  13. data/examples/model/example.rb +0 -144
  14. data/examples/word_count/markov +0 -38
  15. data/examples/word_count/views/books/chunked-map.js +0 -3
  16. data/examples/word_count/views/books/united-map.js +0 -1
  17. data/examples/word_count/views/markov/chain-map.js +0 -6
  18. data/examples/word_count/views/markov/chain-reduce.js +0 -7
  19. data/examples/word_count/views/word_count/count-map.js +0 -6
  20. data/examples/word_count/views/word_count/count-reduce.js +0 -3
  21. data/examples/word_count/word_count.rb +0 -46
  22. data/examples/word_count/word_count_query.rb +0 -40
  23. data/examples/word_count/word_count_views.rb +0 -26
  24. data/lib/couchrest/commands/generate.rb +0 -71
  25. data/lib/couchrest/commands/push.rb +0 -103
  26. data/lib/couchrest/core/database.rb +0 -318
  27. data/lib/couchrest/core/design.rb +0 -89
  28. data/lib/couchrest/core/document.rb +0 -96
  29. data/lib/couchrest/core/response.rb +0 -16
  30. data/lib/couchrest/core/server.rb +0 -88
  31. data/lib/couchrest/core/view.rb +0 -4
  32. data/lib/couchrest/helper/pager.rb +0 -103
  33. data/lib/couchrest/helper/streamer.rb +0 -44
  34. data/lib/couchrest/helper/upgrade.rb +0 -51
  35. data/lib/couchrest/mixins.rb +0 -4
  36. data/lib/couchrest/mixins/attachments.rb +0 -31
  37. data/lib/couchrest/mixins/callbacks.rb +0 -483
  38. data/lib/couchrest/mixins/design_doc.rb +0 -64
  39. data/lib/couchrest/mixins/document_queries.rb +0 -48
  40. data/lib/couchrest/mixins/extended_attachments.rb +0 -68
  41. data/lib/couchrest/mixins/extended_document_mixins.rb +0 -6
  42. data/lib/couchrest/mixins/properties.rb +0 -125
  43. data/lib/couchrest/mixins/validation.rb +0 -234
  44. data/lib/couchrest/mixins/views.rb +0 -168
  45. data/lib/couchrest/monkeypatches.rb +0 -119
  46. data/lib/couchrest/more/casted_model.rb +0 -28
  47. data/lib/couchrest/more/extended_document.rb +0 -217
  48. data/lib/couchrest/more/property.rb +0 -40
  49. data/lib/couchrest/support/blank.rb +0 -42
  50. data/lib/couchrest/support/class.rb +0 -191
  51. data/lib/couchrest/validation/auto_validate.rb +0 -163
  52. data/lib/couchrest/validation/contextual_validators.rb +0 -78
  53. data/lib/couchrest/validation/validation_errors.rb +0 -118
  54. data/lib/couchrest/validation/validators/absent_field_validator.rb +0 -74
  55. data/lib/couchrest/validation/validators/confirmation_validator.rb +0 -99
  56. data/lib/couchrest/validation/validators/format_validator.rb +0 -117
  57. data/lib/couchrest/validation/validators/formats/email.rb +0 -66
  58. data/lib/couchrest/validation/validators/formats/url.rb +0 -43
  59. data/lib/couchrest/validation/validators/generic_validator.rb +0 -120
  60. data/lib/couchrest/validation/validators/length_validator.rb +0 -134
  61. data/lib/couchrest/validation/validators/method_validator.rb +0 -89
  62. data/lib/couchrest/validation/validators/numeric_validator.rb +0 -104
  63. data/lib/couchrest/validation/validators/required_field_validator.rb +0 -109
  64. data/spec/couchrest/core/couchrest_spec.rb +0 -201
  65. data/spec/couchrest/core/database_spec.rb +0 -745
  66. data/spec/couchrest/core/design_spec.rb +0 -131
  67. data/spec/couchrest/core/document_spec.rb +0 -311
  68. data/spec/couchrest/core/server_spec.rb +0 -35
  69. data/spec/couchrest/helpers/pager_spec.rb +0 -122
  70. data/spec/couchrest/helpers/streamer_spec.rb +0 -23
  71. data/spec/couchrest/more/casted_extended_doc_spec.rb +0 -40
  72. data/spec/couchrest/more/casted_model_spec.rb +0 -98
  73. data/spec/couchrest/more/extended_doc_attachment_spec.rb +0 -130
  74. data/spec/couchrest/more/extended_doc_spec.rb +0 -509
  75. data/spec/couchrest/more/extended_doc_view_spec.rb +0 -207
  76. data/spec/couchrest/more/property_spec.rb +0 -130
  77. data/spec/couchrest/support/class_spec.rb +0 -59
  78. data/spec/fixtures/attachments/README +0 -3
  79. data/spec/fixtures/attachments/couchdb.png +0 -0
  80. data/spec/fixtures/attachments/test.html +0 -11
  81. data/spec/fixtures/more/article.rb +0 -34
  82. data/spec/fixtures/more/card.rb +0 -20
  83. data/spec/fixtures/more/course.rb +0 -14
  84. data/spec/fixtures/more/event.rb +0 -6
  85. data/spec/fixtures/more/invoice.rb +0 -17
  86. data/spec/fixtures/more/person.rb +0 -8
  87. data/spec/fixtures/more/question.rb +0 -6
  88. data/spec/fixtures/more/service.rb +0 -12
  89. data/spec/fixtures/views/lib.js +0 -3
  90. data/spec/fixtures/views/test_view/lib.js +0 -3
  91. data/spec/fixtures/views/test_view/only-map.js +0 -4
  92. data/spec/fixtures/views/test_view/test-map.js +0 -3
  93. data/spec/fixtures/views/test_view/test-reduce.js +0 -3
  94. data/spec/spec.opts +0 -6
  95. data/spec/spec_helper.rb +0 -26
  96. data/utils/remap.rb +0 -27
  97. data/utils/subset.rb +0 -30
@@ -1,40 +0,0 @@
1
- module CouchRest
2
-
3
- # Basic attribute support for adding getter/setter + validation
4
- class Property
5
- attr_reader :name, :type, :read_only, :alias, :default, :casted, :init_method, :options
6
-
7
- # attribute to define
8
- def initialize(name, type = nil, options = {})
9
- @name = name.to_s
10
- parse_type(type)
11
- parse_options(options)
12
- self
13
- end
14
-
15
-
16
- private
17
-
18
- def parse_type(type)
19
- if type.nil?
20
- @type = 'String'
21
- elsif type.is_a?(Array) && type.empty?
22
- @type = 'Array'
23
- else
24
- @type = type.is_a?(Array) ? [type.first.to_s] : type.to_s
25
- end
26
- end
27
-
28
- def parse_options(options)
29
- return if options.empty?
30
- @validation_format = options.delete(:format) if options[:format]
31
- @read_only = options.delete(:read_only) if options[:read_only]
32
- @alias = options.delete(:alias) if options[:alias]
33
- @default = options.delete(:default) if options[:default]
34
- @casted = options[:casted] ? true : false
35
- @init_method = options[:send] ? options.delete(:send) : 'new'
36
- @options = options
37
- end
38
-
39
- end
40
- end
@@ -1,42 +0,0 @@
1
- # blank? methods for several different class types
2
- class Object
3
- # Returns true if the object is nil or empty (if applicable)
4
- def blank?
5
- nil? || (respond_to?(:empty?) && empty?)
6
- end
7
- end # class Object
8
-
9
- class Numeric
10
- # Numerics can't be blank
11
- def blank?
12
- false
13
- end
14
- end # class Numeric
15
-
16
- class NilClass
17
- # Nils are always blank
18
- def blank?
19
- true
20
- end
21
- end # class NilClass
22
-
23
- class TrueClass
24
- # True is not blank.
25
- def blank?
26
- false
27
- end
28
- end # class TrueClass
29
-
30
- class FalseClass
31
- # False is always blank.
32
- def blank?
33
- true
34
- end
35
- end # class FalseClass
36
-
37
- class String
38
- # Strips out whitespace then tests if the string is empty.
39
- def blank?
40
- strip.empty?
41
- end
42
- end # class String
@@ -1,191 +0,0 @@
1
- # Copyright (c) 2004-2008 David Heinemeier Hansson
2
- #
3
- # Permission is hereby granted, free of charge, to any person obtaining
4
- # a copy of this software and associated documentation files (the
5
- # "Software"), to deal in the Software without restriction, including
6
- # without limitation the rights to use, copy, modify, merge, publish,
7
- # distribute, sublicense, and/or sell copies of the Software, and to
8
- # permit persons to whom the Software is furnished to do so, subject to
9
- # the following conditions:
10
- #
11
- # The above copyright notice and this permission notice shall be
12
- # included in all copies or substantial portions of the Software.
13
- #
14
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
-
22
- # Allows attributes to be shared within an inheritance hierarchy, but where
23
- # each descendant gets a copy of their parents' attributes, instead of just a
24
- # pointer to the same. This means that the child can add elements to, for
25
- # example, an array without those additions being shared with either their
26
- # parent, siblings, or children, which is unlike the regular class-level
27
- # attributes that are shared across the entire hierarchy.
28
- module CouchRest
29
- module ClassExtension
30
- def self.included(base)
31
- if CouchRest::ClassExtension::InstanceMethods.instance_methods.all? {|m| base.respond_to?(m)}
32
- # do nothing
33
- elsif CouchRest::ClassExtension::InstanceMethods.instance_methods.any? {|m| base.respond_to?(m)}
34
- raise RuntimeError, "Conflicting extentions to Class, work it out"
35
- else
36
- base.send(:include, CouchRest::ClassExtension::InstanceMethods)
37
- end
38
- end
39
-
40
- module InstanceMethods
41
- # Defines class-level and instance-level attribute reader.
42
- #
43
- # @param *syms<Array> Array of attributes to define reader for.
44
- # @return <Array[#to_s]> List of attributes that were made into cattr_readers
45
- #
46
- # @api public
47
- #
48
- # @todo Is this inconsistent in that it does not allow you to prevent
49
- # an instance_reader via :instance_reader => false
50
- def cattr_reader(*syms)
51
- syms.flatten.each do |sym|
52
- next if sym.is_a?(Hash)
53
- class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
54
- unless defined? @@#{sym}
55
- @@#{sym} = nil
56
- end
57
-
58
- def self.#{sym}
59
- @@#{sym}
60
- end
61
-
62
- def #{sym}
63
- @@#{sym}
64
- end
65
- RUBY
66
- end
67
- end
68
-
69
- # Defines class-level (and optionally instance-level) attribute writer.
70
- #
71
- # @param <Array[*#to_s, Hash{:instance_writer => Boolean}]> Array of attributes to define writer for.
72
- # @option syms :instance_writer<Boolean> if true, instance-level attribute writer is defined.
73
- # @return <Array[#to_s]> List of attributes that were made into cattr_writers
74
- #
75
- # @api public
76
- def cattr_writer(*syms)
77
- options = syms.last.is_a?(Hash) ? syms.pop : {}
78
- syms.flatten.each do |sym|
79
- class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
80
- unless defined? @@#{sym}
81
- @@#{sym} = nil
82
- end
83
-
84
- def self.#{sym}=(obj)
85
- @@#{sym} = obj
86
- end
87
- RUBY
88
-
89
- unless options[:instance_writer] == false
90
- class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
91
- def #{sym}=(obj)
92
- @@#{sym} = obj
93
- end
94
- RUBY
95
- end
96
- end
97
- end
98
-
99
- # Defines class-level (and optionally instance-level) attribute accessor.
100
- #
101
- # @param *syms<Array[*#to_s, Hash{:instance_writer => Boolean}]> Array of attributes to define accessor for.
102
- # @option syms :instance_writer<Boolean> if true, instance-level attribute writer is defined.
103
- # @return <Array[#to_s]> List of attributes that were made into accessors
104
- #
105
- # @api public
106
- def cattr_accessor(*syms)
107
- cattr_reader(*syms)
108
- cattr_writer(*syms)
109
- end
110
-
111
- # Defines class-level inheritable attribute reader. Attributes are available to subclasses,
112
- # each subclass has a copy of parent's attribute.
113
- #
114
- # @param *syms<Array[#to_s]> Array of attributes to define inheritable reader for.
115
- # @return <Array[#to_s]> Array of attributes converted into inheritable_readers.
116
- #
117
- # @api public
118
- #
119
- # @todo Do we want to block instance_reader via :instance_reader => false
120
- # @todo It would be preferable that we do something with a Hash passed in
121
- # (error out or do the same as other methods above) instead of silently
122
- # moving on). In particular, this makes the return value of this function
123
- # less useful.
124
- def class_inheritable_reader(*ivars)
125
- instance_reader = ivars.pop[:reader] if ivars.last.is_a?(Hash)
126
-
127
- ivars.each do |ivar|
128
- self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
129
- def self.#{ivar}
130
- return @#{ivar} if self.object_id == #{self.object_id} || defined?(@#{ivar})
131
- ivar = superclass.#{ivar}
132
- return nil if ivar.nil? && !#{self}.instance_variable_defined?("@#{ivar}")
133
- @#{ivar} = ivar && !ivar.is_a?(Module) && !ivar.is_a?(Numeric) && !ivar.is_a?(TrueClass) && !ivar.is_a?(FalseClass) && !ivar.is_a?(Symbol) ? ivar.dup : ivar
134
- end
135
- RUBY
136
- unless instance_reader == false
137
- self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
138
- def #{ivar}
139
- self.class.#{ivar}
140
- end
141
- RUBY
142
- end
143
- end
144
- end
145
-
146
- # Defines class-level inheritable attribute writer. Attributes are available to subclasses,
147
- # each subclass has a copy of parent's attribute.
148
- #
149
- # @param *syms<Array[*#to_s, Hash{:instance_writer => Boolean}]> Array of attributes to
150
- # define inheritable writer for.
151
- # @option syms :instance_writer<Boolean> if true, instance-level inheritable attribute writer is defined.
152
- # @return <Array[#to_s]> An Array of the attributes that were made into inheritable writers.
153
- #
154
- # @api public
155
- #
156
- # @todo We need a style for class_eval <<-HEREDOC. I'd like to make it
157
- # class_eval(<<-RUBY, __FILE__, __LINE__), but we should codify it somewhere.
158
- def class_inheritable_writer(*ivars)
159
- instance_writer = ivars.pop[:instance_writer] if ivars.last.is_a?(Hash)
160
- ivars.each do |ivar|
161
- self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
162
- def self.#{ivar}=(obj)
163
- @#{ivar} = obj
164
- end
165
- RUBY
166
- unless instance_writer == false
167
- self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
168
- def #{ivar}=(obj) self.class.#{ivar} = obj end
169
- RUBY
170
- end
171
- end
172
- end
173
-
174
- # Defines class-level inheritable attribute accessor. Attributes are available to subclasses,
175
- # each subclass has a copy of parent's attribute.
176
- #
177
- # @param *syms<Array[*#to_s, Hash{:instance_writer => Boolean}]> Array of attributes to
178
- # define inheritable accessor for.
179
- # @option syms :instance_writer<Boolean> if true, instance-level inheritable attribute writer is defined.
180
- # @return <Array[#to_s]> An Array of attributes turned into inheritable accessors.
181
- #
182
- # @api public
183
- def class_inheritable_accessor(*syms)
184
- class_inheritable_reader(*syms)
185
- class_inheritable_writer(*syms)
186
- end
187
- end
188
- end
189
- end
190
-
191
- Class.send(:include, CouchRest::ClassExtension)
@@ -1,163 +0,0 @@
1
- # Ported from dm-migrations
2
- require File.join(File.dirname(__FILE__), '..', 'support', 'class')
3
-
4
- module CouchRest
5
-
6
- class Property
7
- # flag letting us know if we already checked the autovalidation settings
8
- attr_accessor :autovalidation_check
9
- @autovalidation_check = false
10
- end
11
-
12
- module Validation
13
- module AutoValidate
14
-
15
- # # Force the auto validation for the class properties
16
- # # This feature is still not fully ported over,
17
- # # test are lacking, so please use with caution
18
- # def auto_validate!
19
- # require 'ruby-debug'
20
- # debugger
21
- # auto_validation = true
22
- # end
23
-
24
- # adds message for validator
25
- def options_with_message(base_options, property, validator_name)
26
- options = base_options.clone
27
- opts = property.options
28
- options[:message] = if opts[:messages]
29
- if opts[:messages].is_a?(Hash) and msg = opts[:messages][validator_name]
30
- msg
31
- else
32
- nil
33
- end
34
- elsif opts[:message]
35
- opts[:message]
36
- else
37
- nil
38
- end
39
- options
40
- end
41
-
42
-
43
- ##
44
- # Auto-generate validations for a given property. This will only occur
45
- # if the option :auto_validation is either true or left undefined.
46
- #
47
- # @details [Triggers]
48
- # Triggers that generate validator creation
49
- #
50
- # :nullable => false
51
- # Setting the option :nullable to false causes a
52
- # validates_presence_of validator to be automatically created on
53
- # the property
54
- #
55
- # :size => 20 or :length => 20
56
- # Setting the option :size or :length causes a validates_length_of
57
- # validator to be automatically created on the property. If the
58
- # value is a Integer the validation will set :maximum => value if
59
- # the value is a Range the validation will set :within => value
60
- #
61
- # :format => :predefined / lambda / Proc
62
- # Setting the :format option causes a validates_format_of
63
- # validator to be automatically created on the property
64
- #
65
- # :set => ["foo", "bar", "baz"]
66
- # Setting the :set option causes a validates_within
67
- # validator to be automatically created on the property
68
- #
69
- # Integer type
70
- # Using a Integer type causes a validates_is_number
71
- # validator to be created for the property. integer_only
72
- # is set to true
73
- #
74
- # Float type
75
- # Using a Integer type causes a validates_is_number
76
- # validator to be created for the property. integer_only
77
- # is set to false, and precision/scale match the property
78
- #
79
- #
80
- # Messages
81
- #
82
- # :messages => {..}
83
- # Setting :messages hash replaces standard error messages
84
- # with custom ones. For instance:
85
- # :messages => {:presence => "Field is required",
86
- # :format => "Field has invalid format"}
87
- # Hash keys are: :presence, :format, :length, :is_unique,
88
- # :is_number, :is_primitive
89
- #
90
- # :message => "Some message"
91
- # It is just shortcut if only one validation option is set
92
- #
93
- def auto_generate_validations(property)
94
- return unless ((property.autovalidation_check != true) && self.auto_validation)
95
- return if (property.options && (property.options.has_key?(:auto_validation) && !property.options[:auto_validation]) || property.read_only)
96
- # value is set by the storage system
97
- opts = {}
98
- opts[:context] = property.options[:validates] if property.options.has_key?(:validates)
99
-
100
- # presence
101
- if opts[:allow_nil] == false
102
- # validates_present property.name, opts
103
- validates_present property.name, options_with_message(opts, property, :presence)
104
- end
105
-
106
- # length
107
- if property.type == "String"
108
- # XXX: maybe length should always return a Range, with the min defaulting to 1
109
- # 52 being the max set
110
- len = property.options.fetch(:length, property.options.fetch(:size, 52))
111
- if len.is_a?(Range)
112
- opts[:within] = len
113
- else
114
- opts[:maximum] = len
115
- end
116
- # validates_length property.name, opts
117
- validates_length property.name, options_with_message(opts, property, :length)
118
- end
119
-
120
- # format
121
- if property.options.has_key?(:format)
122
- opts[:with] = property.options[:format]
123
- # validates_format property.name, opts
124
- validates_format property.name, options_with_message(opts, property, :format)
125
- end
126
-
127
- # uniqueness validator
128
- if property.options.has_key?(:unique)
129
- value = property.options[:unique]
130
- if value.is_a?(Array) || value.is_a?(Symbol)
131
- # validates_is_unique property.name, :scope => Array(value)
132
- validates_is_unique property.name, options_with_message({:scope => Array(value)}, property, :is_unique)
133
- elsif value.is_a?(TrueClass)
134
- # validates_is_unique property.name
135
- validates_is_unique property.name, options_with_message({}, property, :is_unique)
136
- end
137
- end
138
-
139
- # within validator
140
- if property.options.has_key?(:set)
141
- validates_within property.name, options_with_message({:set => property.options[:set]}, property, :within)
142
- end
143
-
144
- # numeric validator
145
- if "Integer" == property.type
146
- opts[:integer_only] = true
147
- # validates_is_number property.name, opts
148
- validates_is_number property.name, options_with_message(opts, property, :is_number)
149
- elsif Float == property.type
150
- opts[:precision] = property.precision
151
- opts[:scale] = property.scale
152
- # validates_is_number property.name, opts
153
- validates_is_number property.name, options_with_message(opts, property, :is_number)
154
- end
155
-
156
- # marked the property has checked
157
- property.autovalidation_check = true
158
-
159
- end
160
-
161
- end # module AutoValidate
162
- end # module Validation
163
- end # module CouchRest
@@ -1,78 +0,0 @@
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