iknow_params 2.2.3 → 2.2.4

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
  SHA256:
3
- metadata.gz: b89e48380562cd7aa3238e08fbbeb1f947b336eebc3a47582de1ac648da582d0
4
- data.tar.gz: 0d899f1d4a4ce279a9fe50d651769a29c542f9cdd4c7753f8cf9c512b7f47047
3
+ metadata.gz: e04e6c4dd0a375bed586bd4c0107dccfa2a1226fd42e16397cf7e8cbc74a5dc9
4
+ data.tar.gz: a1b11326a2487733f032f2a095ae1e15bf56e2254dc11eab6522710178109a29
5
5
  SHA512:
6
- metadata.gz: c94310c197e1cb3537be0cbd22768e0d557402a11b40c7c692cfc7966d247bfd7455624d54e0f46a41bb049d12db2798c6cb7070a41f168285a10528d5d2d57a
7
- data.tar.gz: 5df5a8a5ebc3cddcb6509faa6ed446ad23265daa5be55f65e1f959f92b554de9388f2ccda020c8dfa226f359b0fa3a39fc16e8453c59be76440e1865b1ab7a12
6
+ metadata.gz: 7d883780b8b0ba7bf8175f99d79454451a0767d42c3eadc4965d31a8f73cc8cd6748d79d99411da10dcdeb686bb154e3294d4074a2a0ffbdaae84996e26bce8f
7
+ data.tar.gz: c478dacd778625d53f71f28bbd793d00b4558d9e327b52d3f0a77a4ae1b8f879c91bd3bdbdcdca77a0fea8cab188d495e12895d58f98128364c4e69f28cb5ab8
@@ -27,6 +27,7 @@ Gem::Specification.new do |spec|
27
27
 
28
28
  spec.add_development_dependency "byebug"
29
29
  spec.add_development_dependency "appraisal"
30
+ spec.add_development_dependency "tzinfo-data"
30
31
 
31
32
  spec.add_dependency "activesupport", "> 5.0"
32
33
  spec.add_dependency "tzinfo"
@@ -83,6 +83,10 @@ class IknowParams::Serializer
83
83
  end
84
84
  end
85
85
 
86
+ require 'iknow_params/serializer/nullable'
87
+ require 'iknow_params/serializer/array_of'
88
+ require 'iknow_params/serializer/hash_of'
89
+
86
90
  class String < IknowParams::Serializer
87
91
  def initialize
88
92
  super(::String)
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Serialize an array of application types to JSON types. Does not support fully
4
+ # rendering to/from a JSON string.
5
+ class IknowParams::Serializer::ArrayOf < IknowParams::Serializer
6
+ attr_reader :serializer, :allow_singleton
7
+ json_value!
8
+
9
+ def initialize(serializer, allow_singleton: false)
10
+ super(::Array)
11
+ @serializer = serializer
12
+ @allow_singleton = allow_singleton
13
+ end
14
+
15
+ def load(jvals)
16
+ if allow_singleton
17
+ jvals = Array.wrap(jvals)
18
+ else
19
+ unless jvals.is_a?(Array)
20
+ raise IknowParams::Serializer::LoadError.new(
21
+ "Incorrect type for ArrayOf: #{jvals.inspect}:#{jvals.class.name} is not an array")
22
+ end
23
+ end
24
+
25
+ # Special case thanks to Rails' array query param format not differentating
26
+ # empty arrays (i.e. `route?param[]=`). Since we can only express one of
27
+ # empty array and singleton array containing empty string, we pick the more
28
+ # useful former.
29
+ return [] if jvals == ['']
30
+
31
+ jvals.map { |jval| serializer.load(jval) }
32
+ end
33
+
34
+ def dump(vals, json: true)
35
+ matches_type!(vals)
36
+ vals.map { |val| serializer.dump(val, json: json) }
37
+ end
38
+
39
+ def matches_type?(vals)
40
+ super(vals) && vals.all? { |val| serializer.matches_type?(val) }
41
+ end
42
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Serialize a hash structure of application types to JSON types. Does not
4
+ # support fully rendering to/from a JSON string.
5
+ class IknowParams::Serializer::HashOf < IknowParams::Serializer
6
+ attr_reader :key_serializer, :value_serializer
7
+ json_value!
8
+
9
+ def initialize(key_serializer, value_serializer)
10
+ super(::Hash)
11
+ @key_serializer = key_serializer
12
+ @value_serializer = value_serializer
13
+ end
14
+
15
+ def load(jstructure)
16
+ jstructure = jstructure.to_unsafe_h if jstructure.is_a?(ActionController::Parameters)
17
+
18
+ unless jstructure.is_a?(Hash)
19
+ raise IknowParams::Serializer::LoadError.new(
20
+ "Incorrect type for HashOf: #{jstructure.inspect}:#{jstructure.class.name} is not a hash")
21
+ end
22
+
23
+ jstructure.each_with_object({}) do |(jkey, jvalue), result|
24
+ key = key_serializer.load(jkey)
25
+ value = value_serializer.load(jvalue)
26
+ result[key] = value
27
+ end
28
+ end
29
+
30
+ def dump(structure, json: true)
31
+ matches_type!(structure)
32
+ structure.each_with_object({}) do |(key, value), result|
33
+ jkey = key_serializer.dump(key, json: false)
34
+ jvalue = value_serializer.dump(value, json: json)
35
+ result[jkey] = jvalue
36
+ end
37
+ end
38
+
39
+ def matches_type?(vals)
40
+ super(vals) && vals.all? do |k_val, v_val|
41
+ key_serializer.matches_type?(k_val) && value_serializer.matches_type?(v_val)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Serialize a potentially nil application type to JSON types. Does not support
4
+ # fully rendering to/from a string.
5
+ class IknowParams::Serializer::Nullable < IknowParams::Serializer
6
+ attr_reader :serializer
7
+
8
+ def load(val)
9
+ if val.nil?
10
+ nil
11
+ else
12
+ serializer.load(val)
13
+ end
14
+ end
15
+
16
+ def dump(val, json: false)
17
+ if val.nil?
18
+ nil
19
+ else
20
+ serializer.dump(val, json: json)
21
+ end
22
+ end
23
+
24
+ def matches_type?(val)
25
+ val.nil? || super
26
+ end
27
+
28
+ def json_value?
29
+ true
30
+ end
31
+
32
+ def initialize(serializer)
33
+ @serializer = serializer
34
+ end
35
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module IknowParams
4
- VERSION = '2.2.3'
4
+ VERSION = '2.2.4'
5
5
  end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'iknow_params'
