json-schema-serializer 1.5.2 → 1.6.0

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: d6ac2c2fbbc49625b87c22bba8061ef8aacff924a4e1bfa19faedbeeb3d6fcb7
4
- data.tar.gz: 4071e3b87030dc4bff3f5800a650618619b703121094afea5b9cae8b1f584689
3
+ metadata.gz: 54b6ef10587ab0aaeb6bb6aa85ae56daef39f714c356ace19352336c04864ae8
4
+ data.tar.gz: 360d4fef03c36d567ae2e229c2632bca7af26ce9c4e19ec036590917ecffdd93
5
5
  SHA512:
6
- metadata.gz: c5372d62cecbea2832b93c09b4c3c7336a54d52e342f23d7c8689266d2b848d94ffb218cb19464ba16d74464da6ccc69c4706127f85acfa383f24cbb209d9cf0
7
- data.tar.gz: 5426726b5f922839c4e21b0172a1f9281167bc2c5f41ef18baa81995d2395949624627f6b786f2c764715fabef7ba299f21d316a40d4b456543755b9b68eb3e3
6
+ metadata.gz: 4a1fe23e623221c1651c681562fedaa146578f6e495fb23989e4358e75e63d78b63b8d18443e3a52db0ff0f65a3bca58b263177cac8f616ceabe6b9bb964b2ae
7
+ data.tar.gz: 929372326ff9c370c1d04df9d36e3da7d2183a05354cb34cbaee7fe334b7e609eec75267371108c3953db7efa8edfc8208f40155bc79496e8f0632dec539df43
data/README.md CHANGED
@@ -112,6 +112,51 @@ serializer_injected = JSON::Schema::Serializer.new(
112
112
 
113
113
  serializer_injected.serialize([1, 2, 3])
114
114
  # => {"first"=>1, "count"=>3}
115
+
116
+ #
117
+ # object injector with context
118
+ #
119
+
120
+ class BarSerializer
121
+ def initialize(model, context = nil)
122
+ @model = model
123
+ @context = context
124
+ end
125
+
126
+ def id
127
+ @model[:id]
128
+ end
129
+
130
+ def score
131
+ @context[@model[:id]]
132
+ end
133
+ end
134
+
135
+ inject_context = {
136
+ 1 => 100,
137
+ 2 => 200,
138
+ }
139
+
140
+ serializer_injected_with_context = JSON::Schema::Serializer.new(
141
+ {
142
+ type: :object,
143
+ inject: :Bar,
144
+ properties: {
145
+ id: { type: :integer },
146
+ score: { type: :integer },
147
+ },
148
+ },
149
+ {
150
+ inject_key: :inject,
151
+ injectors: {
152
+ Bar: BarSerializer,
153
+ },
154
+ inject_context: inject_context,
155
+ },
156
+ )
157
+
158
+ serializer_injected_with_context.serialize({ id: 1 })
159
+ # => { "id" => 1, "score" => 100 }
115
160
  ```
116
161
 
117
162
  ### "additionalProperties"
@@ -211,12 +256,16 @@ new({
211
256
  }, { schema_key_transform_for_output: ->(name) { name.underscore } }).serialize({ userCount: 1 }) == { "user_count" => 1 }
212
257
  ```
213
258
 
214
- #### options[:injectors] [Hashlike<String, Class>, Class], options[:inject_key] [String, Symbol]
259
+ #### options[:injectors] [Hashlike<String, Class>, Class], options[:inject_key] [String, Symbol], options[:inject_context] [any]
215
260
 
216
261
  If schema has inject key, the serializer treats data by `injectors[inject_key].new(data)` (or `injectors.send(inject_key).new(data)`).
217
262
 
263
+ And if `inject_context` is present, `injectors[inject_key].new(data, inject_context)` (or `injectors.send(inject_key).new(data, inject_context)`).
264
+
218
265
  See examples in [Usage](#usage).
219
266
 
267
+ CAUTION: In many case you should define the `nil?` method in the injector class because Injector always initialized by `Injector.new(obj)` even if obj == nil.
268
+
220
269
  #### options[:null_through] [Boolean]
221
270
 
222
271
  If data is null, always serialize null.
File without changes
data/bin/fmt CHANGED
File without changes
data/bin/setup CHANGED
File without changes
@@ -28,9 +28,10 @@ Gem::Specification.new do |spec|
28
28
  spec.require_paths = %w[lib]
29
29
 
30
30
  spec.add_development_dependency "bundler", "~> 2.0"
31
- spec.add_development_dependency "rake", "~> 10.5"
31
+ spec.add_development_dependency "rake", "~> 13.0"
32
32
  spec.add_development_dependency "rspec", "~> 3.9"
33
33
  spec.add_development_dependency "rspec-power_assert", "~> 1.1"
34
+ spec.add_development_dependency "simplecov", ">= 0.18"
34
35
  spec.add_development_dependency "rubocop", "~> 0.76"
35
36
  spec.add_development_dependency "rubocop-airbnb", "~> 3"
36
37
  spec.add_development_dependency "prettier", ">= 0.16"
@@ -5,7 +5,7 @@ require "set"
5
5
  module JSON
6
6
  class Schema
7
7
  class Serializer
8
- def initialize(schema, options = nil)
8
+ def initialize(schema, options = nil) # rubocop:disable Airbnb/OptArgParameters
9
9
  @schema = options && options[:resolver] ? options[:resolver].call(schema) : schema
10
10
  @options = options || {}
11
11
  end
@@ -27,7 +27,13 @@ module JSON
27
27
  if options[:inject_key]
28
28
  inject_key = try_hash(schema, options[:inject_key])
29
29
  injector = try_hash(options[:injectors], inject_key) if inject_key
30
- obj = injector.new(obj) if injector
30
+ if injector
31
+ if options[:inject_context]
32
+ obj = injector.new(obj, options[:inject_context])
33
+ else
34
+ obj = injector.new(obj)
35
+ end
36
+ end
31
37
  end
32
38
  type_coerce(schema, detect_type(type, obj), format, obj, required, options)
33
39
  end
@@ -1,7 +1,7 @@
1
1
  module JSON
2
2
  class Schema
3
3
  class Serializer
4
- VERSION = "1.5.2".freeze
4
+ VERSION = "1.6.0".freeze
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-schema-serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.2
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Narazaka
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-12-03 00:00:00.000000000 Z
11
+ date: 2020-03-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '10.5'
33
+ version: '13.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '10.5'
40
+ version: '13.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '1.1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0.18'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0.18'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: rubocop
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -181,7 +195,7 @@ metadata:
181
195
  homepage_uri: https://github.com/Narazaka/json-schema-serializer
182
196
  source_code_uri: https://github.com/Narazaka/json-schema-serializer.git
183
197
  changelog_uri: https://github.com/Narazaka/json-schema-serializer/blob/master/CHANGELOG.md
184
- documentation_uri: https://www.rubydoc.info/gems/json-schema-serializer/1.5.2
198
+ documentation_uri: https://www.rubydoc.info/gems/json-schema-serializer/1.6.0
185
199
  post_install_message:
186
200
  rdoc_options: []
187
201
  require_paths: