serega 0.8.1 → 0.8.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/VERSION +1 -1
- data/lib/serega/attribute.rb +19 -3
- data/lib/serega/json/oj.rb +4 -1
- data/lib/serega/map.rb +19 -2
- data/lib/serega/plugins/batch/batch.rb +11 -55
- data/lib/serega/plugins/batch/lib/batch_option_model.rb +73 -0
- data/lib/serega/plugins/batch/lib/plugins_extensions/activerecord_preloads.rb +43 -0
- data/lib/serega/plugins/batch/lib/plugins_extensions/formatters.rb +36 -0
- data/lib/serega/plugins/batch/lib/plugins_extensions/preloads.rb +32 -0
- data/lib/serega/plugins/string_modifiers/parse_string_modifiers.rb +6 -2
- data/lib/serega/utils/symbol_name.rb +37 -0
- data/lib/serega/validations/attribute/check_name.rb +1 -1
- data/lib/serega.rb +33 -22
- metadata +8 -4
- data/lib/serega/plugins/batch/lib/plugins_extensions.rb +0 -62
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 116b00d5c709f65a90899a9eda49f6e2c95cc9a47781a1cddf2499057553f427
|
4
|
+
data.tar.gz: 5f8fe19470218be68c09704964228be0381e677f5bd3cbcf9688f92974e9392d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 047de5335017cf65ee0f887124f81a755b94e19eea0e53a56ae24cb81698e7bdcc7e8eb6eba9256b5160a24bb011a5c0a1c1628cca77c7e7951b8883629ad96f
|
7
|
+
data.tar.gz: 5836df28c6e1cf2238d8c8471280dc806dd41a0cbbe474a101118d801e460830cda97dcb419e0885f9106d1e4e5ae795776ff5cc475555bdab6cb4c5408c1c94
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.8.
|
1
|
+
0.8.3
|
data/lib/serega/attribute.rb
CHANGED
@@ -136,7 +136,9 @@ class Serega
|
|
136
136
|
|
137
137
|
def keyword_block
|
138
138
|
key_method_name = key
|
139
|
-
proc
|
139
|
+
proc do |object|
|
140
|
+
handle_no_method_error { object.public_send(key_method_name) }
|
141
|
+
end
|
140
142
|
end
|
141
143
|
|
142
144
|
def delegate_block
|
@@ -147,11 +149,25 @@ class Serega
|
|
147
149
|
delegate_to = delegate[:to]
|
148
150
|
|
149
151
|
if delegate[:allow_nil]
|
150
|
-
proc
|
152
|
+
proc do |object|
|
153
|
+
handle_no_method_error do
|
154
|
+
object.public_send(delegate_to)&.public_send(key_method_name)
|
155
|
+
end
|
156
|
+
end
|
151
157
|
else
|
152
|
-
proc
|
158
|
+
proc do |object|
|
159
|
+
handle_no_method_error do
|
160
|
+
object.public_send(delegate_to).public_send(key_method_name)
|
161
|
+
end
|
162
|
+
end
|
153
163
|
end
|
154
164
|
end
|
165
|
+
|
166
|
+
def handle_no_method_error
|
167
|
+
yield
|
168
|
+
rescue NoMethodError => error
|
169
|
+
raise error, "NoMethodError when serializing '#{name}' attribute in #{self.class.serializer_class}\n\n#{error.message}", error.backtrace
|
170
|
+
end
|
155
171
|
end
|
156
172
|
|
157
173
|
extend Serega::SeregaHelpers::SerializerClassHelper
|
data/lib/serega/json/oj.rb
CHANGED
@@ -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,
|
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
|
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
|
-
|
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
|
|
@@ -50,6 +50,7 @@ class Serega
|
|
50
50
|
# @return [void]
|
51
51
|
#
|
52
52
|
def self.load_plugin(serializer_class, **_opts)
|
53
|
+
require_relative "./lib/batch_option_model"
|
53
54
|
require_relative "./lib/loader"
|
54
55
|
require_relative "./lib/loaders"
|
55
56
|
require_relative "./lib/validations/check_batch_opt_key"
|
@@ -87,14 +88,19 @@ class Serega
|
|
87
88
|
serializer_class.const_set(:SeregaBatchLoader, batch_loader_class)
|
88
89
|
|
89
90
|
if serializer_class.plugin_used?(:activerecord_preloads)
|
90
|
-
require_relative "./lib/plugins_extensions"
|
91
|
+
require_relative "./lib/plugins_extensions/activerecord_preloads"
|
91
92
|
serializer_class::SeregaBatchLoader.include(PluginsExtensions::ActiveRecordPreloads::BatchLoaderInstanceMethods)
|
92
93
|
end
|
93
94
|
|
94
95
|
if serializer_class.plugin_used?(:formatters)
|
95
|
-
require_relative "./lib/plugins_extensions"
|
96
|
+
require_relative "./lib/plugins_extensions/formatters"
|
96
97
|
serializer_class::SeregaBatchLoader.include(PluginsExtensions::Formatters::BatchLoaderInstanceMethods)
|
97
98
|
end
|
99
|
+
|
100
|
+
if serializer_class.plugin_used?(:preloads)
|
101
|
+
require_relative "./lib/plugins_extensions/preloads"
|
102
|
+
serializer_class::SeregaAttribute.include(PluginsExtensions::Preloads::AttributeInstanceMethods)
|
103
|
+
end
|
98
104
|
end
|
99
105
|
|
100
106
|
#
|
@@ -140,56 +146,6 @@ class Serega
|
|
140
146
|
end
|
141
147
|
end
|
142
148
|
|
143
|
-
#
|
144
|
-
# Stores batch config for specific attribute
|
145
|
-
#
|
146
|
-
class BatchModel
|
147
|
-
attr_reader :opts, :loaders, :many
|
148
|
-
|
149
|
-
#
|
150
|
-
# Initializes batch model
|
151
|
-
#
|
152
|
-
# @param opts [Hash] Attribute :batch option
|
153
|
-
# @param loaders [Array] Array of all loaders defined in serialize class
|
154
|
-
# @param many [Boolean] Option :many, defined on attribute
|
155
|
-
#
|
156
|
-
# @return [void]
|
157
|
-
def initialize(opts, loaders, many)
|
158
|
-
@opts = opts
|
159
|
-
@loaders = loaders
|
160
|
-
@many = many
|
161
|
-
end
|
162
|
-
|
163
|
-
# Returns proc that will be used to batch load registered keys values
|
164
|
-
# @return [#call] batch loader
|
165
|
-
def loader
|
166
|
-
@batch_loader ||= begin
|
167
|
-
loader = opts[:loader]
|
168
|
-
loader = loaders.fetch(loader) if loader.is_a?(Symbol)
|
169
|
-
loader
|
170
|
-
end
|
171
|
-
end
|
172
|
-
|
173
|
-
# Returns proc that will be used to find batch_key for current attribute.
|
174
|
-
# @return [Object] key (uid) of batch loaded object
|
175
|
-
def key
|
176
|
-
@batch_key ||= begin
|
177
|
-
key = opts[:key]
|
178
|
-
key.is_a?(Symbol) ? proc { |object| object.public_send(key) } : key
|
179
|
-
end
|
180
|
-
end
|
181
|
-
|
182
|
-
# Returns default value to use if batch loader does not return value for some key
|
183
|
-
# @return [Object] default value for missing key
|
184
|
-
def default_value
|
185
|
-
if opts.key?(:default)
|
186
|
-
opts[:default]
|
187
|
-
elsif many
|
188
|
-
FROZEN_EMPTY_ARRAY
|
189
|
-
end
|
190
|
-
end
|
191
|
-
end
|
192
|
-
|
193
149
|
#
|
194
150
|
# Config class additional/patched instance methods
|
195
151
|
#
|
@@ -263,16 +219,16 @@ class Serega
|
|
263
219
|
#
|
264
220
|
module MapPointInstanceMethods
|
265
221
|
#
|
266
|
-
# Returns
|
222
|
+
# Returns BatchOptionModel, an object that combines options and methods needed to load batch
|
267
223
|
#
|
268
|
-
# @return [
|
224
|
+
# @return [BatchOptionModel] Class that combines options and methods needed to load batch.
|
269
225
|
#
|
270
226
|
def batch
|
271
227
|
return @batch if instance_variable_defined?(:@batch)
|
272
228
|
|
273
229
|
@batch = begin
|
274
230
|
opts = attribute.batch
|
275
|
-
|
231
|
+
BatchOptionModel.new(attribute) if opts
|
276
232
|
end
|
277
233
|
end
|
278
234
|
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Serega
|
4
|
+
module SeregaPlugins
|
5
|
+
module Batch
|
6
|
+
#
|
7
|
+
# Combines options and methods needed to load batch for specific attribute
|
8
|
+
#
|
9
|
+
class BatchOptionModel
|
10
|
+
attr_reader :attribute, :opts, :loaders, :many
|
11
|
+
|
12
|
+
#
|
13
|
+
# Initializes BatchOptionModel
|
14
|
+
#
|
15
|
+
# @param map_point [Serega::SeregaMapPoint] Map point for attribute with :batch option
|
16
|
+
# @param loaders [Array] Array of all loaders defined in serialize class
|
17
|
+
# @param many [Boolean] Option :many, defined on attribute
|
18
|
+
#
|
19
|
+
# @return [void]
|
20
|
+
def initialize(attribute)
|
21
|
+
@attribute = attribute
|
22
|
+
@opts = attribute.batch
|
23
|
+
@loaders = attribute.class.serializer_class.config.batch_loaders
|
24
|
+
@many = attribute.many
|
25
|
+
end
|
26
|
+
|
27
|
+
# Returns proc that will be used to batch load registered keys values
|
28
|
+
# @return [#call] batch loader
|
29
|
+
def loader
|
30
|
+
@batch_loader ||= begin
|
31
|
+
loader = opts[:loader]
|
32
|
+
loader = loaders.fetch(loader) if loader.is_a?(Symbol)
|
33
|
+
loader
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# Returns proc that will be used to find batch_key for current attribute.
|
38
|
+
# @return [Object] key (uid) of batch loaded object
|
39
|
+
def key
|
40
|
+
@batch_key ||= begin
|
41
|
+
key = opts[:key]
|
42
|
+
|
43
|
+
if key.is_a?(Symbol)
|
44
|
+
proc do |object|
|
45
|
+
handle_no_method_error { object.public_send(key) }
|
46
|
+
end
|
47
|
+
else
|
48
|
+
key
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
# Returns default value to use if batch loader does not return value for some key
|
54
|
+
# @return [Object] default value for missing key
|
55
|
+
def default_value
|
56
|
+
if opts.key?(:default)
|
57
|
+
opts[:default]
|
58
|
+
elsif many
|
59
|
+
FROZEN_EMPTY_ARRAY
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def handle_no_method_error
|
66
|
+
yield
|
67
|
+
rescue NoMethodError => error
|
68
|
+
raise error, "NoMethodError when serializing '#{attribute.name}' attribute in #{attribute.class.serializer_class}\n\n#{error.message}", error.backtrace
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Serega
|
4
|
+
module SeregaPlugins
|
5
|
+
module Batch
|
6
|
+
#
|
7
|
+
# Extensions (mini-plugins) that are enabled when :batch plugin used with other plugins
|
8
|
+
#
|
9
|
+
module PluginsExtensions
|
10
|
+
#
|
11
|
+
# Extension that is used when :batch plugin used with :active_record_preloads plugin
|
12
|
+
#
|
13
|
+
module ActiveRecordPreloads
|
14
|
+
#
|
15
|
+
# BatchLoader additional/patched instance methods
|
16
|
+
#
|
17
|
+
# @see Serega::SeregaPlugins::Batch::SeregaBatchLoader
|
18
|
+
#
|
19
|
+
module BatchLoaderInstanceMethods
|
20
|
+
private
|
21
|
+
|
22
|
+
# Preloads required associations to batch-loaded records
|
23
|
+
def keys_values(*)
|
24
|
+
data = super
|
25
|
+
|
26
|
+
if point.has_nested_points?
|
27
|
+
associations = point.preloads
|
28
|
+
return data if associations.empty?
|
29
|
+
|
30
|
+
records = data.values
|
31
|
+
records.flatten!(1)
|
32
|
+
|
33
|
+
ActiverecordPreloads::Preloader.preload(records, associations)
|
34
|
+
end
|
35
|
+
|
36
|
+
data
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Serega
|
4
|
+
module SeregaPlugins
|
5
|
+
module Batch
|
6
|
+
#
|
7
|
+
# Extensions (mini-plugins) that are enabled when :batch plugin used with other plugins
|
8
|
+
#
|
9
|
+
module PluginsExtensions
|
10
|
+
#
|
11
|
+
# Extension that is used when batch plugin used with :formatters plugin
|
12
|
+
#
|
13
|
+
module Formatters
|
14
|
+
#
|
15
|
+
# BatchLoader additional/patched instance methods
|
16
|
+
#
|
17
|
+
# @see Serega::SeregaPlugins::Batch::SeregaBatchLoader
|
18
|
+
#
|
19
|
+
module BatchLoaderInstanceMethods
|
20
|
+
private
|
21
|
+
|
22
|
+
# Format values after they are prepared
|
23
|
+
def keys_values(*)
|
24
|
+
data = super
|
25
|
+
|
26
|
+
formatter = point.attribute.formatter
|
27
|
+
data.transform_values! { |value| formatter.call(value) } if formatter
|
28
|
+
|
29
|
+
data
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Serega
|
4
|
+
module SeregaPlugins
|
5
|
+
module Batch
|
6
|
+
#
|
7
|
+
# Extensions (mini-plugins) that are enabled when :batch plugin used with other plugins
|
8
|
+
#
|
9
|
+
module PluginsExtensions
|
10
|
+
#
|
11
|
+
# Extension that is used when :preloads plugin is loaded
|
12
|
+
#
|
13
|
+
module Preloads
|
14
|
+
#
|
15
|
+
# Attribute additional/patched instance methods
|
16
|
+
#
|
17
|
+
# @see Serega::SeregaPlugins::Preloads::AttributeInstanceMethods
|
18
|
+
#
|
19
|
+
module AttributeInstanceMethods
|
20
|
+
private
|
21
|
+
|
22
|
+
# Do not add any preloads automatically when batch option provided
|
23
|
+
def get_preloads
|
24
|
+
return if batch && !opts.key?(:preload)
|
25
|
+
super
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -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.
|
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
|
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 =
|
198
|
+
def call(object, opts = nil)
|
199
|
+
opts ||= FROZEN_EMPTY_HASH
|
198
200
|
initiate_keys = config.initiate_keys
|
199
|
-
|
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 =
|
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 =
|
222
|
-
|
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 =
|
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 =
|
259
|
-
@opts = (opts
|
260
|
-
self.class::CheckInitiateParams.new(@opts).validate if opts
|
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.
|
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:
|
11
|
+
date: 2023-02-14 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |
|
14
14
|
JSON Serializer
|
@@ -42,9 +42,12 @@ files:
|
|
42
42
|
- lib/serega/plugins/activerecord_preloads/activerecord_preloads.rb
|
43
43
|
- lib/serega/plugins/activerecord_preloads/lib/preloader.rb
|
44
44
|
- lib/serega/plugins/batch/batch.rb
|
45
|
+
- lib/serega/plugins/batch/lib/batch_option_model.rb
|
45
46
|
- lib/serega/plugins/batch/lib/loader.rb
|
46
47
|
- lib/serega/plugins/batch/lib/loaders.rb
|
47
|
-
- lib/serega/plugins/batch/lib/plugins_extensions.rb
|
48
|
+
- lib/serega/plugins/batch/lib/plugins_extensions/activerecord_preloads.rb
|
49
|
+
- lib/serega/plugins/batch/lib/plugins_extensions/formatters.rb
|
50
|
+
- lib/serega/plugins/batch/lib/plugins_extensions/preloads.rb
|
48
51
|
- lib/serega/plugins/batch/lib/validations/check_batch_opt_key.rb
|
49
52
|
- lib/serega/plugins/batch/lib/validations/check_batch_opt_loader.rb
|
50
53
|
- lib/serega/plugins/batch/lib/validations/check_opt_batch.rb
|
@@ -70,6 +73,7 @@ files:
|
|
70
73
|
- lib/serega/plugins/string_modifiers/parse_string_modifiers.rb
|
71
74
|
- lib/serega/plugins/string_modifiers/string_modifiers.rb
|
72
75
|
- lib/serega/utils/enum_deep_dup.rb
|
76
|
+
- lib/serega/utils/symbol_name.rb
|
73
77
|
- lib/serega/utils/to_hash.rb
|
74
78
|
- lib/serega/validations/attribute/check_block.rb
|
75
79
|
- lib/serega/validations/attribute/check_name.rb
|
@@ -111,7 +115,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
111
115
|
- !ruby/object:Gem::Version
|
112
116
|
version: '0'
|
113
117
|
requirements: []
|
114
|
-
rubygems_version: 3.
|
118
|
+
rubygems_version: 3.4.6
|
115
119
|
signing_key:
|
116
120
|
specification_version: 4
|
117
121
|
summary: JSON Serializer
|
@@ -1,62 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class Serega
|
4
|
-
module SeregaPlugins
|
5
|
-
module Batch
|
6
|
-
# Extensions (mini-plugins) that are enabled when Batch plugin used with other plugins
|
7
|
-
module PluginsExtensions
|
8
|
-
#
|
9
|
-
# Extension that is used when batch plugin used with :active_record_preloads plugin
|
10
|
-
#
|
11
|
-
module ActiveRecordPreloads
|
12
|
-
#
|
13
|
-
# BatchLoader additional/patched instance methods
|
14
|
-
#
|
15
|
-
# @see Serega::SeregaPlugins::Batch::SeregaBatchLoader
|
16
|
-
#
|
17
|
-
module BatchLoaderInstanceMethods
|
18
|
-
private
|
19
|
-
|
20
|
-
# Preloads required associations to batch-loaded records
|
21
|
-
def keys_values(*)
|
22
|
-
data = super
|
23
|
-
|
24
|
-
if point.has_nested_points?
|
25
|
-
associations = point.preloads
|
26
|
-
return data if associations.empty?
|
27
|
-
|
28
|
-
ActiverecordPreloads::Preloader.preload(data.values.flatten(1), associations)
|
29
|
-
end
|
30
|
-
|
31
|
-
data
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
#
|
37
|
-
# Extension that is used when batch plugin used with :formatters plugin
|
38
|
-
#
|
39
|
-
module Formatters
|
40
|
-
#
|
41
|
-
# BatchLoader additional/patched instance methods
|
42
|
-
#
|
43
|
-
# @see Serega::SeregaPlugins::Batch::SeregaBatchLoader
|
44
|
-
#
|
45
|
-
module BatchLoaderInstanceMethods
|
46
|
-
private
|
47
|
-
|
48
|
-
# Format values after they are prepared
|
49
|
-
def keys_values(*)
|
50
|
-
data = super
|
51
|
-
|
52
|
-
formatter = point.attribute.formatter
|
53
|
-
data.transform_values! { |value| formatter.call(value) } if formatter
|
54
|
-
|
55
|
-
data
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|