mattetti-couchrest 0.31 → 0.32

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.
data/history.txt CHANGED
@@ -1,3 +1,18 @@
1
+ == 0.32
2
+
3
+ * Major enhancements
4
+
5
+ * ExtendedDocument.get doesn't raise an exception anymore. If no documents are found nil is returned.
6
+ * ExtendedDocument.get! works the say #get used to work and will raise an exception if a document isn't found.
7
+
8
+ * Minor enhancements
9
+
10
+ * Bug fix: Model.all(:keys => [1,2]) was not working (Matt Aimonetti)
11
+ * Added ValidationErrors#count in order to play nicely with Rails (Peter Wagenet)
12
+ * Bug fix: class proxy design doc refresh (Daniel Kirsh)
13
+ * Bug fix: the count method on the proxy collection was missing (Daniel Kirsch)
14
+ * Added #amount_pages to a paginated collection. (Matt Aimonetti)
15
+
1
16
  == 0.31
2
17
 
3
18
  * Major enhancements
@@ -59,6 +59,10 @@ module CouchRest
59
59
  @klass.all({:database => @database}.merge(opts), &block)
60
60
  end
61
61
 
62
+ def count(opts = {}, &block)
63
+ @klass.all({:database => @database, :raw => true, :limit => 0}.merge(opts), &block)['total_rows']
64
+ end
65
+
62
66
  def first(opts = {})
63
67
  @klass.first({:database => @database}.merge(opts))
64
68
  end
@@ -100,7 +104,7 @@ module CouchRest
100
104
  end
101
105
 
102
106
  def refresh_design_doc
103
- @klass.refresh_design_doc
107
+ @klass.refresh_design_doc_on(@database)
104
108
  end
105
109
 
106
110
  def save_design_doc
@@ -83,6 +83,8 @@ module CouchRest
83
83
 
84
84
  DEFAULT_PAGE = 1
85
85
  DEFAULT_PER_PAGE = 30
86
+
87
+ attr_accessor :amount_pages
86
88
 
87
89
  # Create a new CollectionProxy to represent the specified view. If a
88
90
  # container class is specified, the proxy will create an object of the
@@ -110,7 +112,8 @@ module CouchRest
110
112
  # See Collection.paginate
111
113
  def paginate(options = {})
112
114
  page, per_page = parse_options(options)
113
- results = @database.view(@view_name, pagination_options(page, per_page))
115
+ results = @database.view(@view_name, pagination_options(page, per_page))
116
+ @amount_pages ||= (results['total_rows'].to_f / per_page.to_f).ceil
114
117
  remember_where_we_left_off(results, page)
115
118
  convert_to_container_array(results)
116
119
  end
@@ -35,7 +35,7 @@ module CouchRest
35
35
  'all' => {
36
36
  'map' => "function(doc) {
37
37
  if (doc['couchrest-type'] == '#{self.to_s}') {
38
- emit(null,1);
38
+ emit(doc['_id'],1);
39
39
  }
40
40
  }"
41
41
  }
@@ -48,6 +48,11 @@ module CouchRest
48
48
  save_design_doc
49
49
  end
50
50
 
51
+ def refresh_design_doc_on(db)
52
+ reset_design_doc
53
+ save_design_doc_on(db)
54
+ end
55
+
51
56
  # Save the design doc onto the default database, and update the
52
57
  # design_doc attribute
53
58
  def save_design_doc
@@ -39,7 +39,38 @@ module CouchRest
39
39
  end
40
40
 
41
41
  # Load a document from the database by id
42
+ # No exceptions will be raised if the document isn't found
43
+ #
44
+ # ==== Returns
45
+ # Object:: if the document was found
46
+ # or
47
+ # Nil::
48
+ #
49
+ # === Parameters
50
+ # id<String, Integer>:: Document ID
51
+ # db<Database>:: optional option to pass a custom database to use
42
52
  def get(id, db = database)
53
+ begin
54
+ doc = db.get id
55
+ rescue
56
+ nil
57
+ else
58
+ new(doc)
59
+ end
60
+ end
61
+
62
+ # Load a document from the database by id
63
+ # An exception will be raised if the document isn't found
64
+ #
65
+ # ==== Returns
66
+ # Object:: if the document was found
67
+ # or
68
+ # Exception
69
+ #
70
+ # === Parameters
71
+ # id<String, Integer>:: Document ID
72
+ # db<Database>:: optional option to pass a custom database to use
73
+ def get!(id, db = database)
43
74
  doc = db.get id
44
75
  new(doc)
45
76
  end
