pigeon_hole 0.1.0 → 0.1.1

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: 0145686d84201c9a23404e48b87a1999ecce1269
4
- data.tar.gz: cdadb67c03a83f767ab765e7236359ac396daea8
3
+ metadata.gz: db9906ed606ccac55cd7fb125ae9f45b2e1fdf54
4
+ data.tar.gz: de9b3d71ada0a7b4407f2e28713ca0f5ab83891a
5
5
  SHA512:
6
- metadata.gz: a7b86a2edc2a6dd8302533f92e6f02499c644027f8233185ef357a1f255fd97883d6f8a16aa933631dd6a36c05b7ffaec24865d173652cab75756240d98894d5
7
- data.tar.gz: 7ccca3d7722de161c8dadd6af85d616253e7f67ebfe16ddbdaabd2d7cfa2618436504f72814f18c8e141084a28b2b5d026036f7744c67ee4807a1a949b9ef489
6
+ metadata.gz: e22126d272cccf8bd411c9ef191fd78474ba310d53b71b4282aad0d5db90659ef6b1e60b75dfe4531d5b59b351762a1a23fd7a556843b8f262142de2136f6ce8
7
+ data.tar.gz: 1c9fe90d9b7a2b8018f4e54f67d7adf6fc9abe89043f16b3527dc7c6cb2b6fa67dcaed8ab749e61cac713e6482ded01ba0e505934ce806ead998d76bbdff01d6
@@ -1,6 +1,30 @@
1
1
  module PigeonHole
2
2
  class TypedJSON
3
3
  class UnsupportedType < ArgumentError
4
+ attr_reader :key
5
+ attr_reader :klass
6
+
7
+ def initialize(key, klass)
8
+ @key = key
9
+ @klass = klass
10
+
11
+ super("Serialization of #{klass} is not supported - key=#{key}")
12
+ end
13
+
14
+ def add_key_context(parent_key)
15
+ add_context(parent_key)
16
+ end
17
+
18
+ def add_index_context(index)
19
+ add_context("[#{index}]")
20
+ end
21
+
22
+ private
23
+
24
+ def add_context(context)
25
+ combined_key = [context, key].compact.join(".").sub(".[", "[")
26
+ self.class.new(combined_key, klass)
27
+ end
4
28
  end
5
29
 
6
30
  BASIC_TYPES = [
@@ -71,15 +95,25 @@ module PigeonHole
71
95
  hash = {}
72
96
 
73
97
  value.each do |k, v|
74
- hash[k] = serialize_value(v)
98
+ begin
99
+ hash[k] = serialize_value(v)
100
+ rescue UnsupportedType => e
101
+ raise e.add_key_context(k)
102
+ end
75
103
  end
76
104
 
77
105
  hash
78
106
  when Array
79
- value.map { |av| serialize_value(av) }
107
+ value.each_with_index.map do |av, i|
108
+ begin
109
+ serialize_value(av)
110
+ rescue UnsupportedType => e
111
+ raise e.add_index_context(i)
112
+ end
113
+ end
80
114
  else
81
115
  unless serializer = SERIALIZERS[value.class]
82
- raise UnsupportedType.new(value.class)
116
+ raise UnsupportedType.new(nil, value.class)
83
117
  end
84
118
 
85
119
  serializer.serialize(value)
@@ -100,15 +100,37 @@ end
100
100
  describe "serializing custom type" do
101
101
  CustomType = Struct.new(:name)
102
102
 
103
- let(:input) do
104
- {
105
- "custom" => CustomType.new("hello"),
106
- }
103
+ context "in a hash" do
104
+ let(:input) do
105
+ {
106
+ "nested" => {
107
+ "custom" => CustomType.new("hello"),
108
+ }
109
+ }
110
+ end
111
+
112
+ subject { PigeonHole.generate(input) }
113
+
114
+ it "raises an unsupported type error" do
115
+ expect { subject }.to raise_error(PigeonHole::TypedJSON::UnsupportedType, "Serialization of CustomType is not supported - key=nested.custom")
116
+ end
107
117
  end
108
118
 
109
- subject { PigeonHole.generate(input) }
119
+ context "in an array" do
120
+ let(:input) do
121
+ {
122
+ "nested" => [
123
+ "string",
124
+ 1337,
125
+ CustomType.new("hello"),
126
+ ]
127
+ }
128
+ end
129
+
130
+ subject { PigeonHole.generate(input) }
110
131
 
111
- it "raises an unsupported type error" do
112
- expect { subject }.to raise_error(PigeonHole::TypedJSON::UnsupportedType)
132
+ it "raises an unsupported type error" do
133
+ expect { subject }.to raise_error(PigeonHole::TypedJSON::UnsupportedType, "Serialization of CustomType is not supported - key=nested[2]")
134
+ end
113
135
  end
114
136
  end
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.0
4
+ version: 0.1.1
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-07-29 00:00:00.000000000 Z
11
+ date: 2016-08-09 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -43,10 +43,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
43
43
  version: '0'
44
44
  requirements: []
45
45
  rubyforge_project:
46
- rubygems_version: 2.4.8
46
+ rubygems_version: 2.6.6
47
47
  signing_key:
48
48
  specification_version: 4
49
49
  summary: Opt-in typed serialization for complex JSON types
50
50
  test_files:
51
51
  - spec/integration_spec.rb
52
52
  - spec/spec_helper.rb
53
+ has_rdoc: