toystore 0.9.0 → 0.10.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -1
- data/.travis.yml +9 -0
- data/Changelog.md +10 -1
- data/Gemfile +14 -13
- data/README.md +213 -2
- data/examples/plain_old_object.rb +54 -0
- data/examples/plain_old_object_on_roids.rb +160 -0
- data/lib/toy.rb +3 -1
- data/lib/toy/association_serialization.rb +50 -0
- data/lib/toy/attribute.rb +17 -2
- data/lib/toy/equality.rb +5 -1
- data/lib/toy/identity/abstract_key_factory.rb +5 -0
- data/lib/toy/identity_map.rb +1 -2
- data/lib/toy/inheritance.rb +29 -0
- data/lib/toy/inspect.rb +17 -4
- data/lib/toy/object.rb +6 -0
- data/lib/toy/querying.rb +20 -3
- data/lib/toy/reference.rb +18 -4
- data/lib/toy/reloadable.rb +2 -2
- data/lib/toy/serialization.rb +1 -40
- data/lib/toy/store.rb +2 -2
- data/lib/toy/timestamps.rb +3 -1
- data/lib/toy/version.rb +2 -2
- data/spec/helper.rb +2 -3
- data/spec/support/constants.rb +15 -15
- data/spec/toy/association_serialization_spec.rb +103 -0
- data/spec/toy/attribute_spec.rb +17 -1
- data/spec/toy/equality_spec.rb +9 -2
- data/spec/toy/extensions/array_spec.rb +2 -2
- data/spec/toy/identity/uuid_key_factory_spec.rb +35 -3
- data/spec/toy/identity_map_spec.rb +4 -0
- data/spec/toy/inheritance_spec.rb +93 -0
- data/spec/toy/inspect_spec.rb +12 -4
- data/spec/toy/object_spec.rb +47 -0
- data/spec/toy/plugins_spec.rb +4 -4
- data/spec/toy/querying_spec.rb +71 -11
- data/spec/toy/reference_spec.rb +82 -72
- data/spec/toy/serialization_spec.rb +16 -111
- data/spec/toy/store_spec.rb +14 -28
- metadata +23 -13
- data/Gemfile.lock +0 -71
data/spec/toy/reference_spec.rb
CHANGED
@@ -226,7 +226,7 @@ describe Toy::Reference do
|
|
226
226
|
|
227
227
|
it "unmemoizes the list" do
|
228
228
|
@game.user.should == @user
|
229
|
-
@game.
|
229
|
+
@game.reset_user
|
230
230
|
User.should_receive(:get).and_return(@user)
|
231
231
|
@game.user.should == @user
|
232
232
|
end
|
@@ -238,87 +238,97 @@ describe Toy::Reference do
|
|
238
238
|
end
|
239
239
|
end
|
240
240
|
|
241
|
-
context "
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
end
|
241
|
+
context "when target is nil" do
|
242
|
+
before do
|
243
|
+
@game = Game.create
|
244
|
+
end
|
246
245
|
|
247
|
-
|
248
|
-
|
249
|
-
|
246
|
+
it "returns nil if nil" do
|
247
|
+
@game.user.inspect.should == 'nil'
|
248
|
+
end
|
250
249
|
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
end
|
250
|
+
it "returns true for nil?" do
|
251
|
+
@game.user.nil?.should be_true
|
252
|
+
end
|
255
253
|
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
end
|
254
|
+
it "does not evaluate to true if used in conditional" do
|
255
|
+
lambda { raise if @game.user }.should_not raise_error
|
256
|
+
end
|
260
257
|
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
end
|
258
|
+
it "returns true when prefaced with !" do
|
259
|
+
(!@game.user).should == true
|
260
|
+
end
|
265
261
|
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
end
|
262
|
+
it "delegates #kind_of?" do
|
263
|
+
@game.user.kind_of?(NilClass).should be_true
|
264
|
+
@game.user.kind_of?(User).should be_false
|
265
|
+
end
|
271
266
|
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
@game.user.should_not equal(@game)
|
276
|
-
end
|
267
|
+
it "delegates #instance_of?" do
|
268
|
+
@game.user.instance_of?(NilClass).should be_true
|
269
|
+
@game.user.instance_of?(User).should be_false
|
277
270
|
end
|
278
271
|
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
end
|
272
|
+
it "delegates #is_a?" do
|
273
|
+
@game.user.is_a?(NilClass).should be_true
|
274
|
+
@game.user.is_a?(User).should be_false
|
275
|
+
end
|
284
276
|
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
277
|
+
it "delegates #eql?" do
|
278
|
+
@game.user.should eql(nil)
|
279
|
+
@game.user.should_not eql(User.create)
|
280
|
+
@game.user.should_not eql(@game)
|
281
|
+
end
|
289
282
|
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
283
|
+
it "delegates #equal?" do
|
284
|
+
@game.user.should equal(nil)
|
285
|
+
@game.user.should_not equal(User.create)
|
286
|
+
@game.user.should_not equal(@game)
|
287
|
+
end
|
288
|
+
end
|
294
289
|
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
290
|
+
context "when target is not nil" do
|
291
|
+
before do
|
292
|
+
@user = User.create
|
293
|
+
@game = Game.create(:user => @user)
|
294
|
+
end
|
299
295
|
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
296
|
+
it "delegates #inspect" do
|
297
|
+
@game.user = @user
|
298
|
+
@game.user.inspect.should == %Q(#<User:#{@user.object_id} id: "#{@user.id}">)
|
299
|
+
end
|
304
300
|
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
301
|
+
it "delegates #==" do
|
302
|
+
(@game.user == @user).should be_true
|
303
|
+
(@user == @game.user).should be_true
|
304
|
+
end
|
309
305
|
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
end
|
306
|
+
it "delegates #is_a?" do
|
307
|
+
@game.user.is_a?(User).should be_true
|
308
|
+
@game.user.is_a?(Game).should be_false
|
309
|
+
end
|
315
310
|
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
311
|
+
it "delegates #kind_of?" do
|
312
|
+
@game.user.kind_of?(User).should be_true
|
313
|
+
@game.user.kind_of?(Game).should be_false
|
314
|
+
end
|
315
|
+
|
316
|
+
it "delegates #instance_of?" do
|
317
|
+
@game.user.instance_of?(User).should be_true
|
318
|
+
@game.user.instance_of?(Game).should be_false
|
319
|
+
end
|
320
|
+
|
321
|
+
it "delegates #eql?" do
|
322
|
+
@game.user.should eql(@user)
|
323
|
+
@game.user.should_not eql(User.create)
|
324
|
+
@game.user.should_not eql(@game)
|
325
|
+
end
|
326
|
+
|
327
|
+
it "delegates #equal?" do
|
328
|
+
@game.user.should equal(@user)
|
329
|
+
@user.should equal(@game.user)
|
330
|
+
@game.user.should_not equal(User.create)
|
331
|
+
@game.user.should_not equal(@game)
|
322
332
|
end
|
323
333
|
end
|
324
334
|
|
@@ -339,7 +349,7 @@ describe Toy::Reference do
|
|
339
349
|
describe "reference#create" do
|
340
350
|
before do
|
341
351
|
@game = Game.create
|
342
|
-
@user = @game.
|
352
|
+
@user = @game.create_user
|
343
353
|
end
|
344
354
|
|
345
355
|
it_should_behave_like 'reference#create'
|
@@ -349,7 +359,7 @@ describe Toy::Reference do
|
|
349
359
|
before do
|
350
360
|
User.attribute(:name, String)
|
351
361
|
@game = Game.create
|
352
|
-
@user = @game.
|
362
|
+
@user = @game.create_user(:name => 'John')
|
353
363
|
end
|
354
364
|
|
355
365
|
it_should_behave_like 'reference#create'
|
@@ -374,14 +384,14 @@ describe Toy::Reference do
|
|
374
384
|
|
375
385
|
it "does not save owner" do
|
376
386
|
@game.should_not_receive(:save)
|
377
|
-
@game.
|
387
|
+
@game.build_user
|
378
388
|
end
|
379
389
|
end
|
380
390
|
|
381
391
|
describe "reference#build" do
|
382
392
|
before do
|
383
393
|
@game = Game.create
|
384
|
-
@user = @game.
|
394
|
+
@user = @game.build_user
|
385
395
|
end
|
386
396
|
|
387
397
|
it_should_behave_like 'reference#build'
|
@@ -391,7 +401,7 @@ describe Toy::Reference do
|
|
391
401
|
before do
|
392
402
|
User.attribute(:name, String)
|
393
403
|
@game = Game.create
|
394
|
-
@user = @game.
|
404
|
+
@user = @game.build_user(:name => 'John')
|
395
405
|
end
|
396
406
|
|
397
407
|
it_should_behave_like 'reference#build'
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'helper'
|
2
2
|
|
3
3
|
describe Toy::Serialization do
|
4
|
-
|
4
|
+
uses_objects('User', 'Move')
|
5
5
|
|
6
6
|
before do
|
7
7
|
User.attribute :name, String
|
@@ -10,7 +10,7 @@ describe Toy::Serialization do
|
|
10
10
|
|
11
11
|
it "serializes to json" do
|
12
12
|
doc = User.new(:name => 'John', :age => 28)
|
13
|
-
|
13
|
+
MultiJson.load(doc.to_json).should == {
|
14
14
|
'user' => {
|
15
15
|
'name' => 'John',
|
16
16
|
'id' => doc.id,
|
@@ -28,7 +28,6 @@ describe Toy::Serialization do
|
|
28
28
|
'age' => 28
|
29
29
|
}
|
30
30
|
}
|
31
|
-
|
32
31
|
end
|
33
32
|
|
34
33
|
it "correctly serializes methods" do
|
@@ -38,124 +37,31 @@ describe Toy::Serialization do
|
|
38
37
|
end
|
39
38
|
end
|
40
39
|
json = User.new.to_json(:methods => [:foo])
|
41
|
-
|
40
|
+
MultiJson.load(json)['user']['foo'].should == {'foo' => 'bar'}
|
42
41
|
end
|
43
42
|
|
44
43
|
it "allows using :only" do
|
45
44
|
user = User.new
|
46
45
|
json = user.to_json(:only => :id)
|
47
|
-
|
46
|
+
MultiJson.load(json).should == {'user' => {'id' => user.id}}
|
48
47
|
end
|
49
48
|
|
50
49
|
it "allows using :only with strings" do
|
51
50
|
user = User.new
|
52
51
|
json = user.to_json(:only => 'id')
|
53
|
-
|
52
|
+
MultiJson.load(json).should == {'user' => {'id' => user.id}}
|
54
53
|
end
|
55
54
|
|
56
55
|
it "allows using :except" do
|
57
56
|
user = User.new
|
58
57
|
json = user.to_json(:except => :id)
|
59
|
-
|
58
|
+
MultiJson.load(json)['user'].should_not have_key('id')
|
60
59
|
end
|
61
60
|
|
62
61
|
it "allows using :except with strings" do
|
63
62
|
user = User.new
|
64
63
|
json = user.to_json(:except => 'id')
|
65
|
-
|
66
|
-
end
|
67
|
-
|
68
|
-
describe "serializing relationships" do
|
69
|
-
before do
|
70
|
-
User.list :games, :inverse_of => :user
|
71
|
-
Game.reference :user
|
72
|
-
end
|
73
|
-
|
74
|
-
it "should include references" do
|
75
|
-
user = User.create(:name => 'John', :age => 28)
|
76
|
-
game = user.games.create
|
77
|
-
|
78
|
-
ActiveSupport::JSON.decode(game.to_json(:include => [:user])).should == {
|
79
|
-
'game' => {
|
80
|
-
'id' => game.id,
|
81
|
-
'user_id' => user.id,
|
82
|
-
'user' => {
|
83
|
-
'name' => 'John',
|
84
|
-
'game_ids' => [game.id],
|
85
|
-
'id' => user.id,
|
86
|
-
'age' => 28,
|
87
|
-
}
|
88
|
-
}
|
89
|
-
}
|
90
|
-
end
|
91
|
-
|
92
|
-
it "should include lists" do
|
93
|
-
user = User.create(:name => 'John', :age => 28)
|
94
|
-
game = user.games.create
|
95
|
-
ActiveSupport::JSON.decode(user.to_json(:include => [:games])).should == {
|
96
|
-
'user' => {
|
97
|
-
'name' => 'John',
|
98
|
-
'game_ids' => [game.id],
|
99
|
-
'id' => user.id,
|
100
|
-
'age' => 28,
|
101
|
-
'games' => [{'id' => game.id, 'user_id' => user.id}],
|
102
|
-
}
|
103
|
-
}
|
104
|
-
end
|
105
|
-
|
106
|
-
it "should not cause circular reference JSON errors for references" do
|
107
|
-
user = User.create(:name => 'John', :age => 28)
|
108
|
-
game = user.games.create
|
109
|
-
|
110
|
-
ActiveSupport::JSON.decode(ActiveSupport::JSON.encode(game.user)).should == {
|
111
|
-
'user' => {
|
112
|
-
'name' => 'John',
|
113
|
-
'game_ids' => [game.id],
|
114
|
-
'id' => user.id,
|
115
|
-
'age' => 28
|
116
|
-
}
|
117
|
-
}
|
118
|
-
end
|
119
|
-
|
120
|
-
it "should not cause circular reference JSON errors for references when called indirectly" do
|
121
|
-
user = User.create(:name => 'John', :age => 28)
|
122
|
-
game = user.games.create
|
123
|
-
|
124
|
-
ActiveSupport::JSON.decode(ActiveSupport::JSON.encode([game.user])).should == [
|
125
|
-
'user' => {
|
126
|
-
'name' => 'John',
|
127
|
-
'game_ids' => [game.id],
|
128
|
-
'id' => user.id,
|
129
|
-
'age' => 28
|
130
|
-
}
|
131
|
-
]
|
132
|
-
end
|
133
|
-
|
134
|
-
it "should not cause circular reference JSON errors for lists" do
|
135
|
-
user = User.create(:name => 'John', :age => 28)
|
136
|
-
game = user.games.create
|
137
|
-
|
138
|
-
ActiveSupport::JSON.decode(ActiveSupport::JSON.encode(user.games)).should == [{
|
139
|
-
'game' => {
|
140
|
-
'id' => game.id,
|
141
|
-
'user_id' => user.id
|
142
|
-
}
|
143
|
-
}]
|
144
|
-
end
|
145
|
-
|
146
|
-
it "should not cause circular reference JSON errors for lists when called indirectly" do
|
147
|
-
user = User.create(:name => 'John', :age => 28)
|
148
|
-
game = user.games.create
|
149
|
-
|
150
|
-
ActiveSupport::JSON.decode(ActiveSupport::JSON.encode({:games => user.games})).should == {
|
151
|
-
'games' => [{
|
152
|
-
'game' => {
|
153
|
-
'id' => game.id,
|
154
|
-
'user_id' => user.id
|
155
|
-
}
|
156
|
-
}]
|
157
|
-
}
|
158
|
-
end
|
64
|
+
MultiJson.load(json)['user'].should_not have_key('id')
|
159
65
|
end
|
160
66
|
|
161
67
|
describe "serializing specific attributes" do
|
@@ -191,7 +97,7 @@ describe Toy::Serialization do
|
|
191
97
|
end
|
192
98
|
|
193
99
|
move = Move.new(:index => 0, :points => 15, :words => ['QI', 'XI'])
|
194
|
-
|
100
|
+
MultiJson.load(move.to_json).should == {
|
195
101
|
'move' => {
|
196
102
|
'id' => move.id,
|
197
103
|
'points' => 15,
|
@@ -213,7 +119,7 @@ describe Toy::Serialization do
|
|
213
119
|
end
|
214
120
|
|
215
121
|
move = Move.new(:index => 0, :points => 15, :words => ['QI', 'XI'])
|
216
|
-
|
122
|
+
MultiJson.load(move.to_json).should == {
|
217
123
|
'move' => {
|
218
124
|
'id' => move.id,
|
219
125
|
'index' => 0,
|
@@ -228,18 +134,17 @@ describe Toy::Serialization do
|
|
228
134
|
describe "#serializable_hash" do
|
229
135
|
context "with method that is another toystore object" do
|
230
136
|
before do
|
231
|
-
|
232
|
-
@game = Game.create(:creator => User.create)
|
137
|
+
Move.class_eval { attr_accessor :creator }
|
233
138
|
end
|
234
|
-
|
139
|
+
|
140
|
+
let(:move) { Move.new(:creator => User.new) }
|
235
141
|
|
236
142
|
it "returns serializable hash of object" do
|
237
|
-
|
238
|
-
'id' =>
|
239
|
-
'
|
240
|
-
'creator' => {'id' => game.creator.id}
|
143
|
+
move.serializable_hash(:methods => [:creator]).should == {
|
144
|
+
'id' => move.id,
|
145
|
+
'creator' => {'id' => move.creator.id}
|
241
146
|
}
|
242
147
|
end
|
243
148
|
end
|
244
149
|
end
|
245
|
-
end
|
150
|
+
end
|
data/spec/toy/store_spec.rb
CHANGED
@@ -3,39 +3,25 @@ require 'helper'
|
|
3
3
|
describe Toy::Store do
|
4
4
|
uses_constants('User')
|
5
5
|
|
6
|
-
describe "
|
7
|
-
it "
|
8
|
-
|
9
|
-
|
10
|
-
model_name.singular.should == 'user'
|
11
|
-
model_name.plural.should == 'users'
|
6
|
+
describe "#to_key" do
|
7
|
+
it "returns [id] if persisted" do
|
8
|
+
user = User.create
|
9
|
+
user.to_key.should == [user.id]
|
12
10
|
end
|
13
11
|
|
14
|
-
it "
|
15
|
-
|
16
|
-
user.to_model.should == user
|
12
|
+
it "returns nil if not persisted" do
|
13
|
+
User.new.to_key.should be_nil
|
17
14
|
end
|
15
|
+
end
|
18
16
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
end
|
24
|
-
|
25
|
-
it "returns nil if not persisted" do
|
26
|
-
User.new.to_key.should be_nil
|
27
|
-
end
|
17
|
+
describe "#to_param" do
|
18
|
+
it "returns key joined by - if to_key present" do
|
19
|
+
user = User.create
|
20
|
+
user.to_param.should == user.to_key.join('-')
|
28
21
|
end
|
29
22
|
|
30
|
-
|
31
|
-
|
32
|
-
user = User.create
|
33
|
-
user.to_param.should == user.to_key.join('-')
|
34
|
-
end
|
35
|
-
|
36
|
-
it "returns nil if to_key nil" do
|
37
|
-
User.new.to_param.should be_nil
|
38
|
-
end
|
23
|
+
it "returns nil if to_key nil" do
|
24
|
+
User.new.to_param.should be_nil
|
39
25
|
end
|
40
26
|
end
|
41
|
-
end
|
27
|
+
end
|