store_model 0.13.0 → 1.2.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: f02467ba044729f1a4196b520ae35b90157cd09b8de8a41db53673f74bd24481
4
- data.tar.gz: f893737770e749558b124154c81d3400b2f7a6defcfccee7164ad7fc667ba797
3
+ metadata.gz: 6139e784c55ab957dc9de25a43b4727c91333dde4b7c92f4f22ea2634c204bf5
4
+ data.tar.gz: 04ae362612c4032445da8b05320e0d95f29a4b825bba751fa72811aabae263bd
5
5
  SHA512:
6
- metadata.gz: 8b3357e13f694420ebc0e58018fe247ed28cf7cab32345b24208c49751e119561a4413f11bb39cf80bab99f54cbfbd818dbcd54a4f6e801c16dd9cf22f7c5392
7
- data.tar.gz: 9679bf7228ac2156f6704a4ac457db2d39f8837d428a04607bbb9a09e07dfeaa843ff91d929e04690ef6c1b65e1d2b318b4a98cc4beaba125f48e16659b82c16
6
+ metadata.gz: fc7e96d96c1d4e8b7074b7d4000755137b3199b55769ff5e57ad59235e79cde8441e80d59133a54981788a94202df8b698484a896d4ea2d22dd90fdb3633736f
7
+ data.tar.gz: '001499ba6ff97ee59dc16fca855fbd9128fe9f6f85d4fe37438f4ea13bdea5ae81bf6da9281b8436b816573be6c79a15f68c6fdb0c3b610e7d23a897ba762b6b'
@@ -10,5 +10,13 @@ module StoreModel
10
10
  # Controls usage of MergeArrayErrorStrategy
11
11
  # @return [Boolean]
12
12
  attr_accessor :merge_array_errors
13
+
14
+ # Controls if the result of `as_json` will contain the unknown attributes of the model
15
+ # @return [Boolean]
16
+ attr_accessor :serialize_unknown_attributes
17
+
18
+ def initialize
19
+ @serialize_unknown_attributes = true
20
+ end
13
21
  end
14
22
  end
@@ -15,9 +15,17 @@ module StoreModel
15
15
  end
16
16
  end
17
17
 
18
- def write_attribute(*)
19
- super.tap do |value|
20
- assign_parent_to_store_model_relation(value)
18
+ if Rails::VERSION::MAJOR >= 7
19
+ def _write_attribute(*)
20
+ super.tap do |value|
21
+ assign_parent_to_store_model_relation(value)
22
+ end
23
+ end
24
+ else
25
+ def write_attribute(*)
26
+ super.tap do |value|
27
+ assign_parent_to_store_model_relation(value)
28
+ end
21
29
  end
22
30
  end
23
31
  end
@@ -11,6 +11,7 @@ module StoreModel
11
11
  def self.included(base) # :nodoc:
12
12
  base.include ActiveModel::Model
13
13
  base.include ActiveModel::Attributes
14
+ base.include ActiveRecord::AttributeMethods::BeforeTypeCast
14
15
  base.include ActiveModel::AttributeMethods
15
16
  base.include StoreModel::NestedAttributes
16
17
 
@@ -22,6 +23,8 @@ module StoreModel
22
23
 
23
24
  attr_accessor :parent
24
25
 
26
+ delegate :each_value, :fetch, to: :attributes
27
+
25
28
  # Returns a hash representing the model. Some configuration can be
26
29
  # passed through +options+.
27
30
  #
@@ -29,7 +32,15 @@ module StoreModel
29
32
  #
30
33
  # @return [Hash]
31
34
  def as_json(options = {})
32
- attributes.with_indifferent_access.merge(unknown_attributes).as_json(options)
35
+ serialize_unknown_attributes = if options.key?(:serialize_unknown_attributes)
36
+ options[:serialize_unknown_attributes]
37
+ else
38
+ StoreModel.config.serialize_unknown_attributes
39
+ end
40
+
41
+ result = attributes.with_indifferent_access
42
+ result.merge!(unknown_attributes) if serialize_unknown_attributes
43
+ result.as_json(options)
33
44
  end
34
45
 
35
46
  # Compares two StoreModel::Model instances
@@ -42,6 +53,24 @@ module StoreModel
42
53
 
43
54
  attributes.all? { |name, value| value == other.attributes[name] }
44
55
  end
56
+ alias eql? ==
57
+
58
+ # Accessing attribute using brackets
59
+ #
60
+ # @return [Object]
61
+ def [](attr_name)
62
+ @attributes.fetch_value(attr_name.to_s)
63
+ end
64
+
65
+ # Setting attribute using brackets
66
+ #
67
+ # @param name [String, Symbol]
68
+ # @param value [Object]
69
+ #
70
+ # @return [Object]
71
+ def []=(attr_name, value)
72
+ @attributes.write_from_user(attr_name.to_s, value)
73
+ end
45
74
 
46
75
  # Returns hash for a StoreModel::Model instance based on attributes hash
47
76
  #
@@ -38,7 +38,7 @@ module StoreModel
38
38
  def serialize(value)
39
39
  case value
40
40
  when Array
41
- ActiveSupport::JSON.encode(value)
41
+ ActiveSupport::JSON.encode(value, serialize_unknown_attributes: true)
42
42
  else
43
43
  super
44
44
  end
@@ -47,7 +47,7 @@ module StoreModel
47
47
  def serialize(value)
48
48
  case value
49
49
  when Hash, @model_klass
50
- ActiveSupport::JSON.encode(value)
50
+ ActiveSupport::JSON.encode(value, serialize_unknown_attributes: true)
51
51
  else
52
52
  super
53
53
  end
@@ -52,9 +52,11 @@ module StoreModel
52
52
  def serialize(value)
53
53
  case value
54
54
  when Hash
55
- ActiveSupport::JSON.encode(value)
55
+ ActiveSupport::JSON.encode(value, serialize_unknown_attributes: true)
56
56
  else
57
- return ActiveSupport::JSON.encode(value) if implements_model?(value.class)
57
+ if implements_model?(value.class)
58
+ return ActiveSupport::JSON.encode(value, serialize_unknown_attributes: true)
59
+ end
58
60
 
59
61
  super
60
62
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StoreModel # :nodoc:
4
- VERSION = "0.13.0"
4
+ VERSION = "1.2.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: 0.13.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - DmitryTsepelev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-02-11 00:00:00.000000000 Z
11
+ date: 2022-08-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -135,7 +135,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
135
135
  - !ruby/object:Gem::Version
136
136
  version: '0'
137
137
  requirements: []
138
- rubygems_version: 3.0.3.1
138
+ rubygems_version: 3.1.6
139
139
  signing_key:
140
140
  specification_version: 4
141
141
  summary: Gem for working with JSON-backed attributes as ActiveRecord models