satt 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/satt.rb +43 -6
- metadata +2 -2
data/lib/satt.rb
CHANGED
@@ -1,4 +1,11 @@
|
|
1
|
-
|
1
|
+
unless defined?(MessagePack)
|
2
|
+
require "msgpack"
|
3
|
+
end
|
4
|
+
|
5
|
+
unless defined?(BigDecimal)
|
6
|
+
class BigDecimal
|
7
|
+
end
|
8
|
+
end
|
2
9
|
|
3
10
|
class Satt
|
4
11
|
class InvalidArgument < RuntimeError; end
|
@@ -16,7 +23,7 @@ class Satt
|
|
16
23
|
class Primitive
|
17
24
|
NOT_DUMPABLE = [ Binding, IO, Proc, Class ].freeze
|
18
25
|
DONT_SERIALIZE = [ NilClass, Fixnum, Float, TrueClass, FalseClass ].freeze
|
19
|
-
OTHER_PRIMITIVES = [ Symbol, String, Array, Hash, Bignum ].freeze
|
26
|
+
OTHER_PRIMITIVES = [ Symbol, String, Array, Hash, Bignum, BigDecimal ].freeze
|
20
27
|
|
21
28
|
class Dumper
|
22
29
|
def initialize()
|
@@ -29,10 +36,18 @@ class Satt
|
|
29
36
|
raise InvalidArgument, "objects of type #{cl.to_s} are not dumpable" if obj.is_a?(cl)
|
30
37
|
end
|
31
38
|
|
39
|
+
if obj.respond_to?(:marshal_dump)
|
40
|
+
return [ class_identifier(obj), dump(obj.marshal_dump) ]
|
41
|
+
end
|
42
|
+
|
43
|
+
if obj.respond_to?(:_dump)
|
44
|
+
return [ class_identifier(obj), obj._dump ]
|
45
|
+
end
|
46
|
+
|
32
47
|
case obj
|
33
48
|
when *DONT_SERIALIZE
|
34
49
|
obj
|
35
|
-
when Symbol, Bignum
|
50
|
+
when Symbol, Bignum, BigDecimal
|
36
51
|
[ class_identifier(obj), dump_value(obj) ]
|
37
52
|
else
|
38
53
|
id, cached = local_id(obj)
|
@@ -50,7 +65,7 @@ class Satt
|
|
50
65
|
obj
|
51
66
|
when Symbol
|
52
67
|
obj.to_s
|
53
|
-
when Bignum
|
68
|
+
when Bignum, BigDecimal
|
54
69
|
obj.to_s(16)
|
55
70
|
when Array
|
56
71
|
obj.map{ |e| dump(e) }
|
@@ -94,6 +109,16 @@ class Satt
|
|
94
109
|
raise InvalidArgument, priv.inspect if priv.class != Array or priv.empty?
|
95
110
|
objclass = get_class(priv.first)
|
96
111
|
|
112
|
+
if objclass.respond_to?(:marshal_load)
|
113
|
+
raise InvalidArgument unless priv.length == 2
|
114
|
+
return objclass.marshal_load(load(priv.last))
|
115
|
+
end
|
116
|
+
|
117
|
+
if objclass.respond_to?(:_load)
|
118
|
+
raise InvalidArgument, priv.inspect unless priv.length == 2
|
119
|
+
return objclass._load(priv.last)
|
120
|
+
end
|
121
|
+
|
97
122
|
if objclass == Symbol
|
98
123
|
raise InvalidArgument unless priv.length == 2 and priv.last.class == String
|
99
124
|
return priv.last.to_sym
|
@@ -104,6 +129,11 @@ class Satt
|
|
104
129
|
return priv.last.to_i(16)
|
105
130
|
end
|
106
131
|
|
132
|
+
if objclass == BigDecimal
|
133
|
+
raise InvalidArgument unless priv.length == 2 and priv.last.class == String
|
134
|
+
return BigDecimal.new(priv.last)
|
135
|
+
end
|
136
|
+
|
107
137
|
if [Array, Hash].include?(objclass) and priv.length == 3
|
108
138
|
raise InvalidArgument unless priv.last.class == objclass
|
109
139
|
end
|
@@ -159,8 +189,15 @@ class Satt
|
|
159
189
|
|
160
190
|
def get_class(id)
|
161
191
|
return OTHER_PRIMITIVES[id] if id.class == Fixnum
|
162
|
-
|
163
|
-
|
192
|
+
path = id.split("::")
|
193
|
+
objclass = Object
|
194
|
+
while tmp = path.shift
|
195
|
+
unless objclass.constants.include?(tmp.to_sym) and
|
196
|
+
objclass = objclass.const_get(tmp) and
|
197
|
+
(objclass.class == Class or (path != [] and objclass.class == Module)) then
|
198
|
+
raise InvalidArgument, "unknown class #{id.to_s}"
|
199
|
+
end
|
200
|
+
objclass
|
164
201
|
end
|
165
202
|
objclass
|
166
203
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: satt
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-05-
|
12
|
+
date: 2013-05-29 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: msgpack
|