toystore 0.13.0 → 0.13.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/Changelog.md +12 -0
  2. data/Guardfile +2 -2
  3. data/README.md +26 -35
  4. data/examples/attributes_abbreviation.rb +2 -2
  5. data/examples/attributes_virtual.rb +2 -2
  6. data/examples/identity_map.rb +2 -2
  7. data/examples/memcached.rb +2 -2
  8. data/examples/memory.rb +2 -2
  9. data/examples/mongo.rb +2 -2
  10. data/examples/namespacing_keys.rb +1 -1
  11. data/examples/plain_old_object.rb +2 -2
  12. data/examples/plain_old_object_on_roids.rb +13 -22
  13. data/examples/redis.rb +2 -2
  14. data/examples/riak.rb +2 -2
  15. data/lib/toy.rb +4 -0
  16. data/lib/toy/attributes.rb +10 -21
  17. data/lib/toy/cloneable.rb +1 -3
  18. data/lib/toy/dirty.rb +0 -6
  19. data/lib/toy/equality.rb +3 -14
  20. data/lib/toy/extensions/symbol.rb +17 -0
  21. data/lib/toy/extensions/uuid.rb +6 -2
  22. data/lib/toy/identity.rb +39 -2
  23. data/lib/toy/inspect.rb +3 -4
  24. data/lib/toy/object.rb +1 -5
  25. data/lib/toy/persistence.rb +34 -5
  26. data/lib/toy/querying.rb +9 -9
  27. data/lib/toy/reloadable.rb +3 -3
  28. data/lib/toy/store.rb +1 -0
  29. data/lib/toy/types/json.rb +20 -0
  30. data/lib/toy/version.rb +1 -1
  31. data/spec/helper.rb +1 -0
  32. data/spec/toy/attributes_spec.rb +21 -59
  33. data/spec/toy/cloneable_spec.rb +1 -8
  34. data/spec/toy/equality_spec.rb +18 -19
  35. data/spec/toy/extensions/symbol_spec.rb +26 -0
  36. data/spec/toy/extensions/uuid_spec.rb +31 -11
  37. data/spec/toy/identity/uuid_key_factory_spec.rb +4 -4
  38. data/spec/toy/identity_spec.rb +111 -5
  39. data/spec/toy/inheritance_spec.rb +4 -4
  40. data/spec/toy/inspect_spec.rb +3 -3
  41. data/spec/toy/object_spec.rb +0 -40
  42. data/spec/toy/persistence_spec.rb +99 -1
  43. data/spec/toy/reloadable_spec.rb +9 -4
  44. data/spec/toy/serialization_spec.rb +16 -21
  45. data/spec/toy/types/json_spec.rb +37 -0
  46. data/spec/toy/validations_spec.rb +13 -0
  47. metadata +10 -4
@@ -10,10 +10,15 @@ describe Toy::Reloadable do
10
10
  end
11
11
  let(:user) { @user }
12
12
 
13
- it "reloads id from database" do
14
- id = user.id
13
+ it "uses persisted id to read from adapter" do
14
+ User.class_eval do
15
+ def persisted_id
16
+ 'foo'
17
+ end
18
+ end
19
+ User.adapter.should_receive(:read).with('foo').and_return({})
15
20
  user.reload
16
- user.id.should == id
21
+ user.id.should == 'foo'
17
22
  end
18
23
 
19
24
  it "reloads record from the database" do
@@ -78,4 +83,4 @@ describe Toy::Reloadable do
78
83
  user.reload.admin.should be_true
79
84
  end
80
85
  end
81
- end
86
+ end
@@ -13,7 +13,6 @@ describe Toy::Serialization do
13
13
  MultiJson.load(doc.to_json).should == {
14
14
  'user' => {
15
15
  'name' => 'John',
16
- 'id' => doc.id,
17
16
  'age' => 28
18
17
  }
19
18
  }
@@ -24,7 +23,6 @@ describe Toy::Serialization do
24
23
  Hash.from_xml(doc.to_xml).should == {
25
24
  'user' => {
26
25
  'name' => 'John',
27
- 'id' => doc.id,
28
26
  'age' => 28
29
27
  }
30
28
  }
@@ -41,27 +39,27 @@ describe Toy::Serialization do
41
39
  end
42
40
 
43
41
  it "allows using :only" do
