thirtythirty 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/thirtythirty.rb +11 -3
- data/spec/spec_helper.rb +1 -0
- data/spec/support/compressed.rb +4 -0
- data/spec/support/uncompressed.rb +3 -0
- data/spec/thirtythirty_spec.rb +39 -0
- data/thirtythirty.gemspec +1 -1
- metadata +9 -5
data/lib/thirtythirty.rb
CHANGED
@@ -25,16 +25,22 @@ module Thirtythirty
|
|
25
25
|
attr_accessor *marshal(attributes)
|
26
26
|
end
|
27
27
|
|
28
|
+
def marshal_with_compression(level = Zlib::BEST_COMPRESSION)
|
29
|
+
marshal
|
30
|
+
@marshalling_compression_level = level
|
31
|
+
end
|
32
|
+
|
28
33
|
private
|
29
34
|
|
30
35
|
module ClassMethods
|
31
36
|
|
32
|
-
attr_reader :marshalled_attributes
|
37
|
+
attr_reader :marshalled_attributes, :marshalling_compression_level
|
33
38
|
|
34
39
|
protected
|
35
40
|
|
36
41
|
def _load(dumped)
|
37
|
-
|
42
|
+
uncompressed = Zlib::Inflate.inflate(dumped) rescue dumped
|
43
|
+
data = Marshal.load(uncompressed)
|
38
44
|
obj = new
|
39
45
|
marshalled_attributes.each do |attr|
|
40
46
|
obj.send(:"#{attr}=", Marshal.load(data[attr]))
|
@@ -47,7 +53,9 @@ private
|
|
47
53
|
module InstanceMethods
|
48
54
|
|
49
55
|
def _dump(*args)
|
50
|
-
Marshal.dump(build_marshalled_attributes_hash {|v| Marshal.dump(v)})
|
56
|
+
dumped = Marshal.dump(build_marshalled_attributes_hash {|v| Marshal.dump(v)})
|
57
|
+
dumped = Zlib::Deflate.deflate(dumped, self.class.marshalling_compression_level) if self.class.marshalling_compression_level
|
58
|
+
dumped
|
51
59
|
end
|
52
60
|
|
53
61
|
def marshalled_attributes
|
data/spec/spec_helper.rb
CHANGED
data/spec/thirtythirty_spec.rb
CHANGED
@@ -131,6 +131,27 @@ describe Thirtythirty do
|
|
131
131
|
|
132
132
|
end
|
133
133
|
|
134
|
+
describe ".marshal_with_compression/#marshalling_compression_level" do
|
135
|
+
|
136
|
+
subject { Class.new(ThirtythirtyBase) }
|
137
|
+
|
138
|
+
it "should default to nil" do
|
139
|
+
subject.marshal # needs to be done to activate custom marshalling at all
|
140
|
+
subject.marshalling_compression_level.should be_nil
|
141
|
+
end
|
142
|
+
|
143
|
+
it "should be settable" do
|
144
|
+
subject.marshal_with_compression Zlib::BEST_SPEED
|
145
|
+
subject.marshalling_compression_level.should == Zlib::BEST_SPEED
|
146
|
+
end
|
147
|
+
|
148
|
+
it "should default to Zlib::BEST_COMPRESSION" do
|
149
|
+
subject.marshal_with_compression
|
150
|
+
subject.marshalling_compression_level.should == Zlib::BEST_COMPRESSION
|
151
|
+
end
|
152
|
+
|
153
|
+
end
|
154
|
+
|
134
155
|
describe "marshalling (#_dump/._load)" do
|
135
156
|
|
136
157
|
before do
|
@@ -208,6 +229,24 @@ describe Thirtythirty do
|
|
208
229
|
|
209
230
|
end
|
210
231
|
|
232
|
+
context "with compression" do
|
233
|
+
|
234
|
+
it "compressed version should be smaller than the uncompressed version and generally work" do
|
235
|
+
obj = Uncompressed.new
|
236
|
+
obj.persistent = "loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo text"
|
237
|
+
uncompressed = Marshal.dump(obj)
|
238
|
+
obj = Compressed.new
|
239
|
+
obj.persistent = "loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo text"
|
240
|
+
compressed = Marshal.dump(obj)
|
241
|
+
compressed.size.should < uncompressed.size
|
242
|
+
|
243
|
+
restored = Marshal.load(compressed)
|
244
|
+
restored.should be_a(Compressed)
|
245
|
+
restored.persistent.should == "loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo text"
|
246
|
+
end
|
247
|
+
|
248
|
+
end
|
249
|
+
|
211
250
|
end
|
212
251
|
|
213
252
|
end
|
data/thirtythirty.gemspec
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: thirtythirty
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 5
|
10
|
+
version: 0.0.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Benjamin Behr
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2011-02-
|
19
|
+
date: 2011-02-23 00:00:00 +01:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|
@@ -69,8 +69,10 @@ files:
|
|
69
69
|
- Rakefile
|
70
70
|
- lib/thirtythirty.rb
|
71
71
|
- spec/spec_helper.rb
|
72
|
+
- spec/support/compressed.rb
|
72
73
|
- spec/support/thirtythirty_base.rb
|
73
74
|
- spec/support/tree.rb
|
75
|
+
- spec/support/uncompressed.rb
|
74
76
|
- spec/thirtythirty_spec.rb
|
75
77
|
- thirtythirty.gemspec
|
76
78
|
has_rdoc: true
|
@@ -103,12 +105,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
103
105
|
requirements: []
|
104
106
|
|
105
107
|
rubyforge_project: thirtythirty
|
106
|
-
rubygems_version: 1.
|
108
|
+
rubygems_version: 1.5.2
|
107
109
|
signing_key:
|
108
110
|
specification_version: 3
|
109
111
|
summary: Marshalling customization
|
110
112
|
test_files:
|
111
113
|
- spec/spec_helper.rb
|
114
|
+
- spec/support/compressed.rb
|
112
115
|
- spec/support/thirtythirty_base.rb
|
113
116
|
- spec/support/tree.rb
|
117
|
+
- spec/support/uncompressed.rb
|
114
118
|
- spec/thirtythirty_spec.rb
|