jrun-couchrest 0.12.6 → 0.17.1

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 (60) hide show
  1. data/README.md +31 -6
  2. data/Rakefile +4 -1
  3. data/examples/model/example.rb +19 -13
  4. data/lib/couchrest.rb +55 -12
  5. data/lib/couchrest/core/database.rb +15 -31
  6. data/lib/couchrest/core/document.rb +40 -45
  7. data/lib/couchrest/core/response.rb +16 -0
  8. data/lib/couchrest/core/server.rb +1 -1
  9. data/lib/couchrest/helper/upgrade.rb +51 -0
  10. data/lib/couchrest/mixins.rb +4 -0
  11. data/lib/couchrest/mixins/attachments.rb +31 -0
  12. data/lib/couchrest/mixins/callbacks.rb +483 -0
  13. data/lib/couchrest/mixins/design_doc.rb +64 -0
  14. data/lib/couchrest/mixins/document_queries.rb +48 -0
  15. data/lib/couchrest/mixins/extended_attachments.rb +68 -0
  16. data/lib/couchrest/mixins/extended_document_mixins.rb +6 -0
  17. data/lib/couchrest/mixins/properties.rb +125 -0
  18. data/lib/couchrest/mixins/validation.rb +234 -0
  19. data/lib/couchrest/mixins/views.rb +168 -0
  20. data/lib/couchrest/monkeypatches.rb +68 -48
  21. data/lib/couchrest/more/casted_model.rb +28 -0
  22. data/lib/couchrest/more/extended_document.rb +217 -0
  23. data/lib/couchrest/more/property.rb +40 -0
  24. data/lib/couchrest/support/blank.rb +42 -0
  25. data/lib/couchrest/support/class.rb +191 -0
  26. data/lib/couchrest/validation/auto_validate.rb +163 -0
  27. data/lib/couchrest/validation/contextual_validators.rb +78 -0
  28. data/lib/couchrest/validation/validation_errors.rb +118 -0
  29. data/lib/couchrest/validation/validators/absent_field_validator.rb +74 -0
  30. data/lib/couchrest/validation/validators/confirmation_validator.rb +99 -0
  31. data/lib/couchrest/validation/validators/format_validator.rb +117 -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 +134 -0
  36. data/lib/couchrest/validation/validators/method_validator.rb +89 -0
  37. data/lib/couchrest/validation/validators/numeric_validator.rb +104 -0
  38. data/lib/couchrest/validation/validators/required_field_validator.rb +109 -0
  39. data/spec/couchrest/core/database_spec.rb +35 -89
  40. data/spec/couchrest/core/document_spec.rb +1 -45
  41. data/spec/couchrest/core/server_spec.rb +35 -0
  42. data/spec/couchrest/more/casted_extended_doc_spec.rb +40 -0
  43. data/spec/couchrest/more/casted_model_spec.rb +98 -0
  44. data/spec/couchrest/more/extended_doc_attachment_spec.rb +130 -0
  45. data/spec/couchrest/more/extended_doc_spec.rb +509 -0
  46. data/spec/couchrest/more/extended_doc_view_spec.rb +207 -0
  47. data/spec/couchrest/more/property_spec.rb +130 -0
  48. data/spec/couchrest/support/class_spec.rb +59 -0
  49. data/spec/fixtures/more/article.rb +34 -0
  50. data/spec/fixtures/more/card.rb +20 -0
  51. data/spec/fixtures/more/course.rb +14 -0
  52. data/spec/fixtures/more/event.rb +6 -0
  53. data/spec/fixtures/more/invoice.rb +17 -0
  54. data/spec/fixtures/more/person.rb +8 -0
  55. data/spec/fixtures/more/question.rb +6 -0
  56. data/spec/fixtures/more/service.rb +12 -0
  57. data/spec/spec_helper.rb +6 -1
  58. metadata +57 -3
  59. data/lib/couchrest/core/model.rb +0 -615
  60. data/spec/couchrest/core/model_spec.rb +0 -855
