store_model 2.4.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 738610a630b9d60da6e855965f5f2ecfee4bb1acd68d89b8f012645f62930591
4
- data.tar.gz: 512a83d32803641b177f9bd385f7c543595492851e96f296f7257fcc6ac8275e
3
+ metadata.gz: 79f3b084f46c6eaf4add97a2c0cf23356524f4e5c08c8387535561231453cb4c
4
+ data.tar.gz: 9471a0a8db98416257030134e1bebadb661a6bf8d2d1495c5232c09029884daf
5
5
  SHA512:
6
- metadata.gz: 4ef1e9fab0a057edc2322593fb17595e4517c1c10c87b726c02f84deb1aabbd73a37949c885987cf9f342892d2df17675b4e18839a82f0d6153c57797930acb8
7
- data.tar.gz: 150c9d6dc291512309046c9049316c3ad9f2784e0989751ae406dcc6924a43973b88c2c606a8645c6a25d9243816d7f0dc7470ffbe02512837db4862f783b988
6
+ metadata.gz: 48c10f69a844c7adc77346b3cad3f27d15c53fd9281a7953213f7c7c92c0baad25b62c1a6f469fab7056ace9c7856688818d997d9a684b147935580d7e2972a5
7
+ data.tar.gz: 60c00607715329c2e34bf101eba4c014a6cd29f564323d38bcd74921349996b441048c590d48c023eb6d2a4f254e1015526034bab7a10b5eb11a6524876284aa
@@ -27,6 +27,7 @@ module StoreModel
27
27
  def initialize
28
28
  @serialize_unknown_attributes = true
29
29
  @enable_parent_assignment = true
30
+ @serialize_enums_using_as_json = true
30
31
  end
31
32
  end
32
33
  end
@@ -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
- values[key] = serialized_attribute(key)
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(attr_name)
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.as_json
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, serialize_unknown_attributes: true)
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, Hash
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
- ActiveSupport::JSON.encode(value, serialize_unknown_attributes: true)
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StoreModel # :nodoc:
4
- VERSION = "2.4.0"
4
+ VERSION = "3.0.0"
5
5
  end
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: 2.4.0
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-04-15 00:00:00.000000000 Z
11
+ date: 2024-06-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord