crnixon-mongomapper 0.2.0 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. data/.gitignore +1 -0
  2. data/History +48 -0
  3. data/README.rdoc +5 -3
  4. data/Rakefile +6 -4
  5. data/VERSION +1 -1
  6. data/bin/mmconsole +56 -0
  7. data/lib/mongomapper.rb +29 -18
  8. data/lib/mongomapper/associations.rb +53 -38
  9. data/lib/mongomapper/associations/base.rb +53 -20
  10. data/lib/mongomapper/associations/belongs_to_polymorphic_proxy.rb +34 -0
  11. data/lib/mongomapper/associations/belongs_to_proxy.rb +10 -14
  12. data/lib/mongomapper/associations/many_documents_as_proxy.rb +27 -0
  13. data/lib/mongomapper/associations/many_documents_proxy.rb +103 -0
  14. data/lib/mongomapper/associations/many_embedded_polymorphic_proxy.rb +33 -0
  15. data/lib/mongomapper/associations/{has_many_embedded_proxy.rb → many_embedded_proxy.rb} +6 -8
  16. data/lib/mongomapper/associations/many_polymorphic_proxy.rb +11 -0
  17. data/lib/mongomapper/associations/{array_proxy.rb → many_proxy.rb} +1 -1
  18. data/lib/mongomapper/associations/proxy.rb +24 -21
  19. data/lib/mongomapper/callbacks.rb +1 -1
  20. data/lib/mongomapper/document.rb +160 -74
  21. data/lib/mongomapper/dynamic_finder.rb +38 -0
  22. data/lib/mongomapper/embedded_document.rb +154 -105
  23. data/lib/mongomapper/finder_options.rb +11 -7
  24. data/lib/mongomapper/key.rb +15 -21
  25. data/lib/mongomapper/pagination.rb +52 -0
  26. data/lib/mongomapper/rails_compatibility/document.rb +15 -0
  27. data/lib/mongomapper/rails_compatibility/embedded_document.rb +25 -0
  28. data/lib/mongomapper/serialization.rb +1 -1
  29. data/lib/mongomapper/serializers/json_serializer.rb +15 -0
  30. data/lib/mongomapper/support.rb +30 -0
  31. data/mongomapper.gemspec +87 -46
  32. data/test/NOTE_ON_TESTING +1 -0
  33. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +53 -0
  34. data/test/functional/associations/test_belongs_to_proxy.rb +45 -0
  35. data/test/functional/associations/test_many_documents_as_proxy.rb +253 -0
  36. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +131 -0
  37. data/test/functional/associations/test_many_embedded_proxy.rb +106 -0
  38. data/test/functional/associations/test_many_polymorphic_proxy.rb +261 -0
  39. data/test/functional/associations/test_many_proxy.rb +295 -0
  40. data/test/functional/test_associations.rb +47 -0
  41. data/test/{test_callbacks.rb → functional/test_callbacks.rb} +2 -1
  42. data/test/functional/test_document.rb +952 -0
  43. data/test/functional/test_pagination.rb +81 -0
  44. data/test/functional/test_rails_compatibility.rb +30 -0
  45. data/test/functional/test_validations.rb +172 -0
  46. data/test/models.rb +169 -0
  47. data/test/test_helper.rb +7 -2
  48. data/test/unit/serializers/test_json_serializer.rb +189 -0
  49. data/test/unit/test_association_base.rb +144 -0
  50. data/test/unit/test_document.rb +123 -0
  51. data/test/unit/test_embedded_document.rb +526 -0
  52. data/test/{test_finder_options.rb → unit/test_finder_options.rb} +36 -1
  53. data/test/{test_key.rb → unit/test_key.rb} +59 -12
  54. data/test/{test_mongomapper.rb → unit/test_mongomapper.rb} +0 -0
  55. data/test/{test_observing.rb → unit/test_observing.rb} +0 -0
  56. data/test/unit/test_pagination.rb +113 -0
  57. data/test/unit/test_rails_compatibility.rb +34 -0
  58. data/test/{test_serializations.rb → unit/test_serializations.rb} +0 -2
  59. data/test/{test_validations.rb → unit/test_validations.rb} +0 -134
  60. metadata +81 -43
  61. data/lib/mongomapper/associations/has_many_proxy.rb +0 -28
  62. data/lib/mongomapper/associations/polymorphic_belongs_to_proxy.rb +0 -31
  63. data/lib/mongomapper/rails_compatibility.rb +0 -23
  64. data/test/serializers/test_json_serializer.rb +0 -104
  65. data/test/test_associations.rb +0 -174
  66. data/test/test_document.rb +0 -944
  67. data/test/test_embedded_document.rb +0 -253
  68. data/test/test_rails_compatibility.rb +0 -29
