Skip to content

Commit 92599c8

Browse files
committed
Dump&Load API extended
1 parent 269bc7b commit 92599c8

File tree

2 files changed

+65
-11
lines changed

2 files changed

+65
-11
lines changed

lib/protobuf_nested_struct.rb

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@
77

88
module ProtobufNestedStruct
99
class Value
10+
def self.dump(obj)
11+
v = new
12+
v.from_ruby(obj)
13+
encode(v)
14+
end
15+
16+
def self.load(string)
17+
v = decode(string)
18+
v.to_ruby
19+
end
20+
1021
def from_ruby(obj)
1122
case obj
1223
when nil
@@ -60,6 +71,17 @@ def to_ruby
6071
end
6172

6273
class HashMapStringValue
74+
def self.dump(obj)
75+
v = new
76+
v.from_ruby(obj)
77+
encode(v)
78+
end
79+
80+
def self.load(string)
81+
v = decode(string)
82+
v.to_ruby
83+
end
84+
6385
def from_ruby(obj)
6486
Hash === obj or raise ArgumentError
6587
obj.each do |key, value|
@@ -76,6 +98,17 @@ def to_ruby
7698
end
7799

78100
class ListValue
101+
def self.dump(obj)
102+
v = new
103+
v.from_ruby(obj)
104+
encode(v)
105+
end
106+
107+
def self.load(string)
108+
v = decode(string)
109+
v.to_ruby
110+
end
111+
79112
def from_ruby(obj)
80113
Array === obj or raise ArgumentError
81114
values.clear
@@ -90,13 +123,10 @@ def to_ruby
90123
end
91124

92125
def self.dump(obj)
93-
v = Value.new
94-
v.from_ruby(obj)
95-
Value.encode(v)
126+
Value.dump(obj)
96127
end
97128

98129
def self.load(string)
99-
v = Value.decode(string)
100-
v.to_ruby
130+
Value.load(string)
101131
end
102132
end

spec/protobuf_nested_struct_spec.rb

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,16 +262,40 @@ module ProtobufNestedStruct
262262

263263
specify ".load & .dump" do
264264
obj = {"asd" => "def"}
265-
expect(dump_and_load(obj)).to eql(obj)
265+
expect(dump_and_load(obj, ProtobufNestedStruct)).to eql(obj)
266266

267267
obj = nil
268-
expect(dump_and_load(obj)).to eql(obj)
268+
expect(dump_and_load(obj, ProtobufNestedStruct)).to eql(obj)
269269

270270
obj = [1,2,"5", 6.0]
271-
expect(dump_and_load(obj)).to eql(obj)
271+
expect(dump_and_load(obj, ProtobufNestedStruct)).to eql(obj)
272272

273273
obj = true
274-
expect(dump_and_load(obj)).to eql(obj)
274+
expect(dump_and_load(obj, ProtobufNestedStruct)).to eql(obj)
275+
end
276+
277+
specify "Value.load & Value.dump" do
278+
obj = {"asd" => "def"}
279+
expect(dump_and_load(obj, Value)).to eql(obj)
280+
281+
obj = nil
282+
expect(dump_and_load(obj, Value)).to eql(obj)
283+
284+
obj = [1,2,"5", 6.0]
285+
expect(dump_and_load(obj, Value)).to eql(obj)
286+
287+
obj = true
288+
expect(dump_and_load(obj, Value)).to eql(obj)
289+
end
290+
291+
specify "HashMapStringValue.load & HashMapStringValue.dump" do
292+
obj = {"asd" => "def"}
293+
expect(dump_and_load(obj, HashMapStringValue)).to eql(obj)
294+
end
295+
296+
specify "ListValue.load & ListValue.dump" do
297+
obj = [1,2,"5", 6.0]
298+
expect(dump_and_load(obj, ListValue)).to eql(obj)
275299
end
276300

277301
private
@@ -281,8 +305,8 @@ def clone(v)
281305
klass.decode(klass.encode(v))
282306
end
283307

284-
def dump_and_load(obj)
285-
ProtobufNestedStruct.load(ProtobufNestedStruct.dump(obj))
308+
def dump_and_load(obj, klass)
309+
klass.load(klass.dump(obj))
286310
end
287311
end
288312
end

0 commit comments

Comments
 (0)