@@ -95,11 +95,11 @@ module CouchRest
95
95
 
96
96
  # Dispatches to any named view.
97
97
  def view(name, query={}, &block)
98
- unless design_doc_fresh
99
- refresh_design_doc
98
+ db = query.delete(:database) || database
99
+ unless design_doc_fresh
100
+ refresh_design_doc_on(db)
100
101
  end
101
102
  query[:raw] = true if query[:reduce]
102
- db = query.delete(:database) || database
103
103
  raw = query.delete(:raw)
104
104
  fetch_view_with_docs(db, name, query, raw, &block)
105
105
  end
@@ -104,6 +104,13 @@ module CouchRest
104
104
  entries.empty?
105
105
  end
106
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
+
107
114
  def method_missing(meth, *args, &block)
108
115
  errors.send(meth, *args, &block)
109
116
  end
data/lib/couchrest.rb CHANGED
@@ -28,7 +28,7 @@ require 'couchrest/monkeypatches'
28
28
 
29
29
  # = CouchDB, close to the metal
30
30
  module CouchRest
31
- VERSION = '0.31' unless self.const_defined?("VERSION")
31
+ VERSION = '0.32' unless self.const_defined?("VERSION")
32
32
 
33
33
  autoload :Server, 'couchrest/core/server'
34
34
  autoload :Database, 'couchrest/core/database'
@@ -216,6 +216,15 @@ describe "ExtendedDocument" do
216
216
  foundart = Article.get @art.id
217
217
  foundart.title.should == "All About Getting"
218
218
  end
219
+
220
+ it "should return nil if `get` is used and the document doesn't exist" do
221
+ foundart = Article.get 'matt aimonetti'
222
+ foundart.should be_nil
223
+ end
224
+
225
+ it "should raise an error if `get!` is used and the document doesn't exist" do
226
+ lambda{foundart = Article.get!('matt aimonetti')}.should raise_error
227
+ end
219
228
  end
220
229
 
221
230
  describe "getting a model with a subobjects array" do
@@ -507,7 +516,7 @@ describe "ExtendedDocument" do
507
516
  end
508
517
  it "should make it go away" do
509
518
  @dobj.destroy
510
- lambda{Basic.get(@dobj.id)}.should raise_error
519
+ lambda{Basic.get!(@dobj.id)}.should raise_error
511
520
  end
512
521
  end
513
522
 
@@ -168,8 +168,11 @@ describe "ExtendedDocument views" do
168
168
  end
169
169
  things[0]["doc"]["title"].should =='aaa'
170
170
  end
171
- it "should barf on get if no database given" do
172
- lambda{Unattached.get("aaa")}.should raise_error
171
+ it "should return nil on get if no database given" do
172
+ Unattached.get("aaa").should be_nil
173
+ end
174
+ it "should barf on get! if no database given" do
175
+ lambda{Unattached.get!("aaa")}.should raise_error
173
176
  end
174
177
  it "should get from specific database" do
175
178
  u = Unattached.get(@first_id, @db)
@@ -200,7 +203,7 @@ describe "ExtendedDocument views" do
200
203
  before(:all) do
201
204
  reset_test_db!
202
205
  # setup the class default doc to save the design doc
203
- Unattached.use_database DB
206
+ Unattached.use_database nil # just to be sure it is really unattached
204
207
  @us = Unattached.on(DB)
205
208
  %w{aaa bbb ddd eee}.each do |title|
206
209
  u = @us.new(:title => title)
@@ -212,6 +215,9 @@ describe "ExtendedDocument views" do
212
215
  rs = @us.all
213
216
  rs.length.should == 4
214
217
  end
218
+ it "should count" do
219
+ @us.count.should == 4
220
+ end
215
221
  it "should make the design doc upon first query" do
216
222
  @us.by_title
217
223
  doc = @us.design_doc
@@ -365,6 +371,11 @@ describe "ExtendedDocument views" do
365
371
  articles.paginated_each(:per_page => 3) do |a|
366
372
  a.should_not be_nil
367
373
  end
374
+ end
375
+ it "should have the amount of paginated pages" do
376
+ articles = Article.by_date :key => Date.today
377
+ articles.paginate(:per_page => 3)
378
+ articles.amount_pages.should == 3
368
379
  end
369
380
  it "should provide a class method to access the collection directly" do
370
381
  articles = Article.collection_proxy_for('Article', 'by_date', :descending => true,
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mattetti-couchrest
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.31"
4
+ version: "0.32"
5
5
  platform: ruby
6
6
  authors:
7
7
  - J. Chris Anderson