44
- user = User.new
45
- json = user.to_json(:only => :id)
46
- MultiJson.load(json).should == {'user' => {'id' => user.id}}
42
+ user = User.new(:name => 'John', :age => 30)
43
+ json = user.to_json(:only => :name)
44
+ MultiJson.load(json).should == {'user' => {'name' => user.name}}
47
45
  end
48
46
 
49
47
  it "allows using :only with strings" do
50
- user = User.new
51
- json = user.to_json(:only => 'id')
52
- MultiJson.load(json).should == {'user' => {'id' => user.id}}
48
+ user = User.new(:name => 'John', :age => 30)
49
+ json = user.to_json(:only => 'name')
50
+ MultiJson.load(json).should == {'user' => {'name' => user.name}}
53
51
  end
54
52
 
55
53
  it "allows using :except" do
56
- user = User.new
57
- json = user.to_json(:except => :id)
58
- MultiJson.load(json)['user'].should_not have_key('id')
54
+ user = User.new(:name => 'John', :age => 30)
55
+ json = user.to_json(:except => :name)
56
+ MultiJson.load(json)['user'].should_not have_key('name')
59
57
  end
60
58
 
61
59
  it "allows using :except with strings" do
62
- user = User.new
63
- json = user.to_json(:except => 'id')
64
- MultiJson.load(json)['user'].should_not have_key('id')
60
+ user = User.new(:name => 'John', :age => 30)
61
+ json = user.to_json(:except => 'name')
62
+ MultiJson.load(json)['user'].should_not have_key('name')
65
63
  end
66
64
 
67
65
  describe "serializing specific attributes" do
@@ -73,7 +71,7 @@ describe Toy::Serialization do
73
71
 
74
72
  it "should default to all attributes" do
75
73
  move = Move.new(:index => 0, :points => 15, :words => ['QI', 'XI'])
76
- move.serializable_attributes.should == [:id, :index, :points, :words]
74
+ move.serializable_attributes.should == [:index, :points, :words]
77
75
  end
78
76
 
79
77
  it "should be set per model" do
@@ -85,7 +83,7 @@ describe Toy::Serialization do
85
83
  end
86
84
 
87
85
  move = Move.new(:index => 0, :points => 15, :words => ['QI', 'XI'])
88
- move.serializable_attributes.should == [:id, :points, :words]
86
+ move.serializable_attributes.should == [:points, :words]
89
87
  end
90
88
 
91
89
  it "should only serialize specified attributes" do
@@ -99,7 +97,6 @@ describe Toy::Serialization do
99
97
  move = Move.new(:index => 0, :points => 15, :words => ['QI', 'XI'])