@@ -0,0 +1,109 @@
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 RequiredFieldValidator < GenericValidator
32
+
33
+ def initialize(field_name, options={})
34
+ super
35
+ @field_name, @options = field_name, options
36
+ end
37
+
38
+ def call(target)
39
+ value = target.validation_property_value(field_name)
40
+ property = target.validation_property(field_name)
41
+ return true if present?(value, property)
42
+
43
+ error_message = @options[:message] || default_error(property)
44
+ add_error(target, error_message, field_name)
45
+
46
+ false
47
+ end
48
+
49
+ protected
50
+
51
+ # Boolean property types are considered present if non-nil.
52
+ # Other property types are considered present if non-blank.
53
+ # Non-properties are considered present if non-blank.
54
+ def present?(value, property)
55
+ boolean_type?(property) ? !value.nil? : !value.blank?
56
+ end
57
+
58
+ def default_error(property)
59
+ actual = boolean_type?(property) ? :nil : :blank
60
+ ValidationErrors.default_error_message(actual, field_name)
61
+ end
62
+
63
+ # Is +property+ a boolean property?
64
+ #
65
+ # Returns true for Boolean, ParanoidBoolean, TrueClass, etc. properties.
66
+ # Returns false for other property types.
67
+ # Returns false for non-properties.
68
+ def boolean_type?(property)
69
+ property ? property.type == TrueClass : false
70
+ end
71
+
72
+ end # class RequiredFieldValidator
73
+
74
+ module ValidatesPresent
75
+
76
+ ##
77
+ # Validates that the specified attribute is present.
78
+ #
79
+ # For most property types "being present" is the same as being "not
80
+ # blank" as determined by the attribute's #blank? method. However, in
81
+ # the case of Boolean, "being present" means not nil; i.e. true or
82
+ # false.
83
+ #
84
+ # @note
85
+ # dm-core's support lib adds the blank? method to many classes,
86
+ # @see lib/dm-core/support/blank.rb (dm-core) for more information.
87
+ #
88
+ # @example [Usage]
89
+ #
90
+ # class Page
91
+ #
92
+ # property :required_attribute, String
93
+ # property :another_required, String
94
+ # property :yet_again, String
95
+ #
96
+ # validates_present :required_attribute
97
+ # validates_present :another_required, :yet_again
98
+ #
99
+ # # a call to valid? will return false unless
100
+ # # all three attributes are !blank?
101
+ # end
102
+ def validates_present(*fields)
103
+ opts = opts_from_validator_args(fields)
104
+ add_validator_to_context(opts, fields, CouchRest::Validation::RequiredFieldValidator)
105
+ end
106
+
107
+ end # module ValidatesPresent
108
+ end # module Validation
109
+ end # module CouchRest
@@ -149,8 +149,8 @@ describe CouchRest::Database do
149
149
  {"mild" => "yet local"},
150
150
  {"another" => ["set","of","keys"]}
151
151
  ])
152
- rs['new_revs'].each do |r|
153
- @db.get(r['id'])
152
+ rs.each do |r|
153
+ @db.get(r['id']).rev.should == r["rev"]
154
154
  end
155
155
  end
156
156
 
@@ -170,26 +170,10 @@ describe CouchRest::Database do
170
170
  {"_id" => "twoB", "mild" => "yet local"},
171
171
  {"another" => ["set","of","keys"]}
172
172
  ])
173
- rs['new_revs'].each do |r|
174
- @db.get(r['id'])
173
+ rs.each do |r|
174
+ @db.get(r['id']).rev.should == r["rev"]
175
175
  end
176
176
  end
177
-
178
- it "in the case of an id conflict should not insert anything" do
179
- @r = @db.save_doc({'lemons' => 'from texas', 'and' => 'how', "_id" => "oneB"})
180
-
181
- lambda do
182
- rs = @db.bulk_save([
183
- {"_id" => "oneB", "wild" => "and random"},
184
- {"_id" => "twoB", "mild" => "yet local"},
185
- {"another" => ["set","of","keys"]}
186
- ])
187
- end.should raise_error(RestClient::RequestFailed)
188
-
189
- lambda do
190
- @db.get('twoB')
191
- end.should raise_error(RestClient::ResourceNotFound)
192
- end
193
177
 
194
178
  it "should empty the bulk save cache if no documents are given" do
195
179
  @db.save_doc({"_id" => "bulk_cache_1", "val" => "test"}, true)
@@ -244,12 +228,14 @@ describe CouchRest::Database do
244
228
  }
245
229
  }
246
230
  }
247
- @db.save(@doc)
231
+ @db.save_doc(@doc)
248
232
  end
249
233
 
250
- it "should get the attachment with the doc's _id" do
251
- @db.fetch_attachment("mydocwithattachment", "test.html").should == @attach
252
- end
234
+ # Depreacated
235
+ # it "should get the attachment with the doc's _id" do
236
+ # @db.fetch_attachment("mydocwithattachment", "test.html").should == @attach
237
+ # end
238
+
253
239
  it "should get the attachment with the doc itself" do
