mattetti-couchrest 0.15 → 0.16

Sign up to get free protection for your applications and to get access to all the features.
data/lib/couchrest.rb CHANGED
@@ -27,13 +27,13 @@ require 'couchrest/monkeypatches'
27
27
 
28
28
  # = CouchDB, close to the metal
29
29
  module CouchRest
30
- VERSION = '0.15'
30
+ VERSION = '0.16' unless self.const_defined?("VERSION")
31
31
 
32
32
  autoload :Server, 'couchrest/core/server'
33
33
  autoload :Database, 'couchrest/core/database'
34
- autoload :Response, 'couchrest/core/response'
34
+ autoload :Response, 'couchrest/core/response'
35
35
  autoload :Document, 'couchrest/core/document'
36
- autoload :Design, 'couchrest/core/design'
36
+ autoload :Design, 'couchrest/core/design'
37
37
  autoload :View, 'couchrest/core/view'
38
38
  autoload :Model, 'couchrest/core/model'
39
39
  autoload :Pager, 'couchrest/helper/pager'
@@ -79,7 +79,7 @@ module CouchRest
79
79
  def next_uuid(count = @uuid_batch_count)
80
80
  @uuids ||= []
81
81
  if @uuids.empty?
82
- @uuids = CouchRest.post("#{@uri}/_uuids?count=#{count}")["uuids"]
82
+ @uuids = CouchRest.get("#{@uri}/_uuids?count=#{count}")["uuids"]
83
83
  end
84
84
  @uuids.pop
85
85
  end
@@ -325,7 +325,7 @@ module CouchRest
325
325
  end
326
326
 
327
327
  module ClassMethods
328
- CHAINS = {:before => :before, :around => :before, :after => :after}
328
+ CHAINS = {:before => :before, :around => :before, :after => :after} unless self.const_defined?("CHAINS")
329
329
 
330
330
  # Make the _run_save_callbacks method. The generated method takes
331
331
  # a block that it'll yield to. It'll call the before and around filters
@@ -14,7 +14,7 @@ module CouchRest
14
14
 
15
15
  # reads the data from an attachment
16
16
  def read_attachment(attachment_name)
17
- Base64.decode64(database.fetch_attachment(self.id, attachment_name))
17
+ Base64.decode64(database.fetch_attachment(self, attachment_name))
18
18
  end
19
19
 
20
20
  # modifies a file attachment on the current doc
@@ -1,4 +1,5 @@
1
1
  begin
2
+ # still required for Time parsing and pluralization in the validation
2
3
  require 'extlib'
3
4
  rescue
4
5
  puts "CouchRest::ExtendedDocument still requires extlib (not for much longer). This is left out of the gemspec on purpose."
@@ -247,9 +247,11 @@ describe CouchRest::Database do
247
247
  @db.save_doc(@doc)
248
248
  end
249
249
 
250
- it "should get the attachment with the doc's _id" do
251
- @db.fetch_attachment("mydocwithattachment", "test.html").should == @attach
252
- end
250
+ # Depreacated
251
+ # it "should get the attachment with the doc's _id" do
252
+ # @db.fetch_attachment("mydocwithattachment", "test.html").should == @attach
253
+ # end
254
+
253
255
  it "should get the attachment with the doc itself" do
254
256
  @db.fetch_attachment(@db.get('mydocwithattachment'), 'test.html').should == @attach
255
257
  end
@@ -266,7 +268,8 @@ describe CouchRest::Database do
266
268
  it "should save the attachment to a new doc" do
267
269
  r = @db.put_attachment({'_id' => 'attach-this'}, 'couchdb.png', image = @file.read, {:content_type => 'image/png'})
268
270
  r['ok'].should == true
269
- attachment = @db.fetch_attachment("attach-this","couchdb.png")
271
+ doc = @db.get("attach-this")
272
+ attachment = @db.fetch_attachment(doc,"couchdb.png")
270
273
  attachment.should == image
