jsonb_accessor 1.3.1 → 1.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/jsonb_accessor/macro.rb +16 -7
- data/lib/jsonb_accessor/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5b446e2b5fcf294a928c055c00334951830d068f7161437ec8fcab443cf2cee5
|
4
|
+
data.tar.gz: 443355750846f163a89307f662cd06d5034b5b922e7305209e8101b879f75b3e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 98019ca7f52229b5ca88a11b29ece4919bdd308a4c101dbc3a1b53ffa9d783ab90d9c09219be7ff7de37641a4545593851ed60da69cfc57dd53dfe6b7ee04238
|
7
|
+
data.tar.gz: 0e7b87df70bc3fdd71f4e384857970863b89bf60931c623b15b0983e554875cc1581672977bb274f0104fb393052d81447e05db4759826f83255d8cf03f355bd
|
data/lib/jsonb_accessor/macro.rb
CHANGED
@@ -71,17 +71,26 @@ module JsonbAccessor
|
|
71
71
|
end
|
72
72
|
|
73
73
|
# Overrides the jsonb attribute setter to make sure the jsonb fields are kept in sync.
|
74
|
-
define_method("#{jsonb_attribute}=") do |
|
75
|
-
value
|
74
|
+
define_method("#{jsonb_attribute}=") do |value|
|
75
|
+
value ||= {}
|
76
76
|
names_to_store_keys = self.class.public_send(store_key_mapping_method_name)
|
77
|
+
store_keys_to_names = names_to_store_keys.invert
|
77
78
|
|
78
|
-
|
79
|
-
|
79
|
+
# this is the raw hash we want to save in the jsonb_attribute
|
80
|
+
value_with_store_keys = value.transform_keys do |k|
|
81
|
+
names_to_store_keys[k.to_s] || k
|
82
|
+
end
|
83
|
+
write_attribute(jsonb_attribute, value_with_store_keys)
|
80
84
|
|
81
|
-
|
82
|
-
|
85
|
+
# this maps attributes to values
|
86
|
+
value_with_named_keys = value.transform_keys do |k|
|
87
|
+
store_keys_to_names[k.to_s] || k
|
88
|
+
end
|
83
89
|
|
84
|
-
empty_named_attributes.
|
90
|
+
empty_named_attributes = names_to_store_keys.transform_values { nil }
|
91
|
+
empty_named_attributes.merge(value_with_named_keys).each do |name, attribute_value|
|
92
|
+
write_attribute(name, attribute_value)
|
93
|
+
end
|
85
94
|
end
|
86
95
|
end
|
87
96
|
include setters
|