json-schema-serializer 1.1.0 → 1.2.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: d1a4d5629a30b0c5aedc94644fc57e231477a4987c338487ca579ec9141cad5e
4
- data.tar.gz: 33df5ee8f27d068a35fa1637caeea613afa650dbbcee5dd62a24945c1b06c846
3
+ metadata.gz: 466a7da01b15a1e212581fdac863dbea33ff469fdc41aa8b158a7203f4e8082e
4
+ data.tar.gz: 29e92d4f1bb61a91ded2b2d88f8ab3afe7a2470678b25f6b3bf2e15f1998637c
5
5
  SHA512:
6
- metadata.gz: ae32409a4b3d94544a1f6e2ae4a1863762d40d6e8c62e53ddb289ce3782b0ae634a59cf9d8a30c5cfd5ef6f48b6a06fa5cdbfa092229934d10939ba4ec79d723
7
- data.tar.gz: f0585aba113213442c38167ad46864b5402ae72893b5bb72d7e18316e287f40c1c7d0881478ad328d75e984c172bab7ea3981dda0bbf46611558d01b2de55fab
6
+ metadata.gz: c8c9a3c7b6d722b06c898feb3b47fdc50eeaa7413a52d97df02e620fa8acb1834bee8bef88a13f112dec849d2efdf1090b68fd6d86d40e9237f8ef7d364b8bbc
7
+ data.tar.gz: 4ff50230eeaa258532d6219e319879805a9a596629b87f195f1d82c8aa048f1d8a9f9a66963598521f71f193363ba8684141e76bb81a28ed33b43feaf5f222ae
data/README.md CHANGED
@@ -74,6 +74,44 @@ serializer2 = JSON::Schema::Serializer.new(Schema.new)
74
74
  serializer2.serialize(32)
75
75
  # => "32"
76
76
  # non-hash schema allowed
77
+
78
+ #
79
+ # object injector allowed!
80
+ #
81
+
82
+ class FooSerializer
83
+ def initialize(model)
84
+ @model = model
85
+ end
86
+
87
+ def first
88
+ @model.first
89
+ end
90
+
91
+ def count
92
+ @model.size
93
+ end
94
+ end
95
+
96
+ serializer_injected = JSON::Schema::Serializer.new(
97
+ {
98
+ type: :object,
99
+ inject: :Foo,
100
+ properties: {
101
+ first: { type: :integer },
102
+ count: { type: :integer },
103
+ },
104
+ },
105
+ {
106
+ inject_key: :inject,
107
+ injectors: {
108
+ Foo: FooSerializer,
109
+ },
110
+ },
111
+ )
112
+
113
+ serializer_injected.serialize([1, 2, 3])
114
+ # => {"first"=>1, "count"=>3}
77
115
  ```
78
116
 
79
117
  ### Caution
@@ -5,24 +5,30 @@ require "set"
5
5
  module JSON
6
6
  class Schema
7
7
  class Serializer
8
- def initialize(obj)
8
+ def initialize(obj, options = {})
9
9
  @schema = obj
10
+ @options = options
10
11
  end
11
12
 
12
13
  def serialize(obj)
13
- Walker.walk(@schema, obj, true)
14
+ Walker.walk(@schema, obj, true, @options)
14
15
  end
15
16
 
16
17
  class Walker
17
18
  class << self
18
19
  TimeWithZone = defined?(ActiveSupport::TimeWithZone) ? ActiveSupport::TimeWithZone : nil
19
20
 
20
- def walk(schema, obj, required)
21
+ def walk(schema, obj, required, options)
21
22
  type = try_hash(schema, :type)
22
23
  default = try_hash(schema, :default)
23
24
  format = try_hash(schema, :format)
24
25
  obj = default if obj.nil?
25
- type_coerce(schema, detect_type(type, obj), format, obj, required)
26
+ if options[:inject_key]
27
+ inject_key = try_hash(schema, options[:inject_key])
28
+ injector = try_hash(options[:injectors], inject_key) if inject_key
29
+ obj = injector.new(obj) if injector
30
+ end
31
+ type_coerce(schema, detect_type(type, obj), format, obj, required, options)
26
32
  end
27
33
 
28
34
  def detect_type(type, obj)
@@ -105,7 +111,7 @@ module JSON
105
111
  end
106
112
  end
107
113
 
108
- def type_coerce(schema, type, format, obj, required)
114
+ def type_coerce(schema, type, format, obj, required, options)
109
115
  return nil if !required && obj.nil?
110
116
 
111
117
  case type.to_s
@@ -151,12 +157,12 @@ module JSON
151
157
  obj == true
152
158
  when "array"
153
159
  items_schema = try_hash(schema, :items)
154
- obj.nil? ? [] : obj.map { |item| walk(items_schema, item, true) }
160
+ obj.nil? ? [] : obj.map { |item| walk(items_schema, item, true, options) }
155
161
  when "object"
156
162
  properties_schema = try_hash(schema, :properties)
157
163
  required_schema = Set.new(try_hash(schema, :required)&.map(&:to_s))
158
164
  properties_schema.map do |name, property_schema|
159
- [name.to_s, walk(property_schema, try_hash(obj, name), required_schema.include?(name.to_s))]
165
+ [name.to_s, walk(property_schema, try_hash(obj, name), required_schema.include?(name.to_s), options)]
160
166
  end.to_h
161
167
  end
162
168
  end
@@ -1,7 +1,7 @@
1
1
  module Json
2
2
  module Schema
3
3
  module Serializer
4
- VERSION = "1.1.0".freeze
4
+ VERSION = "1.2.0".freeze
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-schema-serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Narazaka