271
274
  end
272
275
  end
@@ -274,7 +277,7 @@ describe CouchRest::Database do
274
277
  describe "PUT document with attachment" do
275
278
  before(:each) do
276
279
  @attach = "<html><head><title>My Doc</title></head><body><p>Has words.</p></body></html>"
277
- @doc = {
280
+ doc = {
278
281
  "_id" => "mydocwithattachment",
279
282
  "field" => ["some value"],
280
283
  "_attachments" => {
@@ -284,14 +287,14 @@ describe CouchRest::Database do
284
287
  }
285
288
  }
286
289
  }
287
- @db.save_doc(@doc)
290
+ @db.save_doc(doc)
291
+ @doc = @db.get("mydocwithattachment")
288
292
  end
289
293
  it "should save and be indicated" do
290
- doc = @db.get("mydocwithattachment")
291
- doc['_attachments']['test.html']['length'].should == @attach.length
294
+ @doc['_attachments']['test.html']['length'].should == @attach.length
292
295
  end
293
296
  it "should be there" do
294
- attachment = @db.fetch_attachment("mydocwithattachment","test.html")
297
+ attachment = @db.fetch_attachment(@doc,"test.html")
295
298
  attachment.should == @attach
296
299
  end
297
300
  end
@@ -309,14 +312,14 @@ describe CouchRest::Database do
309
312
  }
310
313
  }
311
314
  @db.save_doc(doc)
312
- doc = @db.get('mydocwithattachment')
313
315
  doc['field'] << 'another value'
314
- @db.save_doc(doc)
316
+ @db.save_doc(doc).should be_true
315
317
  end
316
318
 
317
319
  it 'should be there' do
318
- attachment = @db.fetch_attachment('mydocwithattachment', 'test.html')
319
- attachment.should == @attach
320
+ doc = @db.get('mydocwithattachment')
321
+ attachment = @db.fetch_attachment(doc, 'test.html')
322
+ Base64.decode64(attachment).should == @attach
320
323
  end
321
324
  end
322
325
 
@@ -339,18 +342,18 @@ describe CouchRest::Database do
339
342
  }
340
343
  }
341
344
  @db.save_doc(@doc)
345
+ @doc = @db.get("mydocwithattachment")
342
346
  end
343
347
  it "should save and be indicated" do
344
- doc = @db.get("mydocwithattachment")
345
- doc['_attachments']['test.html']['length'].should == @attach.length
346
- doc['_attachments']['other.html']['length'].should == @attach2.length
348
+ @doc['_attachments']['test.html']['length'].should == @attach.length
349
+ @doc['_attachments']['other.html']['length'].should == @attach2.length
347
350
  end
348
351
  it "should be there" do
349
- attachment = @db.fetch_attachment("mydocwithattachment","test.html")
352
+ attachment = @db.fetch_attachment(@doc,"test.html")
350
353
  attachment.should == @attach
351
354
  end
352
355
  it "should be there" do
353
- attachment = @db.fetch_attachment("mydocwithattachment","other.html")
356
+ attachment = @db.fetch_attachment(@doc,"other.html")
354
357
  attachment.should == @attach2
355
358
  end
356
359
  end
@@ -370,9 +373,9 @@ describe CouchRest::Database do
370
373
  @doc = @db.get('mydocwithattachment')
371
374
  end
372
375
  it "should delete the attachment" do
373
- lambda { @db.fetch_attachment('mydocwithattachment','test.html') }.should_not raise_error
376
+ lambda { @db.fetch_attachment(@doc,'test.html') }.should_not raise_error
374
377
  @db.delete_attachment(@doc, "test.html")
375
- lambda { @db.fetch_attachment('mydocwithattachment','test.html') }.should raise_error(RestClient::ResourceNotFound)
378
+ lambda { @db.fetch_attachment(@doc,'test.html') }.should raise_error(RestClient::ResourceNotFound)
376
379
  end
377
380
  end