@@ -1,253 +0,0 @@
1
- require 'test_helper'
2
-
3
- class EmbeddedDocumentTest < Test::Unit::TestCase
4
- context "Including MongoMapper::EmbeddedDocument" do
5
- setup do
6
- @klass = Class.new do
7
- include MongoMapper::EmbeddedDocument
8
- end
9
- end
10
-
11
- should "clear out document default keys" do
12
- @klass.keys.size.should == 0
13
- end
14
- end
15
-
16
- context "An instance of an embedded document" do
17
- setup do
18
- @document = Class.new do
19
- include MongoMapper::EmbeddedDocument
20
-
21
- key :name, String
22
- key :age, Integer
23
- end
24
- end
25
-
26
- context "when initialized" do
27
- should "accept a hash that sets keys and values" do
28
- doc = @document.new(:name => 'John', :age => 23)
29
- doc.attributes.should == {'name' => 'John', 'age' => 23}
30
- end
31
-
32
- should "not throw error if initialized with nil" do
33
- doc = @document.new(nil)
34
- end
35
- end
36
-
37
- context "mass assigning keys" do
38
- should "update values for keys provided" do
39
- doc = @document.new(:name => 'foobar', :age => 10)
40
- doc.attributes = {:name => 'new value', :age => 5}
41
- doc.attributes[:name].should == 'new value'
42
- doc.attributes[:age].should == 5
43
- end
44
-
45
- should "not update values for keys that were not provided" do
46
- doc = @document.new(:name => 'foobar', :age => 10)
47
- doc.attributes = {:name => 'new value'}
48
- doc.attributes[:name].should == 'new value'
49
- doc.attributes[:age].should == 10
50
- end
51
-
52
- should "ignore keys that do not exist" do
53
- doc = @document.new(:name => 'foobar', :age => 10)
54
- doc.attributes = {:name => 'new value', :foobar => 'baz'}
55
- doc.attributes[:name].should == 'new value'
56
- doc.attributes[:foobar].should be(nil)
57
- end
58
-
59
- should "not ignore keys that have methods defined" do
60
- @document.class_eval do
61
- attr_writer :password
62
-
63
- def passwd
64
- @password
65
- end
66
- end
67
-
68
- doc = @document.new(:name => 'foobar', :password => 'secret')
69
- doc.passwd.should == 'secret'
70
- end
71
-
72
- should "typecast key values" do
73
- doc = @document.new(:name => 1234, :age => '21')
74
- doc.name.should == '1234'
75
- doc.age.should == 21
76
- end
77
- end
78
-
79
- context "requesting keys" do
80
- should "default to empty hash" do
81
- doc = @document.new
82
- doc.attributes.should == {}
83
- end
84
-
85
- should "return all keys that aren't nil" do
86
- doc = @document.new(:name => 'string', :age => nil)
87
- doc.attributes.should == {'name' => 'string'}
88
- end
89
- end
90
-
91
- context "key shorcuts" do
92
- should "be able to read key with []" do
93
- doc = @document.new(:name => 'string')
94
- doc[:name].should == 'string'
95
- end
96
-
97
- should "be able to write key value with []=" do
98
- doc = @document.new
99
- doc[:name] = 'string'
100
- doc[:name].should == 'string'
101
- end
102
- end
103
-
104
- context "indifferent access" do
105
- should "be enabled for keys" do
106
- doc = @document.new(:name => 'string')
107
- doc.attributes[:name].should == 'string'
108
- doc.attributes['name'].should == 'string'
109
- end
110
- end
111
-
112
- context "reading an attribute" do
113
- should "work for defined keys" do
114
- doc = @document.new(:name => 'string')
115
- doc.name.should == 'string'
116
- end
117
-
118
- should "raise no method error for undefined keys" do
119
- doc = @document.new
120
- lambda { doc.fart }.should raise_error(NoMethodError)
121
- end
122
-
123
- should "know if reader defined" do
124
- doc = @document.new
125
- doc.reader?('name').should be(true)
126
- doc.reader?(:name).should be(true)
127
- doc.reader?('age').should be(true)
128
- doc.reader?(:age).should be(true)
129
- doc.reader?('foobar').should be(false)
130
- doc.reader?(:foobar).should be(false)
131
- end
132
-
133
- should "be accessible for use in the model" do
134
- @document.class_eval do
135
- def name_and_age
136
- "#{read_attribute(:name)} (#{read_attribute(:age)})"
137
- end
138
- end
139
-
140
- doc = @document.new(:name => 'John', :age => 27)
141
- doc.name_and_age.should == 'John (27)'
142
- end
143
- end
144
-
145
- context "reading an attribute before typcasting" do
146
- should "work for defined keys" do
147
- doc = @document.new(:name => 12)
148
- doc.name_before_typecast.should == 12
149
- end
150
-
151
- should "raise no method error for undefined keys" do
152
- doc = @document.new
153
- lambda { doc.foo_before_typecast }.should raise_error(NoMethodError)
154
- end
155
-
156
- should "be accessible for use in a document" do
157
- @document.class_eval do
158
- def untypcasted_name
159
- read_attribute_before_typecast(:name)
160
- end
161
- end
162
-
163
- doc = @document.new(:name => 12)
164
- doc.name.should == '12'
165
- doc.untypcasted_name.should == 12
166
- end
167
- end
168
-
169
- context "writing an attribute" do
170
- should "work for defined keys" do
171
- doc = @document.new
172
- doc.name = 'John'
173
- doc.name.should == 'John'
174
- end
175
-
176
- should "raise no method error for undefined keys" do
177
- doc = @document.new
178
- lambda { doc.fart = 'poof!' }.should raise_error(NoMethodError)
179
- end
180
-
181
- should "typecast value" do
182
- doc = @document.new
183
- doc.name = 1234
184
- doc.name.should == '1234'
185
- doc.age = '21'
186
- doc.age.should == 21
187
- end
188
-
189
- should "know if writer defined" do
190
- doc = @document.new
191
- doc.writer?('name').should be(true)
192
- doc.writer?('name=').should be(true)
193
- doc.writer?(:name).should be(true)
194
- doc.writer?('age').should be(true)
195
- doc.writer?('age=').should be(true)
196
- doc.writer?(:age).should be(true)
197
- doc.writer?('foobar').should be(false)
198
- doc.writer?('foobar=').should be(false)
199
- doc.writer?(:foobar).should be(false)
200
- end
201
-
202
- should "be accessible for use in the model" do
203
- @document.class_eval do
204
- def name_and_age=(new_value)
205
- new_value.match(/([^\(\s]+) \((.*)\)/)
206
- write_attribute :name, $1
207
- write_attribute :age, $2
208
- end
209
- end
210
-
211
- doc = @document.new
212
- doc.name_and_age = 'Frank (62)'
213
- doc.name.should == 'Frank'
214
- doc.age.should == 62
215
- end
216
- end # writing an attribute
217
-
218
- context "respond_to?" do
219
- setup do
220
- @doc = @document.new
221
- end
222
-
223
- should "work for readers" do
224
- @doc.respond_to?(:name).should be_true
225
- @doc.respond_to?('name').should be_true
226
- end
227
-
228
- should "work for writers" do
229
- @doc.respond_to?(:name=).should be_true
230
- @doc.respond_to?('name=').should be_true
231
- end
232
-
233
- should "work for readers before typecast" do
234
- @doc.respond_to?(:name_before_typecast).should be_true
235
- @doc.respond_to?('name_before_typecast').should be_true
236
- end
237
- end
238
-
239
- context "equality" do
240
- should "be true if all keys and values are equal" do
241
- doc1 = @document.new(:name => 'John', :age => 27)
242
- doc2 = @document.new(:name => 'John', :age => 27)
243
- doc1.should == doc2
244
- end
245
-
246
- should "be false if not all the keys and values are equal" do
247
- doc1 = @document.new(:name => 'Steve', :age => 27)
248
- doc2 = @document.new(:name => 'John', :age => 27)
249
- doc1.should_not == doc2
250
- end
251
- end
252
- end # instance of a embedded document
253
- end
@@ -1,29 +0,0 @@
1
- require 'test_helper'
2
-
3
- class TestRailsCompatibility < Test::Unit::TestCase
4
- def setup
5
- @document = Class.new do
6
- include MongoMapper::Document
7
- end
8
- end
9
-
10
- should "have to_param that returns id" do
11
- instance = @document.create('_id' => '1234')
12
- instance.to_param.should == '1234'
13
- end
14
-
15
- should "alias new to new_record?" do
16
- instance = @document.new
17
- instance.new_record?.should == instance.new?
18
- end
19
-
20
- should "alias many to has_many" do
21
- @document.should respond_to(:has_many)
22
- @document.method(:has_many).should == @document.method(:many)
23
- end
24
-
25
- should "have column names" do
26
- @document.key :fname, String
27
- @document.column_names.sort.should == ['_id', 'created_at', 'fname', 'updated_at']
28
- end
29
- end