100
98
  MultiJson.load(move.to_json).should == {
101
99
  'move' => {
102
- 'id' => move.id,
103
100
  'points' => 15,
104
101
  'words' => ["QI", "XI"]
105
102
  }
@@ -121,7 +118,6 @@ describe Toy::Serialization do
121
118
  move = Move.new(:index => 0, :points => 15, :words => ['QI', 'XI'])
122
119
  MultiJson.load(move.to_json).should == {
123
120
  'move' => {
124
- 'id' => move.id,
125
121
  'index' => 0,
126
122
  'points' => 15,
127
123
  'words' => ["QI", "XI"],
@@ -137,12 +133,11 @@ describe Toy::Serialization do
137
133
  Move.class_eval { attr_accessor :creator }
138
134
  end
139
135
 
140
- let(:move) { Move.new(:creator => User.new) }
136
+ let(:move) { Move.new(:creator => User.new(:name => 'John')) }
141
137
 
142
138
  it "returns serializable hash of object" do
143
139
  move.serializable_hash(:methods => [:creator]).should == {
144
- 'id' => move.id,
145
- 'creator' => {'id' => move.creator.id}
140
+ 'creator' => {'name' => move.creator.name}
146
141
  }
147
142
  end
148
143
  end
@@ -0,0 +1,37 @@
1
+ require 'helper'
2
+
3
+ describe Toy::Types::JSON do
4
+ describe ".to_store" do
5
+ it "encodes as json" do
6
+ value = {'foo' => 'bar'}
7
+ expected = ActiveSupport::JSON.encode(value)
8
+ described_class.to_store(value).should eq(expected)
9
+ end
10
+
11
+ it "returns nil if nil" do
12
+ described_class.to_store(nil).should be_nil
13
+ end
14
+ end
15
+
16
+ describe ".from_store" do
17
+ it "decodes strings as json" do
18
+ value = {'foo' => 'bar'}
19
+ encoded = ActiveSupport::JSON.encode(value)
20
+ described_class.from_store(encoded).should eq(value)
21
+ end
22
+
23
+ it "leaves other values alone" do
24
+ [
25
+ {'foo' => 'bar'},
26
+ ['foo', 'bar'],
27
+ 23,
28
+ ].each do |value|
29
+ described_class.from_store(value).should eq(value)
30
+ end
31
+ end
32
+
33
+ it "returns nil if nil" do
34
+ described_class.from_store(nil).should be_nil
35
+ end
36
+ end
37
+ end
@@ -141,4 +141,17 @@ describe Toy::Validations do
141
141
  end
142
142
  end
143
143
  end
144
+
145
+ context "validating presence of uuid attribute" do
146
+ before do
147
+ User.attribute :app_id, SimpleUUID::UUID
148
+ User.validates_presence_of :app_id
149
+ end
150
+
151
+ it "is invalid if nil" do
152
+ user = User.new(app_id: nil)
153
+ user.should_not be_valid
154
+ user.errors[:app_id].should include("can't be blank")
155
+ end
156
+ end
144
157
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: toystore
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.0
4
+ version: 0.13.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-11-19 00:00:00.000000000 Z
13
+ date: 2013-01-26 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: adapter
@@ -130,6 +130,7 @@ files:
130
130
  - lib/toy/extensions/object.rb
131
131
  - lib/toy/extensions/set.rb
132
132
  - lib/toy/extensions/string.rb
133
+ - lib/toy/extensions/symbol.rb
133
134
  - lib/toy/extensions/time.rb
134
135
  - lib/toy/extensions/uuid.rb
135
136
  - lib/toy/identity.rb
@@ -155,6 +156,7 @@ files:
155
156
  - lib/toy/serialization.rb
156
157
  - lib/toy/store.rb
157
158
  - lib/toy/timestamps.rb
159
+ - lib/toy/types/json.rb
158
160
  - lib/toy/validations.rb
159
161
  - lib/toy/version.rb
160
162
  - lib/toystore.rb
@@ -186,6 +188,7 @@ files:
186
188
  - spec/toy/extensions/nil_class_spec.rb
187
189
  - spec/toy/extensions/set_spec.rb
188
190
  - spec/toy/extensions/string_spec.rb
191
+ - spec/toy/extensions/symbol_spec.rb
189
192
  - spec/toy/extensions/time_spec.rb
190
193
  - spec/toy/extensions/uuid_spec.rb
191
194
  - spec/toy/identity/abstract_key_factory_spec.rb
@@ -209,6 +212,7 @@ files:
209
212
  - spec/toy/serialization_spec.rb
210
213
  - spec/toy/store_spec.rb
211
214
  - spec/toy/timestamps_spec.rb
215
+ - spec/toy/types/json_spec.rb
212
216
  - spec/toy/validations_spec.rb
213
217
  - spec/toy_spec.rb
214
218
  - toystore.gemspec
@@ -226,7 +230,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
226
230
  version: '0'
227
231
  segments:
228
232
  - 0
229
- hash: -1758812902377714196
233
+ hash: 3156935686083657765
230
234
  required_rubygems_version: !ruby/object:Gem::Requirement
231
235
  none: false
232
236
  requirements:
@@ -235,7 +239,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
235
239
  version: '0'
236
240
  segments:
237
241
  - 0
238
- hash: -1758812902377714196
242
+ hash: 3156935686083657765
239
243
  requirements: []
240
244
  rubyforge_project:
241
245
  rubygems_version: 1.8.23
@@ -269,6 +273,7 @@ test_files:
269
273
  - spec/toy/extensions/nil_class_spec.rb
270
274
  - spec/toy/extensions/set_spec.rb
271
275
  - spec/toy/extensions/string_spec.rb
276
+ - spec/toy/extensions/symbol_spec.rb
272
277
  - spec/toy/extensions/time_spec.rb
273
278
  - spec/toy/extensions/uuid_spec.rb
274
279
  - spec/toy/identity/abstract_key_factory_spec.rb
@@ -292,5 +297,6 @@ test_files:
292
297
  - spec/toy/serialization_spec.rb
293
298
  - spec/toy/store_spec.rb
294
299
  - spec/toy/timestamps_spec.rb
300
+ - spec/toy/types/json_spec.rb
295
301
  - spec/toy/validations_spec.rb
296
302
  - spec/toy_spec.rb