couchrest 0.38 → 1.0.0.beta

Sign up to get free protection for your applications and to get access to all the features.
Files changed (77) hide show
  1. data/README.md +8 -8
  2. data/Rakefile +3 -4
  3. data/couchrest.gemspec +25 -105
  4. data/history.txt +5 -4
  5. data/lib/couchrest.rb +31 -52
  6. data/lib/couchrest/{core/database.rb → database.rb} +6 -11
  7. data/lib/couchrest/{core/design.rb → design.rb} +2 -2
  8. data/lib/couchrest/{core/document.rb → document.rb} +1 -1
  9. data/lib/couchrest/helper/attachments.rb +29 -0
  10. data/lib/couchrest/middlewares/logger.rb +3 -3
  11. data/lib/couchrest/monkeypatches.rb +1 -71
  12. data/lib/couchrest/{core/response.rb → response.rb} +0 -0
  13. data/lib/couchrest/{core/rest_api.rb → rest_api.rb} +8 -12
  14. data/lib/couchrest/{core/server.rb → server.rb} +0 -2
  15. data/spec/couchrest/{core/couchrest_spec.rb → couchrest_spec.rb} +15 -9
  16. data/spec/couchrest/{core/database_spec.rb → database_spec.rb} +4 -4
  17. data/spec/couchrest/{core/design_spec.rb → design_spec.rb} +2 -2
  18. data/spec/couchrest/{core/document_spec.rb → document_spec.rb} +1 -1
  19. data/spec/couchrest/{core/server_spec.rb → server_spec.rb} +2 -2
  20. data/spec/spec.opts +0 -1
  21. data/spec/spec_helper.rb +0 -4
  22. metadata +32 -133
  23. data/examples/model/example.rb +0 -144
  24. data/lib/couchrest/core/adapters/restclient.rb +0 -35
  25. data/lib/couchrest/core/http_abstraction.rb +0 -48
  26. data/lib/couchrest/core/view.rb +0 -4
  27. data/lib/couchrest/mixins.rb +0 -4
  28. data/lib/couchrest/mixins/attachments.rb +0 -31
  29. data/lib/couchrest/mixins/attribute_protection.rb +0 -74
  30. data/lib/couchrest/mixins/callbacks.rb +0 -532
  31. data/lib/couchrest/mixins/class_proxy.rb +0 -124
  32. data/lib/couchrest/mixins/collection.rb +0 -260
  33. data/lib/couchrest/mixins/design_doc.rb +0 -103
  34. data/lib/couchrest/mixins/document_queries.rb +0 -80
  35. data/lib/couchrest/mixins/extended_attachments.rb +0 -70
  36. data/lib/couchrest/mixins/extended_document_mixins.rb +0 -9
  37. data/lib/couchrest/mixins/properties.rb +0 -158
  38. data/lib/couchrest/mixins/validation.rb +0 -246
  39. data/lib/couchrest/mixins/views.rb +0 -173
  40. data/lib/couchrest/more/casted_model.rb +0 -58
  41. data/lib/couchrest/more/extended_document.rb +0 -310
  42. data/lib/couchrest/more/property.rb +0 -58
  43. data/lib/couchrest/more/typecast.rb +0 -180
  44. data/lib/couchrest/support/blank.rb +0 -42
  45. data/lib/couchrest/support/rails.rb +0 -42
  46. data/lib/couchrest/validation/auto_validate.rb +0 -157
  47. data/lib/couchrest/validation/contextual_validators.rb +0 -78
  48. data/lib/couchrest/validation/validation_errors.rb +0 -125
  49. data/lib/couchrest/validation/validators/absent_field_validator.rb +0 -74
  50. data/lib/couchrest/validation/validators/confirmation_validator.rb +0 -107
  51. data/lib/couchrest/validation/validators/format_validator.rb +0 -122
  52. data/lib/couchrest/validation/validators/formats/email.rb +0 -66
  53. data/lib/couchrest/validation/validators/formats/url.rb +0 -43
  54. data/lib/couchrest/validation/validators/generic_validator.rb +0 -120
  55. data/lib/couchrest/validation/validators/length_validator.rb +0 -139
  56. data/lib/couchrest/validation/validators/method_validator.rb +0 -89
  57. data/lib/couchrest/validation/validators/numeric_validator.rb +0 -109
  58. data/lib/couchrest/validation/validators/required_field_validator.rb +0 -114
  59. data/spec/couchrest/more/attribute_protection_spec.rb +0 -150
  60. data/spec/couchrest/more/casted_extended_doc_spec.rb +0 -73
  61. data/spec/couchrest/more/casted_model_spec.rb +0 -406
  62. data/spec/couchrest/more/extended_doc_attachment_spec.rb +0 -135
  63. data/spec/couchrest/more/extended_doc_inherited_spec.rb +0 -40
  64. data/spec/couchrest/more/extended_doc_spec.rb +0 -807
  65. data/spec/couchrest/more/extended_doc_subclass_spec.rb +0 -98
  66. data/spec/couchrest/more/extended_doc_view_spec.rb +0 -456
  67. data/spec/couchrest/more/property_spec.rb +0 -628
  68. data/spec/fixtures/more/article.rb +0 -35
  69. data/spec/fixtures/more/card.rb +0 -22
  70. data/spec/fixtures/more/cat.rb +0 -20
  71. data/spec/fixtures/more/course.rb +0 -22
  72. data/spec/fixtures/more/event.rb +0 -8
  73. data/spec/fixtures/more/invoice.rb +0 -17
  74. data/spec/fixtures/more/person.rb +0 -9
  75. data/spec/fixtures/more/question.rb +0 -6
  76. data/spec/fixtures/more/service.rb +0 -12
  77. data/spec/fixtures/more/user.rb +0 -22
@@ -1,124 +0,0 @@
1
- module CouchRest
2
- module Mixins
3
- module ClassProxy
4
-
5
- def self.included(base)
6
- base.extend(ClassMethods)
7
- end
8
-
9
- module ClassMethods
10
-
11
- # Return a proxy object which represents a model class on a
12
- # chosen database instance. This allows you to DRY operations
13
- # where a database is chosen dynamically.
14
- #
15
- # ==== Example:
16
- #
17
- # db = CouchRest::Database.new(...)
18
- # articles = Article.on(db)
19
- #
20
- # articles.all { ... }
21
- # articles.by_title { ... }
22
- #
23
- # u = articles.get("someid")
24
- #
25
- # u = articles.new(:title => "I like plankton")
26
- # u.save # saved on the correct database
27
-
28
- def on(database)
29
- Proxy.new(self, database)
30
- end
31
- end
32
-
33
- class Proxy #:nodoc:
34
- def initialize(klass, database)
35
- @klass = klass
36
- @database = database
37
- end
38
-
39
- # ExtendedDocument
40
-
41
- def new(*args)
42
- doc = @klass.new(*args)
43
- doc.database = @database
44
- doc
45
- end
46
-
47
- def method_missing(m, *args, &block)
48
- if has_view?(m)
49
- query = args.shift || {}
50
- view(m, query, *args, &block)
51
- else
52
- super
53
- end
54
- end
55
-
56
- # Mixins::DocumentQueries
57
-
58
- def all(opts = {}, &block)
59
- docs = @klass.all({:database => @database}.merge(opts), &block)
60
- docs.each { |doc| doc.database = @database if doc.respond_to?(:database) } if docs
61
- docs
62
- end
63
-
64
- def count(opts = {}, &block)
65
- @klass.all({:database => @database, :raw => true, :limit => 0}.merge(opts), &block)['total_rows']
66
- end
67
-
68
- def first(opts = {})
69
- doc = @klass.first({:database => @database}.merge(opts))
70
- doc.database = @database if doc && doc.respond_to?(:database)
71
- doc
72
- end
73
-
74
- def get(id)
75
- doc = @klass.get(id, @database)
76
- doc.database = @database if doc && doc.respond_to?(:database)
77
- doc
78
- end
79
-
80
- # Mixins::Views
81
-
82
- def has_view?(view)
83
- @klass.has_view?(view)
84
- end
85
-
86
- def view(name, query={}, &block)
87
- docs = @klass.view(name, {:database => @database}.merge(query), &block)
88
- docs.each { |doc| doc.database = @database if doc.respond_to?(:database) } if docs
89
- docs
90
- end
91
-
92
- def all_design_doc_versions
93
- @klass.all_design_doc_versions(@database)
94
- end
95
-
96
- def model_design_doc
97
- @klass.model_design_doc(@database)
98
- end
99
-
100
- def cleanup_design_docs!
101
- @klass.cleanup_design_docs!(@database)
102
- end
103
-
104
- # Mixins::DesignDoc
105
-
106
- def design_doc
107
- @klass.design_doc
108
- end
109
-
110
- def design_doc_fresh
111
- @klass.design_doc_fresh
112
- end
113
-
114
- def refresh_design_doc
115
- @klass.refresh_design_doc_on(@database)
116
- end
117
-
118
- def save_design_doc
119
- @klass.save_design_doc_on(@database)
120
- end
121
- end
122
- end
123
- end
124
- end
@@ -1,260 +0,0 @@
1
- module CouchRest
2
- module Mixins
3
- module Collection
4
-
5
- def self.included(base)
6
- base.extend(ClassMethods)
7
- end
8
-
9
- module ClassMethods
10
-
11
- # Creates a new class method, find_all_<collection_name>, that will
12
- # execute the view specified with the design_doc and view_name
13
- # parameters, along with the specified view_options. This method will
14
- # return the results of the view as an Array of objects which are
15
- # instances of the class.
16
- #
17
- # This method is handy for objects that do not use the view_by method
18
- # to declare their views.
19
- def provides_collection(collection_name, design_doc, view_name, view_options)
20
- class_eval <<-END, __FILE__, __LINE__ + 1
21
- def self.find_all_#{collection_name}(options = {})
22
- view_options = #{view_options.inspect} || {}
23
- CollectionProxy.new(database, "#{design_doc}", "#{view_name}", view_options.merge(options), Kernel.const_get('#{self}'))
24
- end
25
- END
26
- end
27
-
28
- # Fetch a group of objects from CouchDB. Options can include:
29
- # :page - Specifies the page to load (starting at 1)
30
- # :per_page - Specifies the number of objects to load per page
31
- #
32
- # Defaults are used if these options are not specified.
33
- def paginate(options)
34
- proxy = create_collection_proxy(options)
35
- proxy.paginate(options)
36
- end
37
-
38
- # Iterate over the objects in a collection, fetching them from CouchDB
39
- # in groups. Options can include:
40
- # :page - Specifies the page to load
41
- # :per_page - Specifies the number of objects to load per page
42
- #
43
- # Defaults are used if these options are not specified.
44
- def paginated_each(options, &block)
45
- search = options.delete(:search)
46
- unless search == true
47
- proxy = create_collection_proxy(options)
48
- else
49
- proxy = create_search_collection_proxy(options)
50
- end
51
- proxy.paginated_each(options, &block)
52
- end
53
-
54
- # Create a CollectionProxy for the specified view and options.
55
- # CollectionProxy behaves just like an Array, but offers support for
56
- # pagination.
57
- def collection_proxy_for(design_doc, view_name, view_options = {})
58
- options = view_options.merge(:design_doc => design_doc, :view_name => view_name)
59
- create_collection_proxy(options)
60
- end
61
-
62
- private
63
-
64
- def create_collection_proxy(options)
65
- design_doc, view_name, view_options = parse_view_options(options)
66
- CollectionProxy.new(database, design_doc, view_name, view_options, self)
67
- end
68
-
69
- def create_search_collection_proxy(options)
70
- design_doc, search_name, search_options = parse_search_options(options)
71
- CollectionProxy.new(@database, design_doc, search_name, search_options, self, :search)
72
- end
73
-
74
- def parse_view_options(options)
75
- design_doc = options.delete(:design_doc)
76
- raise ArgumentError, 'design_doc is required' if design_doc.nil?
77
-
78
- view_name = options.delete(:view_name)
79
- raise ArgumentError, 'view_name is required' if view_name.nil?
80
-
81
- default_view_options = (design_doc.class == Design &&
82
- design_doc['views'][view_name.to_s] &&
83
- design_doc['views'][view_name.to_s]["couchrest-defaults"]) || {}
84
- view_options = default_view_options.merge(options)
85
-
86
- [design_doc, view_name, view_options]
87
- end
88
-
89
- def parse_search_options(options)
90
- design_doc = options.delete(:design_doc)
91
- raise ArgumentError, 'design_doc is required' if design_doc.nil?
92
-
93
- search_name = options.delete(:view_name)
94
- raise ArgumentError, 'search_name is required' if search_name.nil?
95
-
96
- search_options = options.clone
97
- [design_doc, search_name, search_options]
98
- end
99
-
100
- end
101
-
102
- class CollectionProxy
103
- alias_method :proxy_respond_to?, :respond_to?
104
- instance_methods.each { |m| undef_method m unless m =~ /(^__|^nil\?$|^send$|proxy_|^object_id$)/ }
105
-
106
- DEFAULT_PAGE = 1
107
- DEFAULT_PER_PAGE = 30
108
-
109
- # Create a new CollectionProxy to represent the specified view. If a
110
- # container class is specified, the proxy will create an object of the
111
- # given type for each row that comes back from the view. If no
112
- # container class is specified, the raw results are returned.
113
- #
114
- # The CollectionProxy provides support for paginating over a collection
115
- # via the paginate, and paginated_each methods.
116
- def initialize(database, design_doc, view_name, view_options = {}, container_class = nil, query_type = :view)
117
- raise ArgumentError, "database is a required parameter" if database.nil?
118
-
119
- @database = database
120
- @container_class = container_class
121
- @query_type = query_type
122
-
123
- strip_pagination_options(view_options)
124
- @view_options = view_options
125
-
126
- if design_doc.class == Design
127
- @view_name = "#{design_doc.name}/#{view_name}"
128
- else
129
- @view_name = "#{design_doc}/#{view_name}"
130
- end
131
- end
132
-
133
- # See Collection.paginate
134
- def paginate(options = {})
135
- page, per_page = parse_options(options)
136
- results = @database.send(@query_type, @view_name, pagination_options(page, per_page))
137
- remember_where_we_left_off(results, page)
138
- instances = convert_to_container_array(results)
139
-
140
- begin
141
- if Kernel.const_get('WillPaginate')
142
- total_rows = results['total_rows'].to_i
143
- paginated = WillPaginate::Collection.create(page, per_page, total_rows) do |pager|
144
- pager.replace(instances)
145
- end
146
- return paginated
147
- end
148
- rescue NameError
149
- # When not using will_paginate, not much we could do about this. :x
150
- end
151
- return instances
152
- end
153
-
154
- # See Collection.paginated_each
155
- def paginated_each(options = {}, &block)
156
- page, per_page = parse_options(options)
157
-
158
- begin
159
- collection = paginate({:page => page, :per_page => per_page})
160
- collection.each(&block)
161
- page += 1
162
- end until collection.size < per_page
163
- end
164
-
165
- def respond_to?(*args)
166
- proxy_respond_to?(*args) || (load_target && @target.respond_to?(*args))
167
- end
168
-
169
- # Explicitly proxy === because the instance method removal above
170
- # doesn't catch it.
171
- def ===(other)
172
- load_target
173
- other === @target
174
- end
175
-
176
- private
177
-
178
- def method_missing(method, *args)
179
- if load_target
180
- if block_given?
181
- @target.send(method, *args) { |*block_args| yield(*block_args) }
182
- else
183
- @target.send(method, *args)
184
- end
185
- end
186
- end
187
-
188
- def load_target
189
- unless loaded?
190
- @view_options.merge!({:include_docs => true}) if @query_type == :search
191
- results = @database.send(@query_type, @view_name, @view_options)
192
- @target = convert_to_container_array(results)
193
- end
194
- @loaded = true
195
- @target
196
- end
197
-
198
- def loaded?
199
- @loaded
200
- end
201
-
202
- def reload
203
- reset
204
- load_target
205
- self unless @target.nil?
206
- end
207
-
208
- def reset
209
- @loaded = false
210
- @target = nil
211
- end
212
-
213
- def inspect
214
- load_target
215
- @target.inspect
216
- end
217
-
218
- def convert_to_container_array(results)
219
- if @container_class.nil?
220
- results
221
- else
222
- results['rows'].collect { |row| @container_class.create_from_database(row['doc']) } unless results['rows'].nil?
223
- end
224
- end
225
-
226
- def pagination_options(page, per_page)
227
- view_options = @view_options.clone
228
- if @query_type == :view && @last_key && @last_docid && @last_page == page - 1
229
- key = view_options.delete(:key)
230
- end_key = view_options[:endkey] || key
231
- options = { :startkey => @last_key, :endkey => end_key, :startkey_docid => @last_docid, :limit => per_page, :skip => 1 }
232
- else
233
- options = { :limit => per_page, :skip => per_page * (page - 1) }
234
- end
235
- view_options.merge(options)
236
- end
237
-
238
- def parse_options(options)
239
- page = options.delete(:page) || DEFAULT_PAGE
240
- per_page = options.delete(:per_page) || DEFAULT_PER_PAGE
241
- [page.to_i, per_page.to_i]
242
- end
243
-
244
- def strip_pagination_options(options)
245
- parse_options(options)
246
- end
247
-
248
- def remember_where_we_left_off(results, page)
249
- last_row = results['rows'].last
250
- if last_row
251
- @last_key = last_row['key']
252
- @last_docid = last_row['id']
253
- end
254
- @last_page = page
255
- end
256
- end
257
-
258
- end
259
- end
260
- end
@@ -1,103 +0,0 @@
1
- require 'digest/md5'
2
-
3
- module CouchRest
4
- module Mixins
5
- module DesignDoc
6
-
7
- def self.included(base)
8
- base.extend(ClassMethods)
9
- end
10
-
11
- module ClassMethods
12
- attr_accessor :design_doc, :design_doc_slug_cache, :design_doc_fresh
13
-
14
- def design_doc
15
- @design_doc ||= Design.new(default_design_doc)
16
- end
17
-
18
- def design_doc_id
19
- "_design/#{design_doc_slug}"
20
- end
21
-
22
- def design_doc_slug
23
- return design_doc_slug_cache if (design_doc_slug_cache && design_doc_fresh)
24
- funcs = []
25
- design_doc['views'].each do |name, view|
26
- funcs << "#{name}/#{view['map']}#{view['reduce']}"
27
- end
28
- self.design_doc_slug_cache = self.to_s
29
- end
30
-
31
- def default_design_doc
32
- {
33
- "language" => "javascript",
34
- "views" => {
35
- 'all' => {
36
- 'map' => "function(doc) {
37
- if (doc['couchrest-type'] == '#{self.to_s}') {
38
- emit(doc['_id'],1);
39
- }
40
- }"
41
- }
42
- }
43
- }
44
- end
45
-
46
- def refresh_design_doc
47
- reset_design_doc
48
- save_design_doc
49
- end
50
-
51
- def refresh_design_doc_on(db)
52
- reset_design_doc
53
- save_design_doc_on(db)
54
- end
55
-
56
- # Save the design doc onto the default database, and update the
57
- # design_doc attribute
58
- def save_design_doc
59
- reset_design_doc unless design_doc_fresh
60
- self.design_doc = update_design_doc(design_doc)
61
- end
62
-
63
- # Save the design doc onto a target database in a thread-safe way,
64
- # not modifying the model's design_doc
65
- def save_design_doc_on(db)
66
- update_design_doc(Design.new(design_doc), db)
67
- end
68
-
69
- private
70
-
71
- def reset_design_doc
72
- current = self.database.get(design_doc_id) rescue nil
73
- design_doc['_id'] = design_doc_id
74
- if current.nil?
75
- design_doc.delete('_rev')
76
- else
77
- design_doc['_rev'] = current['_rev']
78
- end
79
- self.design_doc_fresh = true
80
- end
81
-
82
- # Writes out a design_doc to a given database, returning the
83
- # updated design doc
84
- def update_design_doc(design_doc, db = database)
85
- saved = db.get(design_doc['_id']) rescue nil
86
- if saved
87
- design_doc['views'].each do |name, view|
88
- saved['views'][name] = view
89
- end
90
- db.save_doc(saved)
91
- saved
92
- else
93
- design_doc.database = db
94
- design_doc.save
95
- design_doc
96
- end
97
- end
98
-
99
- end # module ClassMethods
100
-
101
- end
102
- end
103
- end