254
240
  @db.fetch_attachment(@db.get('mydocwithattachment'), 'test.html').should == @attach
255
241
  end
@@ -266,7 +252,8 @@ describe CouchRest::Database do
266
252
  it "should save the attachment to a new doc" do
267
253
  r = @db.put_attachment({'_id' => 'attach-this'}, 'couchdb.png', image = @file.read, {:content_type => 'image/png'})
268
254
  r['ok'].should == true
269
- attachment = @db.fetch_attachment("attach-this","couchdb.png")
255
+ doc = @db.get("attach-this")
256
+ attachment = @db.fetch_attachment(doc,"couchdb.png")
270
257
  attachment.should == image
271
258
  end
272
259
  end
@@ -274,7 +261,7 @@ describe CouchRest::Database do
274
261
  describe "PUT document with attachment" do
275
262
  before(:each) do
276
263
  @attach = "<html><head><title>My Doc</title></head><body><p>Has words.</p></body></html>"
277
- @doc = {
264
+ doc = {
278
265
  "_id" => "mydocwithattachment",
279
266
  "field" => ["some value"],
280
267
  "_attachments" => {
@@ -284,14 +271,14 @@ describe CouchRest::Database do
284
271
  }
285
272
  }
286
273
  }
287
- @db.save_doc(@doc)
274
+ @db.save_doc(doc)
275
+ @doc = @db.get("mydocwithattachment")
288
276
  end
289
277
  it "should save and be indicated" do
290
- doc = @db.get("mydocwithattachment")
291
- doc['_attachments']['test.html']['length'].should == @attach.length
278
+ @doc['_attachments']['test.html']['length'].should == @attach.length
292
279
  end
293
280
  it "should be there" do
294
- attachment = @db.fetch_attachment("mydocwithattachment","test.html")
281
+ attachment = @db.fetch_attachment(@doc,"test.html")
295
282
  attachment.should == @attach
296
283
  end
297
284
  end
@@ -309,14 +296,15 @@ describe CouchRest::Database do
309
296
  }
310
297
  }
311
298
  @db.save_doc(doc)
312
- doc = @db.get('mydocwithattachment')
299
+ doc['_rev'].should_not be_nil
313
300
  doc['field'] << 'another value'
314
- @db.save_doc(doc)
301
+ @db.save_doc(doc)["ok"].should be_true
315
302
  end
316
303
 
317
304
  it 'should be there' do
318
- attachment = @db.fetch_attachment('mydocwithattachment', 'test.html')
319
- attachment.should == @attach
305
+ doc = @db.get('mydocwithattachment')
306
+ attachment = @db.fetch_attachment(doc, 'test.html')
307
+ Base64.decode64(attachment).should == @attach
320
308
  end
321
309
  end
322
310
 
@@ -339,18 +327,18 @@ describe CouchRest::Database do
339
327
  }
340
328
  }
341
329
  @db.save_doc(@doc)
330
+ @doc = @db.get("mydocwithattachment")
342
331
  end
343
332
  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
333
+ @doc['_attachments']['test.html']['length'].should == @attach.length
334
+ @doc['_attachments']['other.html']['length'].should == @attach2.length
347
335
  end
348
336
  it "should be there" do
349
- attachment = @db.fetch_attachment("mydocwithattachment","test.html")
337
+ attachment = @db.fetch_attachment(@doc,"test.html")
350
338
  attachment.should == @attach
351
339
  end
352
340
  it "should be there" do
353
- attachment = @db.fetch_attachment("mydocwithattachment","other.html")
341
+ attachment = @db.fetch_attachment(@doc,"other.html")
354
342
  attachment.should == @attach2
355
343
  end
356
344
  end
@@ -366,13 +354,13 @@ describe CouchRest::Database do
366
354
  }
367
355
  }
368
356
  }
369
- @db.save(doc)
357
+ @db.save_doc(doc)
370
358
  @doc = @db.get('mydocwithattachment')
371
359
  end
372
360
  it "should delete the attachment" do
373
- lambda { @db.fetch_attachment('mydocwithattachment','test.html') }.should_not raise_error
361
+ lambda { @db.fetch_attachment(@doc,'test.html') }.should_not raise_error
374
362
  @db.delete_attachment(@doc, "test.html")
375
- lambda { @db.fetch_attachment('mydocwithattachment','test.html') }.should raise_error(RestClient::ResourceNotFound)
363
+ lambda { @db.fetch_attachment(@doc,'test.html') }.should raise_error(RestClient::ResourceNotFound)
376
364
  end
377
365
  end
