serega 0.8.2 → 0.8.3

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: 0d467016ac66fcd328919f3d7068dd556ac5a7768f657adb30948c2c316480a1
4
- data.tar.gz: 3ae0c46dc6760cdf94299c0edc34b41a8b195cea92dff13b91a4017bb21653f7
3
+ metadata.gz: 116b00d5c709f65a90899a9eda49f6e2c95cc9a47781a1cddf2499057553f427
4
+ data.tar.gz: 5f8fe19470218be68c09704964228be0381e677f5bd3cbcf9688f92974e9392d
5
5
  SHA512:
6
- metadata.gz: 9f2a9f714be636a26f4ca2de8a5f692455080354f495dc5cc2d69dd874d207ddd0bc3286b47ea600ebf5a0ba2ddfd774d6876da67e82906d35319f4a54614caa
7
- data.tar.gz: 766931dbb8aaa415dcc008a477258ba6f63ca35b664267278ee31ab0becc91f124e16eb1f57bd5968df03e333bb59c98a8396f796be84c254a76e674a472d6e1
6
+ metadata.gz: 047de5335017cf65ee0f887124f81a755b94e19eea0e53a56ae24cb81698e7bdcc7e8eb6eba9256b5160a24bb011a5c0a1c1628cca77c7e7951b8883629ad96f
7
+ data.tar.gz: 5836df28c6e1cf2238d8c8471280dc806dd41a0cbbe474a101118d801e460830cda97dcb419e0885f9106d1e4e5ae795776ff5cc475555bdab6cb4c5408c1c94
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.8.2
1
+ 0.8.3
@@ -6,6 +6,9 @@ class Serega
6
6
  # JSON dump adapter for ::Oj
7
7
  #
8
8
  class OjDump
9
+ # Default Oj serialization options
10
+ OPTS = {mode: :compat}.freeze
11
+
9
12
  #
10
13
  # Dumps data to JSON string
11
14
  #
@@ -14,7 +17,7 @@ class Serega
14
17
  # @return [String] Data serialized to JSON
15
18
  #
16
19
  def self.call(data)
17
- ::Oj.dump(data, mode: :compat)
20
+ ::Oj.dump(data, OPTS)
18
21
  end
19
22
  end
20
23
 
data/lib/serega/map.rb CHANGED
@@ -35,7 +35,8 @@ class Serega
35
35
 
36
36
  def cached_map_for(opts, max_cache_size)
37
37
  @cache ||= {}
38
- cache_key = opts.to_s
38
+ cache_key = construct_cache_key(opts)
39
+
39
40
  map = @cache[cache_key] ||= map_for(opts)
40
41
  @cache.shift if @cache.length > max_cache_size
41
42
  map
@@ -50,7 +51,8 @@ class Serega
50
51
  end
51
52
 
52
53
  def construct_map(serializer_class, only:, except:, with:)
53
- serializer_class.attributes.each_with_object([]) do |(name, attribute), map|
54
+ map = []
55
+ serializer_class.attributes.each do |name, attribute|
54
56
  next unless attribute.visible?(only: only, except: except, with: with)
55
57
 
56
58
  nested_points =
@@ -65,6 +67,21 @@ class Serega
65
67
 
66
68
  map << serializer_class::SeregaMapPoint.new(attribute, nested_points)
67
69
  end
70
+ map
71
+ end
72
+
73
+ def construct_cache_key(opts, cache_key = nil)
74
+ return nil if opts.empty?
75
+
76
+ cache_key ||= +""
77
+
78
+ opts.each do |key, nested_opts|
79
+ cache_key.insert(-1, SeregaUtils::SymbolName.call(key))
80
+ cache_key.insert(-1, "-")
81
+ construct_cache_key(nested_opts, cache_key)
82
+ end
83
+
84
+ cache_key
68
85
  end
69
86
  end
70
87
 
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "stringio"
4
+
3
5
  class Serega
4
6
  module SeregaPlugins
5
7
  #
@@ -14,7 +16,7 @@ class Serega
14
16
  # PostSerializer.new(only: "id,user(id,username)").to_h(post)
15
17
  # PostSerializer.new(except: "user(username,email)").to_h(post)
16
18
  # PostSerializer.new(with: "user(email)").to_h(post)
17
-
19
+ #
18
20
  # # Modifiers can still be provided old way with nested hashes or arrays.
19
21
  # PostSerializer.new(with: {user: %i[email, username]}).to_h(post)
20
22
  #
@@ -52,9 +54,11 @@ class Serega
52
54
  def parse(fields)
53
55
  res = {}
54
56
  attribute = +""
57
+ char = +""
55
58
  path_stack = nil
59
+ fields = StringIO.new(fields)
56
60
 
57
- fields.each_char do |char|
61
+ while fields.read(1, char)
58
62
  case char
59
63
  when ","
