store_model 2.4.0 → 3.0.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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 79f3b084f46c6eaf4add97a2c0cf23356524f4e5c08c8387535561231453cb4c
|
4
|
+
data.tar.gz: 9471a0a8db98416257030134e1bebadb661a6bf8d2d1495c5232c09029884daf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 48c10f69a844c7adc77346b3cad3f27d15c53fd9281a7953213f7c7c92c0baad25b62c1a6f469fab7056ace9c7856688818d997d9a684b147935580d7e2972a5
|
7
|
+
data.tar.gz: 60c00607715329c2e34bf101eba4c014a6cd29f564323d38bcd74921349996b441048c590d48c023eb6d2a4f254e1015526034bab7a10b5eb11a6524876284aa
|
data/lib/store_model/model.rb
CHANGED
@@ -22,6 +22,7 @@ module StoreModel
|
|
22
22
|
end
|
23
23
|
|
24
24
|
attr_accessor :parent
|
25
|
+
attr_writer :serialize_unknown_attributes, :serialize_enums_using_as_json
|
25
26
|
|
26
27
|
delegate :each_value, to: :attributes
|
27
28
|
|
@@ -45,7 +46,9 @@ module StoreModel
|
|
45
46
|
end
|
46
47
|
|
47
48
|
result = @attributes.keys.each_with_object({}) do |key, values|
|
48
|
-
|
49
|
+
attr = @attributes.fetch(key)
|
50
|
+
assign_serialization_options(attr, serialize_unknown_attributes, serialize_enums_using_as_json)
|
51
|
+
values[key] = serialized_attribute(attr)
|
49
52
|
end.with_indifferent_access
|
50
53
|
|
51
54
|
result.merge!(unknown_attributes) if serialize_unknown_attributes
|
@@ -178,6 +181,38 @@ module StoreModel
|
|
178
181
|
@unknown_attributes ||= {}
|
179
182
|
end
|
180
183
|
|
184
|
+
# Returns the value of the `@serialize_unknown_attributes` instance
|
185
|
+
# variable. In the current specification, unknown attributes must be
|
186
|
+
# persisted in the database regardless of the globally configured
|
187
|
+
# `serialize_unknown_attributes` option. Therefore, it returns the
|
188
|
+
# default value `true` if the instance variable is `nil`.
|
189
|
+
#
|
190
|
+
# This method is used to ensure that the `serialize_unknown_attributes`
|
191
|
+
# option is correctly applied to nested StoreModel::Model objects when
|
192
|
+
# the `as_json` method is called.
|
193
|
+
#
|
194
|
+
# @return [Boolean]
|
195
|
+
def serialize_unknown_attributes?
|
196
|
+
@serialize_unknown_attributes.nil? ? true : @serialize_unknown_attributes
|
197
|
+
end
|
198
|
+
|
199
|
+
# Returns the value of the `@serialize_enums_using_as_json` instance
|
200
|
+
# variable. The default value is the value of the globally configured
|
201
|
+
# `serialize_enums_using_as_json` option.
|
202
|
+
#
|
203
|
+
# This method is used to determine whether enums should be serialized
|
204
|
+
# when the `as_json` method is called in nested StoreModel::Model
|
205
|
+
# objects.
|
206
|
+
#
|
207
|
+
# @return [Boolean]
|
208
|
+
def serialize_enums_using_as_json?
|
209
|
+
if @serialize_enums_using_as_json.nil?
|
210
|
+
StoreModel.config.serialize_enums_using_as_json || false
|
211
|
+
else
|
212
|
+
@serialize_enums_using_as_json
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
181
216
|
private
|
182
217
|
|
183
218
|
def attribute?(attribute)
|
@@ -200,15 +235,32 @@ module StoreModel
|
|
200
235
|
end
|
201
236
|
end
|
202
237
|
|
203
|
-
def serialized_attribute(
|
204
|
-
attr = @attributes.fetch(attr_name)
|
238
|
+
def serialized_attribute(attr)
|
205
239
|
if attr.value.is_a? StoreModel::Model
|
206
240
|
Types::RawJSONEncoder.new(attr.value_for_database)
|
207
241
|
elsif attr.value.is_a? Array
|
208
|
-
attr.value
|
242
|
+
serialize_array_attribute(attr.value)
|
209
243
|
else
|
210
244
|
attr.value_for_database
|
211
245
|
end
|
212
246
|
end
|
247
|
+
|
248
|
+
def serialize_array_attribute(array)
|
249
|
+
return array.as_json unless array.all? { |value| value.is_a?(StoreModel::Model) }
|
250
|
+
|
251
|
+
array.as_json(
|
252
|
+
serialize_unknown_attributes: array.first.serialize_unknown_attributes?,
|
253
|
+
serialize_enums_using_as_json: array.first.serialize_enums_using_as_json?
|
254
|
+
)
|
255
|
+
end
|
256
|
+
|
257
|
+
def assign_serialization_options(attr, serialize_unknown_attributes, serialize_enums_using_as_json)
|
258
|
+
return unless Array(attr.value).all? { |value| value.is_a?(StoreModel::Model) }
|
259
|
+
|
260
|
+
Array(attr.value).each do |value|
|
261
|
+
value.serialize_unknown_attributes = serialize_unknown_attributes
|
262
|
+
value.serialize_enums_using_as_json = serialize_enums_using_as_json
|
263
|
+
end
|
264
|
+
end
|
213
265
|
end
|
214
266
|
end
|
@@ -40,7 +40,11 @@ module StoreModel
|
|
40
40
|
def serialize(value)
|
41
41
|
case value
|
42
42
|
when Array
|
43
|
-
ActiveSupport::JSON.encode(value
|
43
|
+
return ActiveSupport::JSON.encode(value) unless value.all? { |v| v.is_a?(StoreModel::Model) }
|
44
|
+
|
45
|
+
ActiveSupport::JSON.encode(value,
|
46
|
+
serialize_unknown_attributes: value.first.serialize_unknown_attributes?,
|
47
|
+
serialize_enums_using_as_json: value.first.serialize_enums_using_as_json?)
|
44
48
|
else
|
45
49
|
super
|
46
50
|
end
|
@@ -46,7 +46,11 @@ module StoreModel
|
|
46
46
|
# @return [String] serialized value
|
47
47
|
def serialize(value)
|
48
48
|
case value
|
49
|
-
when @model_klass
|
49
|
+
when @model_klass
|
50
|
+
ActiveSupport::JSON.encode(value,
|
51
|
+
serialize_unknown_attributes: value.serialize_unknown_attributes?,
|
52
|
+
serialize_enums_using_as_json: value.serialize_enums_using_as_json?)
|
53
|
+
when Hash
|
50
54
|
ActiveSupport::JSON.encode(value)
|
51
55
|
else
|
52
56
|
super
|
@@ -52,7 +52,15 @@ module StoreModel
|
|
52
52
|
def serialize(value)
|
53
53
|
return super unless value.is_a?(Hash) || implements_model?(value.class)
|
54
54
|
|
55
|
-
|
55
|
+
if value.is_a?(StoreModel::Model)
|
56
|
+
ActiveSupport::JSON.encode(
|
57
|
+
value,
|
58
|
+
serialize_unknown_attributes: value.serialize_unknown_attributes?,
|
59
|
+
serialize_enums_using_as_json: value.serialize_enums_using_as_json?
|
60
|
+
)
|
61
|
+
else
|
62
|
+
ActiveSupport::JSON.encode(value)
|
63
|
+
end
|
56
64
|
end
|
57
65
|
|
58
66
|
protected
|
data/lib/store_model/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: store_model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- DmitryTsepelev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-06-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|