378
366
 
@@ -395,7 +383,8 @@ describe CouchRest::Database do
395
383
  doc['_attachments']['http://example.com/stuff.cgi?things=and%20stuff']['length'].should == @attach.length
396
384
  end
397
385
  it "should be there" do
398
- attachment = @db.fetch_attachment(@docid,"http://example.com/stuff.cgi?things=and%20stuff")
386
+ doc = @db.get(@docid)
387
+ attachment = @db.fetch_attachment(doc,"http://example.com/stuff.cgi?things=and%20stuff")
399
388
  attachment.should == @attach
400
389
  end
401
390
  end
@@ -565,49 +554,6 @@ describe CouchRest::Database do
565
554
  end
566
555
  end
567
556
 
568
- describe "MOVE existing document" do
569
- before :each do
570
- @r = @db.save_doc({'artist' => 'Zappa', 'title' => 'Muffin Man'})
571
- @docid = 'tracks/zappa/muffin-man'
572
- @doc = @db.get(@r['id'])
573
- end
574
- describe "to a new location" do
575
- it "should work" do
576
- @db.move_doc @doc, @docid
577
- newdoc = @db.get(@docid)
578
- newdoc['artist'].should == 'Zappa'
579
- lambda {@db.get(@r['id'])}.should raise_error(RestClient::ResourceNotFound)
580
- end
581
- it "should fail without an _id or _rev" do
582
- lambda{@db.move({"not"=>"a real doc"})}.should raise_error(ArgumentError)
583
- lambda{@db.move({"_id"=>"not a real doc"})}.should raise_error(ArgumentError)
584
- end
585
- end
586
- describe "to an existing location" do
587
- before :each do
588
- @db.save_doc({'_id' => @docid, 'will-exist' => 'here'})
589
- end
590
- it "should fail without a rev" do
591
- lambda{@db.move_doc @doc, @docid}.should raise_error(RestClient::RequestFailed)
592
- lambda{@db.get(@r['id'])}.should_not raise_error
593
- end
594
- it "should succeed with a rev" do
595
- @to_be_overwritten = @db.get(@docid)
596
- @db.move_doc @doc, "#{@docid}?rev=#{@to_be_overwritten['_rev']}"
597
- newdoc = @db.get(@docid)
598
- newdoc['artist'].should == 'Zappa'
599
- lambda {@db.get(@r['id'])}.should raise_error(RestClient::ResourceNotFound)
600
- end
601
- it "should succeed given the doc to overwrite" do
602
- @to_be_overwritten = @db.get(@docid)
603
- @db.move_doc @doc, @to_be_overwritten
604
- newdoc = @db.get(@docid)
605
- newdoc['artist'].should == 'Zappa'
606
- lambda {@db.get(@r['id'])}.should raise_error(RestClient::ResourceNotFound)
607
- end
608
- end
609
- end
610
-
611
557
 
612
558
  it "should list documents" do
613
559
  5.times do
@@ -672,7 +618,7 @@ describe CouchRest::Database do
672
618
 
673
619
  describe "replicating a database" do
674
620
  before do
675
- @db.save({'_id' => 'test_doc', 'some-value' => 'foo'})
621
+ @db.save_doc({'_id' => 'test_doc', 'some-value' => 'foo'})
676
622
  @other_db = @cr.database 'couchrest-test-replication'
677
623
  @other_db.delete! rescue nil
678
624
  @other_db = @cr.create_db 'couchrest-test-replication'
@@ -704,7 +650,7 @@ describe CouchRest::Database do
704
650
  describe "creating a database" do
705
651
  before(:each) do
706
652
  @db = @cr.database('couchrest-test-db_to_create')
707
- @db.delete!
653
+ @db.delete! if @cr.databases.include?('couchrest-test-db_to_create')
708
654
  end
709
655
 
710
656
  it "should just work fine" do
@@ -742,4 +688,4 @@ describe CouchRest::Database do
742
688
  end
743
689
 
744
690
 
745
- end
691
+ end
@@ -199,57 +199,13 @@ describe CouchRest::Document do
199
199
  end
200
200
  end
201
201
  end