60
64
  add_attribute(res, path_stack, attribute, FROZEN_EMPTY_HASH)
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Serega
4
+ module SeregaUtils
5
+ #
6
+ # Utility to get frozen string from symbol in any ruby version
7
+ #
8
+ class SymbolName
9
+ class << self
10
+ #
11
+ # Returns frozen string corresponding to provided symbol
12
+ #
13
+ # @param key [Symbol]
14
+ #
15
+ # @return [String] frozen string corresponding to provided symbol
16
+ #
17
+ def call(key)
18
+ key.is_a?(String) ? key : to_frozen_string(key)
19
+ end
20
+
21
+ private
22
+
23
+ # :nocov:
24
+ if RUBY_VERSION < "3"
25
+ def to_frozen_string(key)
26
+ key.to_s.freeze
27
+ end
28
+ else
29
+ def to_frozen_string(key)
30
+ key.name
31
+ end
32
+ end
33
+ # :nocov:
34
+ end
35
+ end
36
+ end
37
+ end
@@ -27,7 +27,7 @@ class Serega
27
27
  # @return [void]
28
28
  #
29
29
  def call(name)
30
- name = name.to_s
30
+ name = SeregaUtils::SymbolName.call(name)
31
31
 
32
32
  valid =
33
33
  case name.size
data/lib/serega.rb CHANGED
@@ -16,6 +16,7 @@ end
16
16
  require_relative "serega/errors"
17
17
  require_relative "serega/helpers/serializer_class_helper"
18
18
  require_relative "serega/utils/enum_deep_dup"
19
+ require_relative "serega/utils/symbol_name"
19
20
  require_relative "serega/utils/to_hash"
20
21
  require_relative "serega/json/adapter"
21
22
 
@@ -184,7 +185,7 @@ class Serega
184
185
  # Serializes provided object to Hash
185
186
  #
186
187
  # @param object [Object] Serialized object
187
- # @param opts [Hash] Serializer modifiers and other instantiating options
188
+ # @param opts [Hash, nil] Serializer modifiers and other instantiating options
188
189
  # @option opts [Array, Hash, String, Symbol] :only The only attributes to serialize
189
190
  # @option opts [Array, Hash, String, Symbol] :except Attributes to hide
190
191
  # @option opts [Array, Hash, String, Symbol] :with Attributes (usually hidden) to serialize additionally
@@ -194,13 +195,23 @@ class Serega
194
195
  #
195
196
  # @return [Hash] Serialization result
196
197
  #
197
- def call(object, opts = FROZEN_EMPTY_HASH)
198
+ def call(object, opts = nil)
199
+ opts ||= FROZEN_EMPTY_HASH
198
200
  initiate_keys = config.initiate_keys
199
- new(opts.slice(*initiate_keys)).to_h(object, opts.except(*initiate_keys))
201
+
202
+ if opts.empty?
203
+ modifiers_opts = FROZEN_EMPTY_HASH
204
+ serialize_opts = nil
205
+ else
206
+ serialize_opts = opts.except(*initiate_keys)
207
+ modifiers_opts = opts.slice(*initiate_keys)
208
+ end
209
+
210
+ new(modifiers_opts).to_h(object, serialize_opts)
200
211
  end
201
212
 
202
213
  # @see #call
203
- def to_h(object, opts = FROZEN_EMPTY_HASH)
214
+ def to_h(object, opts = nil)
204
215
  call(object, opts)
205
216
  end
206
217
 
@@ -208,7 +219,7 @@ class Serega
208
219
  # Serializes provided object to JSON string
209
220
  #
210
221
  # @param object [Object] Serialized object
211
- # @param opts [Hash] Serializer modifiers and other instantiating options
222
+ # @param opts [Hash, nil] Serializer modifiers and other instantiating options
212
223
  # @option opts [Array, Hash, String, Symbol] :only The only attributes to serialize
213
224
  # @option opts [Array, Hash, String, Symbol] :except Attributes to hide
214
225
  # @option opts [Array, Hash, String, Symbol] :with Attributes (usually hidden) to serialize additionally
@@ -218,16 +229,15 @@ class Serega
218
229
  #
219
230
  # @return [String] Serialization result
220
231
  #
221
- def to_json(object, opts = FROZEN_EMPTY_HASH)
222
- initiate_keys = config.initiate_keys
223
- new(opts.slice(*initiate_keys)).to_json(object, opts.except(*initiate_keys))
232
+ def to_json(object, opts = nil)
233
+ config.to_json.call(to_h(object, opts))
224
234
  end
225
235
 
226
236
  #
227
237
  # Serializes provided object as JSON
228
238
  #
229
239
  # @param object [Object] Serialized object
230
- # @param opts [Hash] Serializer modifiers and other instantiating options
240
+ # @param opts [Hash, nil] Serializer modifiers and other instantiating options
231
241
  # @option opts [Array, Hash, String, Symbol] :only The only attributes to serialize
232
242
  # @option opts [Array, Hash, String, Symbol] :except Attributes to hide
233
243
  # @option opts [Array, Hash, String, Symbol] :with Attributes (usually hidden) to serialize additionally
@@ -237,7 +247,7 @@ class Serega
237
247
  #
238
248
  # @return [Hash] Serialization result
239
249
  #
240
- def as_json(object, opts = FROZEN_EMPTY_HASH)
250
+ def as_json(object, opts = nil)
241
251
  config.from_json.call(to_json(object, opts))
