pigeon_hole 0.1.7 → 0.1.8

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: 123fba48e58f496fd52022df7fea6e6adede23d6
4
- data.tar.gz: 872065a790ede251a1a334fb9209288650f0a1ff
3
+ metadata.gz: c21b2387274c83dc6b6a4328065d21d31277bb41
4
+ data.tar.gz: 342edbf12838b2d3da5aa8c12f9fb393a8d49cb9
5
5
  SHA512:
6
- metadata.gz: 2cd3cf6de97c561d1d68ac1d65cb0344b66b4623ff2dbc0f0b4201d216b4fea0a77809803bdc6c385668d51effcacd98de7661d2d2aaa26ffc8b03ea42ea9e16
7
- data.tar.gz: b1fa932d5624dea8b6e529e88e3146b130bff1703bb5bdfd292955a4f570d571ab0f2b8d043a1c2b71f8060f3b9c505661c6e1de2996f55250130a2bc0afa245
6
+ metadata.gz: be6af69d4b64e20e51375e2dc40b9235d5038b2a844430bf2fa104f967593419726321c2d32fdf79af3623b1348678ee29b8a3182d032afca6836f7e6969d986
7
+ data.tar.gz: 630f090dbcc0de6aa8dccbf527370e1217be84fdd3274a4bde63537f77a1b2bb0989d239d55efb42c4497d13c344de04e82dd08dce22967df1bbe15faa6b1f90
data/lib/pigeon_hole.rb CHANGED
@@ -1,9 +1,10 @@
1
1
  require 'json'
2
2
 
3
3
  require_relative 'pigeon_hole/json_date'
4
+ require_relative 'pigeon_hole/json_hash'
5
+ require_relative 'pigeon_hole/json_set'
4
6
  require_relative 'pigeon_hole/json_symbol'
5
7
  require_relative 'pigeon_hole/json_time'
6
- require_relative 'pigeon_hole/json_hash'
7
8
 
8
9
  require_relative 'pigeon_hole/typed_json'
9
10
 
@@ -0,0 +1,16 @@
1
+ module PigeonHole
2
+ class JSONSet
3
+ TYPE_VALUE = 'set'.freeze
4
+
5
+ def self.serialize(set)
6
+ {
7
+ TypedJSON::TYPE_KEY => TYPE_VALUE,
8
+ 'v' => set.to_a
9
+ }
10
+ end
11
+
12
+ def self.deserialize(hash)
13
+ hash['v']
14
+ end
15
+ end
16
+ end
@@ -50,14 +50,14 @@ module PigeonHole
50
50
 
51
51
  DESERIALIZERS = {
52
52
  JSONDate::TYPE_VALUE => JSONDate,
53
- JSONTime::TYPE_VALUE => JSONTime,
54
53
  JSONSymbol::TYPE_VALUE => JSONSymbol,
54
+ JSONTime::TYPE_VALUE => JSONTime,
55
55
  }.freeze
56
56
 
57
57
  SERIALIZERS = {
58
58
  Date => JSONDate,
59
- Time => JSONTime,
60
59
  Symbol => JSONSymbol,
60
+ Time => JSONTime,
61
61
  }.freeze
62
62
 
63
63
  TYPE_KEY = '*'.freeze
@@ -90,6 +90,9 @@ module PigeonHole
90
90
  end
91
91
 
92
92
  hash
93
+ elsif value[TYPE_KEY] == JSONSet::TYPE_VALUE
94
+ values = JSONSet.deserialize(value)
95
+ values.map! { |v| deserialize_value(v) }.to_set
93
96
  else
94
97
  value.each do |k, v|
95
98
  value[k] = deserialize_value(v)
@@ -126,6 +129,16 @@ module PigeonHole
126
129
  end
127
130
 
128
131
  JSONHash.serialize(hash)
132
+ when Set
133
+ values = value.to_a.each_with_index.map do |sv, i|
134
+ begin
135
+ serialize_value(sv)
136
+ rescue UnsupportedType => e
137
+ raise e.add_index_context(i)
138
+ end
139
+ end
140
+
141
+ JSONSet.serialize(values)
129
142
  when Array
130
143
  value.each_with_index.map do |av, i|
131
144
  begin
@@ -91,6 +91,24 @@ describe "serializing arrays" do
91
91
  end
92
92
  end
93
93
 
94
+ describe "serializing sets" do
95
+ let(:set) { %i{foo bar}.to_set }
96
+
97
+ subject { PigeonHole.generate(set: set) }
98
+
99
+ it "serializes hash into a string" do
100
+ result = subject
101
+ expect(result).to_not be_empty
102
+ end
103
+
104
+ it "can be deserialized to a set" do
105
+ result = subject
106
+ hash = PigeonHole.parse(result)
107
+ expect(hash).to eq({ "set" => set })
108
+ expect(hash["set"]).to be_a(Set)
109
+ end
110
+ end
111
+
94
112
  describe "serializing nested hashes" do
95
113
  let(:input) do
96
114
  {
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.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephen Binns
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-08 00:00:00.000000000 Z
11
+ date: 2017-02-10 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -19,6 +19,7 @@ files:
19
19
  - lib/pigeon_hole.rb
20
20
  - lib/pigeon_hole/json_date.rb
21
21
  - lib/pigeon_hole/json_hash.rb
22
+ - lib/pigeon_hole/json_set.rb
22
23
  - lib/pigeon_hole/json_symbol.rb
23
24
  - lib/pigeon_hole/json_time.rb
24
25
  - lib/pigeon_hole/typed_json.rb
@@ -44,7 +45,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
44
45
  version: '0'
45
46
  requirements: []
46
47
  rubyforge_project:
47
- rubygems_version: 2.6.8
48
+ rubygems_version: 2.6.6
48
49
  signing_key:
49
50
  specification_version: 4
50
51
  summary: Opt-in typed serialization for complex JSON types