pigeon_hole 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9de472e364be9cf004da471fe61066a89296c64f
4
- data.tar.gz: 5b1ca22fc9e3fda6f9910f47cbadd66774504eec
3
+ metadata.gz: 4a331a8c3a34ef063b9865ea99351e80a0fead73
4
+ data.tar.gz: 9290c4aecc7891840b9b1b9c7b455ee7d47ea450
5
5
  SHA512:
6
- metadata.gz: 6c17fa2009704cebbe3964e754718239a217a499f923362138876fdfae41c92cd1092cec7cde70283b21d211251ae545ba0063d4aebf1ae16e6a1dfc65ea88f8
7
- data.tar.gz: 305c3e90764ed742d3aaca10892681a2d1d583d6264bab133fe94e2916b140c539aa7a10a45f7931a722e30086823359cc293f1d5b50ba74be9c213b272d3bec
6
+ metadata.gz: ab39e9fca4485d6930c56c577d1e82df2a7fdbfcf84dc19e3cfda036aca9cb3e62c80bad3937096e158dac17e21d65b24e267a8766efe0642eee5f66281d9d76
7
+ data.tar.gz: 89bf8713b463eabf430011528bec96a9183f121b388f07df91a3774a23d39f6da0c27f758d28d0f77f801cfdbd0a469f4907f4b482e419bbf80beaa4b5e39528
@@ -0,0 +1,16 @@
1
+ module PigeonHole
2
+ module JSONHash
3
+ TYPE_VALUE = 'hash'.freeze
4
+
5
+ def self.serialize(hash)
6
+ {
7
+ TypedJSON::TYPE_KEY => TYPE_VALUE,
8
+ 'v' => hash.to_a
9
+ }
10
+ end
11
+
12
+ def self.deserialize(hash)
13
+ Hash[hash['v']]
14
+ end
15
+ end
16
+ end
@@ -73,6 +73,13 @@ module PigeonHole
73
73
  when Hash
74
74
  if deserializer = DESERIALIZERS[value[TYPE_KEY]]
75
75
  deserializer.deserialize(value)
76
+ elsif value[TYPE_KEY] == JSONHash::TYPE_VALUE
77
+ hash = JSONHash.deserialize(value)
78
+ hash.each do |k,v|
79
+ hash[k] = deserialize_value(v)
80
+ end
81
+
82
+ hash
76
83
  else
77
84
  value.each do |k, v|
78
85
  value[k] = deserialize_value(v)
@@ -102,7 +109,7 @@ module PigeonHole
102
109
  end
103
110
  end
104
111
 
105
- hash
112
+ JSONHash.serialize(hash)
106
113
  when Array
107
114
  value.each_with_index.map do |av, i|
108
115
  begin
data/lib/pigeon_hole.rb CHANGED
@@ -3,6 +3,7 @@ require 'json'
3
3
  require_relative 'pigeon_hole/json_date'
4
4
  require_relative 'pigeon_hole/json_symbol'
5
5
  require_relative 'pigeon_hole/json_time'
6
+ require_relative 'pigeon_hole/json_hash'
6
7
 
7
8
  require_relative 'pigeon_hole/typed_json'
8
9
 
@@ -97,6 +97,41 @@ describe "serializing nested hashes" do
97
97
  end
98
98
  end
99
99
 
100
+ describe "preserving order of hashes" do
101
+ let(:input) do
102
+ {
103
+ "zzz" => random_time,
104
+ "aaa" => :temp,
105
+ }
106
+ end
107
+
108
+ subject { PigeonHole.generate(input) }
109
+
110
+ it "serializes hash into a string" do
111
+ result = subject
112
+ expect(result).to_not be_empty
113
+ end
114
+
115
+ it "can be deserialized to a symbol" do
116
+ result = subject
117
+ hash = PigeonHole.parse(result)
118
+
119
+ expect(hash).to eq(input)
120
+ expect(hash.keys.first).to eq("zzz")
121
+ expect(hash.keys.at(1)).to eq("aaa")
122
+ end
123
+ end
124
+
125
+ describe "can deserialize standard json hashes" do
126
+ let(:json_string) { '{ "foo": 1 }' }
127
+
128
+ it "can be deserialized to a symbol" do
129
+ hash = PigeonHole.parse(json_string)
130
+
131
+ expect(hash).to eq({ 'foo' => 1})
132
+ end
133
+ end
134
+
100
135
  describe "serializing custom type" do
101
136
  CustomType = Struct.new(:name)
102
137
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pigeon_hole
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephen Binns
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-15 00:00:00.000000000 Z
11
+ date: 2016-08-24 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -18,6 +18,7 @@ extra_rdoc_files: []
18
18
  files:
19
19
  - lib/pigeon_hole.rb
20
20
  - lib/pigeon_hole/json_date.rb
21
+ - lib/pigeon_hole/json_hash.rb
21
22
  - lib/pigeon_hole/json_symbol.rb
22
23
  - lib/pigeon_hole/json_time.rb
23
24
  - lib/pigeon_hole/typed_json.rb