378
381
 
@@ -395,7 +398,8 @@ describe CouchRest::Database do
395
398
  doc['_attachments']['http://example.com/stuff.cgi?things=and%20stuff']['length'].should == @attach.length
396
399
  end
397
400
  it "should be there" do
398
- attachment = @db.fetch_attachment(@docid,"http://example.com/stuff.cgi?things=and%20stuff")
401
+ doc = @db.get(@docid)
402
+ attachment = @db.fetch_attachment(doc,"http://example.com/stuff.cgi?things=and%20stuff")
399
403
  attachment.should == @attach
400
404
  end
401
405
  end
@@ -737,4 +741,4 @@ describe CouchRest::Database do
737
741
  end
738
742
 
739
743
 
740
- end
744
+ end
@@ -439,12 +439,10 @@ describe "ExtendedDocument" do
439
439
  result.should == true
440
440
  end
441
441
  it "should be resavable" do
442
- pending "TO FIX" do
443
- @dobj.destroy
444
- @dobj.rev.should be_nil
445
- @dobj.id.should be_nil
446
- @dobj.save.should == true
447
- end
442
+ @dobj.destroy
443
+ @dobj.rev.should be_nil
444
+ @dobj.id.should be_nil
445
+ @dobj.save.should == true
448
446
  end
449
447
  it "should make it go away" do
450
448
  @dobj.destroy
@@ -0,0 +1,34 @@
1
+ class Article < CouchRest::ExtendedDocument
2
+ use_database TEST_SERVER.default_database
3
+ unique_id :slug
4
+
5
+ view_by :date, :descending => true
6
+ view_by :user_id, :date
7
+
8
+ view_by :tags,
9
+ :map =>
10
+ "function(doc) {
11
+ if (doc['couchrest-type'] == 'Article' && doc.tags) {
12
+ doc.tags.forEach(function(tag){
13
+ emit(tag, 1);
14
+ });
15
+ }
16
+ }",
17
+ :reduce =>
18
+ "function(keys, values, rereduce) {
19
+ return sum(values);
20
+ }"
21
+
22
+ property :date
23
+ property :slug, :read_only => true
24
+ property :title
25
+ property :tags
26
+
27
+ timestamps!
28
+
29
+ save_callback :before, :generate_slug_from_title
30
+
31
+ def generate_slug_from_title
32
+ self['slug'] = title.downcase.gsub(/[^a-z0-9]/,'-').squeeze('-').gsub(/^\-|\-$/,'') if new_document?
33
+ end
34
+ end
@@ -0,0 +1,14 @@
1
+ require File.join(FIXTURE_PATH, 'more', 'question')
2
+ require File.join(FIXTURE_PATH, 'more', 'person')
3
+
4
+ class Course < CouchRest::ExtendedDocument
5
+ use_database TEST_SERVER.default_database
6
+
7
+ property :title
8
+ property :questions, :cast_as => ['Question']
9
+ property :professor, :cast_as => 'Person'
10
+ property :final_test_at, :cast_as => 'Time'
11
+
12
+ view_by :title
13
+ view_by :dept, :ducktype => true
14
+ end
@@ -0,0 +1,6 @@
1
+ class Event < CouchRest::ExtendedDocument
2
+ use_database TEST_SERVER.default_database
3
+
4
+ property :subject
5
+ property :occurs_at, :cast_as => 'Time', :send => 'parse'
6
+ end
@@ -0,0 +1,8 @@
1
+ class Person < Hash
2
+ include ::CouchRest::CastedModel
3
+ property :name
4
+
5
+ def last_name
6
+ name.last
7
+ end
8
+ end
@@ -0,0 +1,6 @@
1
+ class Question < Hash
2
+ include ::CouchRest::CastedModel
3
+
4
+ property :q
5
+ property :a
6
+ end
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.15"
4
+ version: "0.16"
5
5
  platform: ruby
6
6
  authors:
7
7
  - J. Chris Anderson