202
-
203
- describe "MOVE existing document" do
204
- before :each do
205
- @db = reset_test_db!
206
- @resp = @db.save_doc({'key' => 'value'})
207
- @docid = 'new-location'
208
- @doc = @db.get(@resp['id'])
209
- end
210
- describe "to a new location" do
211
- it "should work" do
212
- @doc.move @docid
213
- newdoc = @db.get(@docid)
214
- newdoc['key'].should == 'value'
215
- lambda {@db.get(@resp['id'])}.should raise_error(RestClient::ResourceNotFound)
216
- end
217
- it "should fail without a database" do
218
- lambda{CouchRest::Document.new({"not"=>"a real doc"}).move}.should raise_error(ArgumentError)
219
- lambda{CouchRest::Document.new({"_id"=>"not a real doc"}).move}.should raise_error(ArgumentError)
220
- end
221
- end
222
- describe "to an existing location" do
223
- before :each do
224
- @db.save_doc({'_id' => @docid, 'will-exist' => 'here'})
225
- end
226
- it "should fail without a rev" do
227
- lambda{@doc.move @docid}.should raise_error(RestClient::RequestFailed)
228
- lambda{@db.get(@resp['id'])}.should_not raise_error
229
- end
230
- it "should succeed with a rev" do
231
- @to_be_overwritten = @db.get(@docid)
232
- @doc.move "#{@docid}?rev=#{@to_be_overwritten['_rev']}"
233
- newdoc = @db.get(@docid)
234
- newdoc['key'].should == 'value'
235
- lambda {@db.get(@resp['id'])}.should raise_error(RestClient::ResourceNotFound)
236
- end
237
- it "should succeed given the doc to overwrite" do
238
- @to_be_overwritten = @db.get(@docid)
239
- @doc.move @to_be_overwritten
240
- newdoc = @db.get(@docid)
241
- newdoc['key'].should == 'value'
242
- lambda {@db.get(@resp['id'])}.should raise_error(RestClient::ResourceNotFound)
243
- end
244
- end
245
- end
246
202
  end
247
203
 
248
204
  describe "dealing with attachments" do
249
205
  before do
250
206
  @db = reset_test_db!
251
207
  @attach = "<html><head><title>My Doc</title></head><body><p>Has words.</p></body></html>"
252
- response = @db.save({'key' => 'value'})
208
+ response = @db.save_doc({'key' => 'value'})
253
209
  @doc = @db.get(response['id'])
254
210
  end
255
211
 
@@ -0,0 +1,35 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe CouchRest::Server do
4
+
5
+ describe "available databases" do
6
+ before(:each) do
7
+ @couch = CouchRest::Server.new
8
+ end
9
+
10
+ after(:each) do
11
+ @couch.available_databases.each do |ref, db|
12
+ db.delete!
13
+ end
14
+ end
15
+
16
+ it "should let you add more databases" do
17
+ @couch.available_databases.should be_empty
18
+ @couch.define_available_database(:default, "cr-server-test-db")
19
+ @couch.available_databases.keys.should include(:default)
20
+ end
21
+
22
+ it "should verify that a database is available" do
23
+ @couch.define_available_database(:default, "cr-server-test-db")
24
+ @couch.available_database?(:default).should be_true
25
+ @couch.available_database?("cr-server-test-db").should be_true
26
+ @couch.available_database?(:matt).should be_false
27
+ end
28
+
29
+ it "should let you set a default database" do
30
+ @couch.default_database = 'cr-server-test-default-db'
31
+ @couch.available_database?(:default).should be_true
32
+ end
33
+ end
34
+
35
+ end
@@ -0,0 +1,40 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')
2
+ require File.join(FIXTURE_PATH, 'more', 'card')
3
+
4
+ class Car < CouchRest::ExtendedDocument
5
+ use_database TEST_SERVER.default_database
6
+
7
+ property :name
8
+ property :driver, :cast_as => 'Driver'
9
+ end
10
+
11
+ class Driver < CouchRest::ExtendedDocument
12
+ use_database TEST_SERVER.default_database
13
+ # You have to add a casted_by accessor if you want to reach a casted extended doc parent
14
+ attr_accessor :casted_by
15
+
16
+ property :name
17
+ end
18
+
19
+ describe "casting an extended document" do
20
+
21
+ before(:each) do
22
+ @car = Car.new(:name => 'Renault 306')
23
+ @driver = Driver.new(:name => 'Matt')
24
+ end
25
+
26
+ # it "should not create an empty casted object" do
27
+ # @car.driver.should be_nil
28
+ # end
29
+
30
+ it "should let you assign the casted attribute after instantializing an object" do
31
+ @car.driver = @driver
32
+ @car.driver.name.should == 'Matt'
33
+ end
34
+
35
+ it "should let the casted document who casted it" do
36
+ Car.new(:name => 'Renault 306', :driver => @driver)
37
+ @car.driver.casted_by.should == @car
38
+ end
39
+
40
+ end