5
+ require 'rails'
6
+
7
+ RSpec.describe IknowParams::Serializer::ArrayOf do
8
+ let(:subject) { IknowParams::Serializer::ArrayOf.new(IknowParams::Serializer::Date) }
9
+
10
+ let(:serialized_dates) { ['2000-01-01', '2000-01-02'] }
11
+ let(:dates) { serialized_dates.map { |d| Date.parse(d) } }
12
+
13
+ describe '#load' do
14
+ it 'can load correct values' do
15
+ expect(subject.load(serialized_dates)).to eq(dates)
16
+ end
17
+
18
+ it 'raises load error on incorrect type' do
19
+ expect { subject.load(12) }.to raise_error(IknowParams::Serializer::LoadError)
20
+ end
21
+ end
22
+
23
+ describe '#dump' do
24
+ it 'can dump correct values' do
25
+ expect(subject.dump(dates)).to eq(serialized_dates)
26
+ end
27
+
28
+ it 'raises dump error on incorrect type' do
29
+ expect { subject.dump(12) }.to raise_error(IknowParams::Serializer::DumpError)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'iknow_params'
5
+ require 'rails'
6
+
7
+ RSpec.describe IknowParams::Serializer::HashOf do
8
+ let(:subject) { IknowParams::Serializer::HashOf.new(IknowParams::Serializer::Integer, IknowParams::Serializer::Date) }
9
+
10
+ let(:serialized_dates) { { '1' => '2000-01-01', '2' => '2000-01-02' } }
11
+ let(:dates) do
12
+ { 1 => Date.parse('2000-01-01'), 2 => Date.parse('2000-01-02') }
13
+ end
14
+
15
+ describe '#load' do
16
+ it 'can load correct values' do
17
+ expect(subject.load(serialized_dates)).to eq(dates)
18
+ end
19
+
20
+ it 'raises load error on incorrect type' do
21
+ expect { subject.load(12) }.to raise_error(IknowParams::Serializer::LoadError)
22
+ end
23
+ end
24
+
25
+ describe '#dump' do
26
+ it 'can dump correct values' do
27
+ expect(subject.dump(dates)).to eq(serialized_dates)
28
+ end
29
+
30
+ it 'raises dump error on incorrect type' do
31
+ expect { subject.dump(12) }.to raise_error(IknowParams::Serializer::DumpError)
32
+ end
33
+ end
34
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iknow_params
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.3
4
+ version: 2.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - dev@iknow.jp
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-02-26 00:00:00.000000000 Z
11
+ date: 2020-09-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -122,6 +122,20 @@ dependencies:
122
122
  - - ">="
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: tzinfo-data
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
125
139
  - !ruby/object:Gem::Dependency
126
140
  name: activesupport
127
141
  requirement: !ruby/object:Gem::Requirement
@@ -187,9 +201,14 @@ files:
187
201
  - lib/iknow_params/parser.rb
188
202
  - lib/iknow_params/parser/hash_parser.rb
189
203
  - lib/iknow_params/serializer.rb
204
+ - lib/iknow_params/serializer/array_of.rb
205
+ - lib/iknow_params/serializer/hash_of.rb
206
+ - lib/iknow_params/serializer/nullable.rb
190
207
  - lib/iknow_params/version.rb
191
208
  - spec/spec_helper.rb
192
209
  - spec/unit/parser_spec.rb
210
+ - spec/unit/serializer/array_of_spec.rb
211
+ - spec/unit/serializer/hash_of_spec.rb
193
212
  - spec/unit/serializer_spec.rb
194
213
  homepage: http://github.com/iknow/iknow_params
195
214
  licenses:
@@ -217,4 +236,6 @@ summary: Rails parameter parser for iKnow.
217
236
  test_files:
218
237
  - spec/spec_helper.rb
219
238
  - spec/unit/parser_spec.rb
239
+ - spec/unit/serializer/array_of_spec.rb
240
+ - spec/unit/serializer/hash_of_spec.rb
220
241
  - spec/unit/serializer_spec.rb