242
252
  end
243
253
  end
@@ -249,36 +259,37 @@ class Serega
249
259
  #
250
260
  # Instantiates new Serega class
251
261
  #
252
- # @param opts [Hash] Serializer modifiers and other instantiating options
262
+ # @param opts [Hash, nil] Serializer modifiers and other instantiating options
253
263
  # @option opts [Array, Hash, String, Symbol] :only The only attributes to serialize
254
264
  # @option opts [Array, Hash, String, Symbol] :except Attributes to hide
255
265
  # @option opts [Array, Hash, String, Symbol] :with Attributes (usually hidden) to serialize additionally
256
266
  # @option opts [Boolean] :validate Validates provided modifiers (Default is true)
257
267
  #
258
- def initialize(opts = FROZEN_EMPTY_HASH)
259
- @opts = (opts == FROZEN_EMPTY_HASH) ? opts : prepare_modifiers(opts)
260
- self.class::CheckInitiateParams.new(@opts).validate if opts.fetch(:check_initiate_params) { config.check_initiate_params }
268
+ def initialize(opts = nil)
269
+ @opts = (opts.nil? || opts.empty?) ? FROZEN_EMPTY_HASH : prepare_modifiers(opts)
270
+ self.class::CheckInitiateParams.new(@opts).validate if opts&.fetch(:check_initiate_params) { config.check_initiate_params }
261
271
  end
262
272
 
263
273
  #
264
274
  # Serializes provided object to Hash
265
275
  #
266
276
  # @param object [Object] Serialized object
267
- # @param opts [Hash] Serializer modifiers and other instantiating options
277
+ # @param opts [Hash, nil] Serializer modifiers and other instantiating options
268
278
  # @option opts [Hash] :context Serialization context
269
279
  # @option opts [Boolean] :many Set true if provided multiple objects (Default `object.is_a?(Enumerable)`)
270
280
  #
271
281
  # @return [Hash] Serialization result
272
282
  #
273
- def call(object, opts = {})
274
- self.class::CheckSerializeParams.new(opts).validate
283
+ def call(object, opts = nil)
284
+ self.class::CheckSerializeParams.new(opts).validate if opts&.any?
285
+ opts ||= {}
275
286
  opts[:context] ||= {}
276
287
 
277
288
  serialize(object, opts)
278
289
  end
279
290
 
280
291
  # @see #call
281
- def to_h(object, opts = {})
292
+ def to_h(object, opts = nil)
282
293
  call(object, opts)
283
294
  end
284
295
 
@@ -286,13 +297,13 @@ class Serega
286
297
  # Serializes provided object to JSON string
287
298
  #
288
299
  # @param object [Object] Serialized object
289
- # @param opts [Hash] Serializer modifiers and other instantiating options
300
+ # @param opts [Hash, nil] Serializer modifiers and other instantiating options
290
301
  # @option opts [Hash] :context Serialization context
291
302
  # @option opts [Boolean] :many Set true if provided multiple objects (Default `object.is_a?(Enumerable)`)
292
303
  #
293
304
  # @return [Hash] Serialization result
294
305
  #
295
- def to_json(object, opts = {})
306
+ def to_json(object, opts = nil)
296
307
  hash = to_h(object, opts)
297
308
  config.to_json.call(hash)
298
309
  end
@@ -301,13 +312,13 @@ class Serega
301
312
  # Serializes provided object as JSON
302
313
  #
303
314
  # @param object [Object] Serialized object
304
- # @param opts [Hash] Serializer modifiers and other instantiating options
315
+ # @param opts [Hash, nil] Serializer modifiers and other instantiating options
305
316
  # @option opts [Hash] :context Serialization context
306
317
  # @option opts [Boolean] :many Set true if provided multiple objects (Default `object.is_a?(Enumerable)`)
307
318
  #
308
319
  # @return [Hash] Serialization result
309
320
  #
310
- def as_json(object, opts = {})
321
+ def as_json(object, opts = nil)
311
322
  json = to_json(object, opts)
312
323
  config.from_json.call(json)
313
324
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: serega
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.2
4
+ version: 0.8.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrey Glushkov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-12-20 00:00:00.000000000 Z
11
+ date: 2023-02-14 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  JSON Serializer
@@ -73,6 +73,7 @@ files:
73
73
  - lib/serega/plugins/string_modifiers/parse_string_modifiers.rb
74
74
  - lib/serega/plugins/string_modifiers/string_modifiers.rb
75
75
  - lib/serega/utils/enum_deep_dup.rb
76
+ - lib/serega/utils/symbol_name.rb
76
77
  - lib/serega/utils/to_hash.rb
77
78
  - lib/serega/validations/attribute/check_block.rb
78
79
  - lib/serega/validations/attribute/check_name.rb
@@ -114,7 +115,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
114
115
  - !ruby/object:Gem::Version
115
116
  version: '0'
116
117
  requirements: []
117
- rubygems_version: 3.3.15
118
+ rubygems_version: 3.4.6
118
119
  signing_key:
119
120
  specification_version: 4
120
121
  summary: JSON Serializer