jchris-couchrest 0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. data/LICENSE +176 -0
  2. data/README.md +93 -0
  3. data/Rakefile +66 -0
  4. data/THANKS.md +18 -0
  5. data/examples/model/example.rb +144 -0
  6. data/examples/word_count/markov +38 -0
  7. data/examples/word_count/views/books/chunked-map.js +3 -0
  8. data/examples/word_count/views/books/united-map.js +1 -0
  9. data/examples/word_count/views/markov/chain-map.js +6 -0
  10. data/examples/word_count/views/markov/chain-reduce.js +7 -0
  11. data/examples/word_count/views/word_count/count-map.js +6 -0
  12. data/examples/word_count/views/word_count/count-reduce.js +3 -0
  13. data/examples/word_count/word_count.rb +46 -0
  14. data/examples/word_count/word_count_query.rb +40 -0
  15. data/examples/word_count/word_count_views.rb +26 -0
  16. data/lib/couchrest/commands/generate.rb +71 -0
  17. data/lib/couchrest/commands/push.rb +103 -0
  18. data/lib/couchrest/core/database.rb +313 -0
  19. data/lib/couchrest/core/design.rb +89 -0
  20. data/lib/couchrest/core/document.rb +96 -0
  21. data/lib/couchrest/core/response.rb +16 -0
  22. data/lib/couchrest/core/server.rb +88 -0
  23. data/lib/couchrest/core/view.rb +4 -0
  24. data/lib/couchrest/helper/pager.rb +103 -0
  25. data/lib/couchrest/helper/streamer.rb +44 -0
  26. data/lib/couchrest/mixins/attachments.rb +31 -0
  27. data/lib/couchrest/mixins/callbacks.rb +442 -0
  28. data/lib/couchrest/mixins/design_doc.rb +63 -0
  29. data/lib/couchrest/mixins/document_queries.rb +48 -0
  30. data/lib/couchrest/mixins/extended_attachments.rb +68 -0
  31. data/lib/couchrest/mixins/extended_document_mixins.rb +6 -0
  32. data/lib/couchrest/mixins/properties.rb +125 -0
  33. data/lib/couchrest/mixins/validation.rb +234 -0
  34. data/lib/couchrest/mixins/views.rb +169 -0
  35. data/lib/couchrest/mixins.rb +4 -0
  36. data/lib/couchrest/monkeypatches.rb +113 -0
  37. data/lib/couchrest/more/casted_model.rb +28 -0
  38. data/lib/couchrest/more/extended_document.rb +217 -0
  39. data/lib/couchrest/more/property.rb +40 -0
  40. data/lib/couchrest/support/blank.rb +42 -0
  41. data/lib/couchrest/support/class.rb +175 -0
  42. data/lib/couchrest/validation/auto_validate.rb +163 -0
  43. data/lib/couchrest/validation/contextual_validators.rb +78 -0
  44. data/lib/couchrest/validation/validation_errors.rb +118 -0
  45. data/lib/couchrest/validation/validators/absent_field_validator.rb +74 -0
  46. data/lib/couchrest/validation/validators/confirmation_validator.rb +99 -0
  47. data/lib/couchrest/validation/validators/format_validator.rb +117 -0
  48. data/lib/couchrest/validation/validators/formats/email.rb +66 -0
  49. data/lib/couchrest/validation/validators/formats/url.rb +43 -0
  50. data/lib/couchrest/validation/validators/generic_validator.rb +120 -0
  51. data/lib/couchrest/validation/validators/length_validator.rb +134 -0
  52. data/lib/couchrest/validation/validators/method_validator.rb +89 -0
  53. data/lib/couchrest/validation/validators/numeric_validator.rb +104 -0
  54. data/lib/couchrest/validation/validators/required_field_validator.rb +109 -0
  55. data/lib/couchrest.rb +189 -0
  56. data/spec/couchrest/core/couchrest_spec.rb +201 -0
  57. data/spec/couchrest/core/database_spec.rb +745 -0
  58. data/spec/couchrest/core/design_spec.rb +131 -0
  59. data/spec/couchrest/core/document_spec.rb +311 -0
  60. data/spec/couchrest/core/server_spec.rb +35 -0
  61. data/spec/couchrest/helpers/pager_spec.rb +122 -0
  62. data/spec/couchrest/helpers/streamer_spec.rb +23 -0
  63. data/spec/couchrest/more/casted_extended_doc_spec.rb +40 -0
  64. data/spec/couchrest/more/casted_model_spec.rb +97 -0
  65. data/spec/couchrest/more/extended_doc_attachment_spec.rb +129 -0
  66. data/spec/couchrest/more/extended_doc_spec.rb +509 -0
  67. data/spec/couchrest/more/extended_doc_view_spec.rb +207 -0
  68. data/spec/couchrest/more/property_spec.rb +129 -0
  69. data/spec/fixtures/attachments/README +3 -0
  70. data/spec/fixtures/attachments/couchdb.png +0 -0
  71. data/spec/fixtures/attachments/test.html +11 -0
  72. data/spec/fixtures/more/article.rb +34 -0
  73. data/spec/fixtures/more/card.rb +20 -0
  74. data/spec/fixtures/more/course.rb +14 -0
  75. data/spec/fixtures/more/event.rb +6 -0
  76. data/spec/fixtures/more/invoice.rb +17 -0
  77. data/spec/fixtures/more/person.rb +8 -0
  78. data/spec/fixtures/more/question.rb +6 -0
  79. data/spec/fixtures/more/service.rb +12 -0
  80. data/spec/fixtures/views/lib.js +3 -0
  81. data/spec/fixtures/views/test_view/lib.js +3 -0
  82. data/spec/fixtures/views/test_view/only-map.js +4 -0
  83. data/spec/fixtures/views/test_view/test-map.js +3 -0
  84. data/spec/fixtures/views/test_view/test-reduce.js +3 -0
  85. data/spec/spec.opts +6 -0
  86. data/spec/spec_helper.rb +26 -0
  87. data/utils/remap.rb +27 -0
  88. data/utils/subset.rb +30 -0
  89. metadata +217 -0
@@ -0,0 +1,97 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')
2
+ require File.join(FIXTURE_PATH, 'more', 'card')
3
+
4
+ class WithCastedModelMixin < Hash
5
+ include CouchRest::CastedModel
6
+ property :name
7
+ end
8
+
9
+ class DummyModel < CouchRest::ExtendedDocument
10
+ use_database TEST_SERVER.default_database
11
+ raise "Default DB not set" if TEST_SERVER.default_database.nil?
12
+ property :casted_attribute, :cast_as => 'WithCastedModelMixin'
13
+ property :keywords, :cast_as => ["String"]
14
+ end
15
+
16
+ describe CouchRest::CastedModel do
17
+
18
+ describe "A non hash class including CastedModel" do
19
+ it "should fail raising and include error" do
20
+ lambda do
21
+ class NotAHashButWithCastedModelMixin
22
+ include CouchRest::CastedModel
23
+ property :name
24
+ end
25
+
26
+ end.should raise_error
27
+ end
28
+ end
29
+
30
+ describe "isolated" do
31
+ before(:each) do
32
+ @obj = WithCastedModelMixin.new
33
+ end
34
+ it "should automatically include the property mixin and define getters and setters" do
35
+ @obj.name = 'Matt'
36
+ @obj.name.should == 'Matt'
37
+ end
38
+ end
39
+
40
+ describe "casted as attribute" do
41
+ before(:each) do
42
+ @obj = DummyModel.new(:casted_attribute => {:name => 'whatever'})
43
+ @casted_obj = @obj.casted_attribute
44
+ end
45
+
46
+ it "should be available from its parent" do
47
+ @casted_obj.should be_an_instance_of(WithCastedModelMixin)
48
+ end
49
+
50
+ it "should have the getters defined" do
51
+ @casted_obj.name.should == 'whatever'
52
+ end
53
+
54
+ it "should know who casted it" do
55
+ @casted_obj.casted_by.should == @obj
56
+ end
57
+ end
58
+
59
+ describe "casted as an array of a different type" do
60
+ before(:each) do
61
+ @obj = DummyModel.new(:keywords => ['couch', 'sofa', 'relax', 'canapé'])
62
+ end
63
+
64
+ it "should cast the array propery" do
65
+ @obj.keywords.should be_an_instance_of(Array)
66
+ @obj.keywords.first.should == 'couch'
67
+ end
68
+
69
+ end
70
+
71
+ describe "saved document with casted models" do
72
+ before(:each) do
73
+ @obj = DummyModel.new(:casted_attribute => {:name => 'whatever'})
74
+ @obj.save.should be_true
75
+ @obj = DummyModel.get(@obj.id)
76
+ end
77
+
78
+ it "should be able to load with the casted models" do
79
+ casted_obj = @obj.casted_attribute
80
+ casted_obj.should_not be_nil
81
+ casted_obj.should be_an_instance_of(WithCastedModelMixin)
82
+ end
83
+
84
+ it "should have defined getters for the casted model" do
85
+ casted_obj = @obj.casted_attribute
86
+ casted_obj.name.should == "whatever"
87
+ end
88
+
89
+ it "should have defined setters for the casted model" do
90
+ casted_obj = @obj.casted_attribute
91
+ casted_obj.name = "test"
92
+ casted_obj.name.should == "test"
93
+ end
94
+
95
+ end
96
+
97
+ end
@@ -0,0 +1,129 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe "ExtendedDocument attachments" do
4
+
5
+ describe "#has_attachment?" do
6
+ before(:each) do
7
+ @obj = Basic.new
8
+ @obj.save.should == true
9
+ @file = File.open(FIXTURE_PATH + '/attachments/test.html')
10
+ @attachment_name = 'my_attachment'
11
+ @obj.create_attachment(:file => @file, :name => @attachment_name)
12
+ end
13
+
14
+ it 'should return false if there is no attachment' do
15
+ @obj.has_attachment?('bogus').should be_false
16
+ end
17
+
18
+ it 'should return true if there is an attachment' do
19
+ @obj.has_attachment?(@attachment_name).should be_true
20
+ end
21
+
22
+ it 'should return true if an object with an attachment is reloaded' do
23
+ @obj.save.should be_true
24
+ reloaded_obj = Basic.get(@obj.id)
25
+ reloaded_obj.has_attachment?(@attachment_name).should be_true
26
+ end
27
+
28
+ it 'should return false if an attachment has been removed' do
29
+ @obj.delete_attachment(@attachment_name)
30
+ @obj.has_attachment?(@attachment_name).should be_false
31
+ end
32
+ end
33
+
34
+ describe "creating an attachment" do
35
+ before(:each) do
36
+ @obj = Basic.new
37
+ @obj.save.should == true
38
+ @file_ext = File.open(FIXTURE_PATH + '/attachments/test.html')
39
+ @file_no_ext = File.open(FIXTURE_PATH + '/attachments/README')
40
+ @attachment_name = 'my_attachment'
41
+ @content_type = 'media/mp3'
42
+ end
43
+
44
+ it "should create an attachment from file with an extension" do
45
+ @obj.create_attachment(:file => @file_ext, :name => @attachment_name)
46
+ @obj.save.should == true
47
+ reloaded_obj = Basic.get(@obj.id)
48
+ reloaded_obj['_attachments'][@attachment_name].should_not be_nil
49
+ end
50
+
51
+ it "should create an attachment from file without an extension" do
52
+ @obj.create_attachment(:file => @file_no_ext, :name => @attachment_name)
53
+ @obj.save.should == true
54
+ reloaded_obj = Basic.get(@obj.id)
55
+ reloaded_obj['_attachments'][@attachment_name].should_not be_nil
56
+ end
57
+
58
+ it 'should raise ArgumentError if :file is missing' do
59
+ lambda{ @obj.create_attachment(:name => @attachment_name) }.should raise_error
60
+ end
61
+
62
+ it 'should raise ArgumentError if :name is missing' do
63
+ lambda{ @obj.create_attachment(:file => @file_ext) }.should raise_error
64
+ end
65
+
66
+ it 'should set the content-type if passed' do
67
+ @obj.create_attachment(:file => @file_ext, :name => @attachment_name, :content_type => @content_type)
68
+ @obj['_attachments'][@attachment_name]['content-type'].should == @content_type
69
+ end
70
+ end
71
+
72
+ describe 'reading, updating, and deleting an attachment' do
73
+ before(:each) do
74
+ @obj = Basic.new
75
+ @file = File.open(FIXTURE_PATH + '/attachments/test.html')
76
+ @attachment_name = 'my_attachment'
77
+ @obj.create_attachment(:file => @file, :name => @attachment_name)
78
+ @obj.save.should == true
79
+ @file.rewind
80
+ @content_type = 'media/mp3'
81
+ end
82
+
83
+ it 'should read an attachment that exists' do
84
+ @obj.read_attachment(@attachment_name).should == @file.read
85
+ end
86
+
87
+ it 'should update an attachment that exists' do
88
+ file = File.open(FIXTURE_PATH + '/attachments/README')
89
+ @file.should_not == file
90
+ @obj.update_attachment(:file => file, :name => @attachment_name)
91
+ @obj.save
92
+ reloaded_obj = Basic.get(@obj.id)
93
+ file.rewind
94
+ reloaded_obj.read_attachment(@attachment_name).should_not == @file.read
95
+ reloaded_obj.read_attachment(@attachment_name).should == file.read
96
+ end
97
+
98
+ it 'should se the content-type if passed' do
99
+ file = File.open(FIXTURE_PATH + '/attachments/README')
100
+ @file.should_not == file
101
+ @obj.update_attachment(:file => file, :name => @attachment_name, :content_type => @content_type)
102
+ @obj['_attachments'][@attachment_name]['content-type'].should == @content_type
103
+ end
104
+
105
+ it 'should delete an attachment that exists' do
106
+ @obj.delete_attachment(@attachment_name)
107
+ @obj.save
108
+ lambda{Basic.get(@obj.id).read_attachment(@attachment_name)}.should raise_error
109
+ end
110
+ end
111
+
112
+ describe "#attachment_url" do
113
+ before(:each) do
114
+ @obj = Basic.new
115
+ @file = File.open(FIXTURE_PATH + '/attachments/test.html')
116
+ @attachment_name = 'my_attachment'
117
+ @obj.create_attachment(:file => @file, :name => @attachment_name)
118
+ @obj.save.should == true
119
+ end
120
+
121
+ it 'should return nil if attachment does not exist' do
122
+ @obj.attachment_url('bogus').should be_nil
123
+ end
124
+
125
+ it 'should return the attachment URL as specified by CouchDB HttpDocumentApi' do
126
+ @obj.attachment_url(@attachment_name).should == "#{Basic.database}/#{@obj.id}/#{@attachment_name}"
127
+ end
128
+ end
129
+ end