trick_serial 0.2.0

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.
@@ -0,0 +1,275 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ require 'trick_serial/serializer'
4
+ require 'ostruct'
5
+
6
+ ######################################################################
7
+
8
+ describe "TrickSerial::Serializer" do
9
+ def t
10
+ TrickSerial::Serializer::Test
11
+ end
12
+ before(:each) do
13
+ @s = TrickSerial::Serializer.new
14
+ @s.class_option_map = {
15
+ t::PhonyActiveRecord => { :proxy_class => TrickSerial::Serializer::ActiveRecordProxy, },
16
+ t::A => { :instance_vars => true },
17
+ t::B => { :instance_vars => [ "@x" ] },
18
+ ::OpenStruct => { :instance_vars => true },
19
+ }
20
+ TrickSerial::Serializer::Test::PhonyActiveRecord.find_map.clear
21
+ # Note: Structs are anonymous Classes and cannot be Marshal'ed.
22
+ @struct = Struct.new :sm, :sa, :sb
23
+ @m = TrickSerial::Serializer::Test::Model.new(123)
24
+ @m2 = TrickSerial::Serializer::Test::Model.new(456)
25
+ @m_unsaved = TrickSerial::Serializer::Test::Model.new(:unsaved)
26
+ @m_unsaved.id = nil
27
+ @h = {
28
+ :a => 1,
29
+ 'b' => 2,
30
+ :obj => Object.new,
31
+ :m => @m,
32
+ :a => [ 0, 1, @m, 3, 4, @m ],
33
+ :m_unsaved => @m_unsaved,
34
+ :s => @struct.new,
35
+ :os => OpenStruct.new,
36
+ }
37
+ @h[:s].sm = @m2
38
+ @h[:s].sa = @h
39
+ @h[:s].sb = 'b'
40
+ @h[:os].osm = @m
41
+ @h[:os].osa = @h
42
+ @h[:os].osb = 'b'
43
+ @h[:a2] = @h[:a]
44
+ @h[:h2] = @h
45
+ end
46
+
47
+ it "should handle #encode!" do
48
+ @h[:m].should == @m
49
+ @h[:a][2].should == @m
50
+
51
+ result = @s.encode!(@h)
52
+
53
+ result.object_id.should == @h.object_id
54
+ end
55
+
56
+ it "should handle #encode! of Struct" do
57
+ @h[:s].sm.should == @m2
58
+ @h[:s].sa.should == @h
59
+ @h[:s].sb = @h[:s] # self-reference
60
+
61
+ result = @s.encode!(@h)
62
+
63
+ result.object_id.should == @h.object_id
64
+ result[:s].class.should == @struct
65
+ result[:s].sm.id.should == @m2.id
66
+ result[:s].sm.class.should == TrickSerial::Serializer::ActiveRecordProxy
67
+ result[:s].sa.object_id.should == @h.object_id
68
+ result[:s].sb.object_id.should == @h[:s].sb.object_id
69
+ result[:s].sb.object_id.should == result[:s].object_id
70
+ end
71
+
72
+ it "should handle #encode! of OpenStruct" do
73
+ @h[:os].osm.should == @m
74
+ @h[:os].osa.should == @h
75
+ @h[:os].osos = @h[:os] # self-reference
76
+
77
+ result = @s.encode!(@h)
78
+
79
+ result.object_id.should == @h.object_id
80
+ result[:os].class.should == @h[:os].class
81
+ result[:os].osm.id.should == @m.id
82
+ result[:os].osm.class.should == TrickSerial::Serializer::ActiveRecordProxy
83
+ result[:os].osa.object_id.should == @h.object_id
84
+ result[:os].osb.object_id.should == @h[:os].osb.object_id
85
+ result[:os].osos.object_id.should == result[:os].object_id
86
+ end
87
+
88
+ it "should honor #enabled?" do
89
+ enabled = true
90
+ @s.enabled = lambda { | | enabled }
91
+ result = @s.encode(@h)
92
+ result.object_id.should_not == @h.object_id
93
+
94
+ enabled = false
95
+ result = @s.encode(@h)
96
+ result.object_id.should == @h.object_id
97
+ end
98
+
99
+ it "should proxy unsaved models" do
100
+ @o = @s.encode!(@h)
101
+
102
+ @o[:m].object_id.should_not == @m.object_id
103
+ @o[:m].class.should == @m.class
104
+ @o[:m].id.should == @m.id
105
+ end
106
+
107
+ it "should not proxy unsaved models" do
108
+ @o = @s.encode!(@h)
109
+
110
+ @o[:m_unsaved].object_id.should == @m_unsaved.object_id
111
+ end
112
+
113
+ it "should handle proxy swizzling through Hash#[]" do
114
+ @s.encode!(@h)
115
+ p = @h[:m]
116
+ # pp [ :p=, p ]
117
+ p.class.should == @m.class
118
+ end
119
+
120
+ it "should handle proxy swizzling through Hash#values" do
121
+ @s.encode!(@h)
122
+ p = @h.values
123
+ p.select{|m| @m.class == m.class}.size.should == 2
124
+ end
125
+
126
+ it "should handle proxy swizzling through Array#[]" do
127
+ @s.encode!(@h)
128
+ p = @h[:a][2]
129
+ p.class.should == @m.class
130
+ end
131
+
132
+ it "should handle proxy swizzling through Array#each" do
133
+ @s.encode!(@h)
134
+ p = @h[:m]
135
+ a = [ ]
136
+ @h[:a].each do | e |
137
+ a << e
138
+ end
139
+ a.should == [ 0, 1, p, 3, 4, p ]
140
+ end
141
+
142
+ it "should handle proxy swizzling through Array#map" do
143
+ @s.encode!(@h)
144
+ p = @h[:m]
145
+ a = @h[:a].each { | e | e }
146
+ a.should == [ 0, 1, p, 3, 4, p ]
147
+ end
148
+
149
+ it "should handle proxy swizzling through Array#map!" do
150
+ @s.encode!(@h)
151
+ p = @h[:m]
152
+ a = @h[:a].map! { | e | 1; e }
153
+ a.should == [ 0, 1, p, 3, 4, p ]
154
+ end
155
+
156
+ it "should handle proxy swizzling through Array#select" do
157
+ @s.encode!(@h)
158
+ p = @h[:m]
159
+ a = @h[:a].select { | e | @m.class == e.class }
160
+ a.should == [ p, p ]
161
+ end
162
+
163
+ it "should handle proxy swizzling through Array#find" do
164
+ @s.encode!(@h)
165
+ p = @h[:m]
166
+ a = @h[:a].find { | e | @m.class == e.class }
167
+ a.should == p
168
+ end
169
+
170
+ it "should lazily traverse proxies" do
171
+ fm = TrickSerial::Serializer::Test::PhonyActiveRecord.find_map
172
+
173
+ fm[@m.id].should == nil
174
+
175
+ @s.encode!(@h)
176
+
177
+ # Note: Structs are anonymous Classes and cannot be Marshal'ed.
178
+ @h[:s] = nil
179
+ @h = Marshal.load(Marshal.dump(@h))
180
+
181
+ fm[@m.id].should == nil
182
+
183
+ p = @h[:m]
184
+
185
+ fm[@m.id].should == 1
186
+
187
+ @h[:m].object_id.should == p.object_id
188
+ @h[:a][2].object_id.should == p.object_id
189
+ @h[:a][5].object_id.should == p.object_id
190
+
191
+ fm[@m.id].should == 1
192
+ fm.keys.size.should == 1
193
+ end
194
+
195
+ it "should preserve object identity" do
196
+ @s.encode!(@h)
197
+
198
+ p = @h[:m]
199
+
200
+ @h[:m].object_id.should == p.object_id
201
+ @h[:a2].object_id.should == @h[:a].object_id
202
+ @h[:a][2].object_id.should == p.object_id
203
+ @h[:a][5].object_id.should == p.object_id
204
+ end
205
+
206
+ it "should produce a serializable result" do
207
+ @s.encode!(@h)
208
+
209
+ # Note: Structs are anonymous Classes and cannot be Marshal'ed.
210
+ @h[:s] = nil
211
+ @o = Marshal.load(Marshal.dump(@h))
212
+
213
+ p = @o[:m]
214
+ p.class.should == @m.class
215
+
216
+ @o[:m].object_id.should == p.object_id
217
+ @o[:a][2].object_id.should == p.object_id
218
+ @o[:a][5].object_id.should == p.object_id
219
+ @o[:h2].object_id.should == @o.object_id
220
+ end
221
+
222
+ it "should copy core collections" do
223
+ @o = @s.encode(@h)
224
+
225
+ @o.object_id.should_not == @h.object_id
226
+ @o[:a].object_id.should_not == @h[:a].object_id
227
+ @o[:a2].object_id.should == @o[:a].object_id
228
+
229
+ @o[:m].object_id.should_not == @h[:m].object_id
230
+ @o[:h2].object_id.should == @o.object_id
231
+ end
232
+
233
+ it "should honor :instance_vars option" do
234
+ obj = t::A.new
235
+ obj.x = @m
236
+ obj.y = @m
237
+ obj = @s.encode!(obj)
238
+ e = Marshal.load(str = Marshal.dump(obj))
239
+ # $stderr.puts "marshal = #{str.inspect}"
240
+
241
+ e.class.should == t::A
242
+ e.x._proxy_class.should == TrickSerial::Serializer::ProxySwizzlingIvar
243
+ # e.instance_variable_get("@x").class.should == TrickSerial::Serializer::ProxySwizzlingIvar # BUG!?!?!
244
+ e.x.class.should == TrickSerial::Serializer::Test::Model
245
+ e.instance_variable_get("@x").class.should == TrickSerial::Serializer::Test::Model
246
+
247
+ e.y._proxy_class.should == TrickSerial::Serializer::ProxySwizzlingIvar
248
+ # e.instance_variable_get("@y").class.should == TrickSerial::Serializer::ProxySwizzlingIvar # BUG!?!?!
249
+ e.y.class.should == TrickSerial::Serializer::Test::Model
250
+ e.instance_variable_get("@y").class.should == TrickSerial::Serializer::Test::Model
251
+ e.x.object_id.should == e.y.object_id
252
+
253
+ obj = t::B.new
254
+ obj.x = @m
255
+ obj.y = @m
256
+ obj = @s.encode!(obj)
257
+ e = Marshal.load(str = Marshal.dump(obj))
258
+ # $stderr.puts "marshal = #{str.inspect}"
259
+
260
+ e.class.should == t::B
261
+ e.x._proxy_class.should == TrickSerial::Serializer::ProxySwizzlingIvar
262
+ # e.instance_variable_get("@x").class.should == TrickSerial::Serializer::ProxySwizzlingIvar # BUG!?!?!
263
+ e.x.class.should == @m.class
264
+ e.instance_variable_get("@x").class.should == TrickSerial::Serializer::Test::Model
265
+ lambda { e.y._proxy_class }.should raise_error
266
+ e.instance_variable_get("@y").class.should == TrickSerial::Serializer::Test::Model
267
+ e.y.class.should == @m2.class
268
+ e.x.id.should == @m.id
269
+ e.y.id.should == @m.id
270
+ e.x.object_id.should_not == @m.object_id
271
+ e.x.object_id.should_not == e.y.object_id
272
+ e.y.object_id.should_not == @m.object_id
273
+ end
274
+
275
+ end
metadata ADDED
@@ -0,0 +1,207 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: trick_serial
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
+ platform: ruby
12
+ authors:
13
+ - Kurt Stephens
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-07-20 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ prerelease: false
23
+ version_requirements: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 2
31
+ - 3
32
+ - 0
33
+ version: 2.3.0
34
+ name: rspec
35
+ requirement: *id001
36
+ type: :development
37
+ - !ruby/object:Gem::Dependency
38
+ prerelease: false
39
+ version_requirements: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ hash: 7
45
+ segments:
46
+ - 0
47
+ - 6
48
+ - 0
49
+ version: 0.6.0
50
+ name: yard
51
+ requirement: *id002
52
+ type: :development
53
+ - !ruby/object:Gem::Dependency
54
+ prerelease: false
55
+ version_requirements: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ hash: 19
61
+ segments:
62
+ - 1
63
+ - 1
64
+ - 0
65
+ version: 1.1.0
66
+ name: bundler
67
+ requirement: *id003
68
+ type: :development
69
+ - !ruby/object:Gem::Dependency
70
+ prerelease: false
71
+ version_requirements: &id004 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ hash: 55
77
+ segments:
78
+ - 1
79
+ - 8
80
+ - 0
81
+ version: 1.8.0
82
+ name: jeweler
83
+ requirement: *id004
84
+ type: :development
85
+ - !ruby/object:Gem::Dependency
86
+ prerelease: false
87
+ version_requirements: &id005 !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ hash: 3
93
+ segments:
94
+ - 0
95
+ version: "0"
96
+ name: rcov
97
+ requirement: *id005
98
+ type: :development
99
+ - !ruby/object:Gem::Dependency
100
+ prerelease: false
101
+ version_requirements: &id006 !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ hash: 3
107
+ segments:
108
+ - 0
109
+ version: "0"
110
+ name: ruby-debug
111
+ requirement: *id006
112
+ type: :development
113
+ - !ruby/object:Gem::Dependency
114
+ prerelease: false
115
+ version_requirements: &id007 !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ~>
119
+ - !ruby/object:Gem::Version
120
+ hash: 19
121
+ segments:
122
+ - 1
123
+ - 2
124
+ - 6
125
+ version: 1.2.6
126
+ name: rails
127
+ requirement: *id007
128
+ type: :development
129
+ - !ruby/object:Gem::Dependency
130
+ prerelease: false
131
+ version_requirements: &id008 !ruby/object:Gem::Requirement
132
+ none: false
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ hash: 11
137
+ segments:
138
+ - 1
139
+ - 7
140
+ - 0
141
+ version: 1.7.0
142
+ name: memcache-client
143
+ requirement: *id008
144
+ type: :development
145
+ description: Trick Serializers using Proxies.
146
+ email: ks.github@kurtstephens.com
147
+ executables: []
148
+
149
+ extensions: []
150
+
151
+ extra_rdoc_files:
152
+ - LICENSE.txt
153
+ - README.rdoc
154
+ files:
155
+ - .document
156
+ - .rspec
157
+ - Gemfile
158
+ - LICENSE.txt
159
+ - README.rdoc
160
+ - Rakefile
161
+ - VERSION
162
+ - cabar.yml
163
+ - lib/trick_serial.rb
164
+ - lib/trick_serial/serializer.rb
165
+ - lib/trick_serial/serializer/cgi_session.rb
166
+ - lib/trick_serial/serializer/rails.rb
167
+ - lib/trick_serial/serializer/simple.rb
168
+ - spec/spec_helper.rb
169
+ - spec/trick_serial/cgi_session_spec.rb
170
+ - spec/trick_serial/serializer_simple_spec.rb
171
+ - spec/trick_serial/serializer_spec.rb
172
+ has_rdoc: true
173
+ homepage: http://github.com/kstephens/trick_serial
174
+ licenses:
175
+ - MIT
176
+ post_install_message:
177
+ rdoc_options: []
178
+
179
+ require_paths:
180
+ - lib
181
+ required_ruby_version: !ruby/object:Gem::Requirement
182
+ none: false
183
+ requirements:
184
+ - - ">="
185
+ - !ruby/object:Gem::Version
186
+ hash: 3
187
+ segments:
188
+ - 0
189
+ version: "0"
190
+ required_rubygems_version: !ruby/object:Gem::Requirement
191
+ none: false
192
+ requirements:
193
+ - - ">="
194
+ - !ruby/object:Gem::Version
195
+ hash: 3
196
+ segments:
197
+ - 0
198
+ version: "0"
199
+ requirements: []
200
+
201
+ rubyforge_project:
202
+ rubygems_version: 1.3.7
203
+ signing_key:
204
+ specification_version: 3
205
+ summary: A Serialization Framework.
206
+ test_files: []
207
+