store_attribute 0.7.1 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +1 -0
- data/lib/store_attribute/active_record/store.rb +70 -22
- data/lib/store_attribute/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a6d0060f9544a3e9e005c149cc189dd6647c0c83493bbb577bb67893e2b5a5fd
|
4
|
+
data.tar.gz: 0f9105dc411953cf7b3342117538e068c9ea7897220d573126bf0d4c01640726
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0521c55632478bc52bfcb03eab0fb9709ec2e63cc59c9eafa1dc3795742c88d7f2caf7019bb3eac3f3232006f49e0c9dc0cec4bcf619154b7f1873ccd7915201
|
7
|
+
data.tar.gz: e6644235407bd7481499dbb4bfc3fb9214ec0805ebf85be79914ddf8179f7b2f9837db1f33760c8af50236608820e0dc976fad74b3f9046e70f5996c86ec251c
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
[![Cult Of Martians](http://cultofmartians.com/assets/badges/badge.svg)](https://cultofmartians.com/tasks/store-attribute-defaults.html#task)
|
1
2
|
[![Gem Version](https://badge.fury.io/rb/store_attribute.svg)](https://rubygems.org/gems/store_attribute) [![Build Status](https://travis-ci.org/palkan/store_attribute.svg?branch=master)](https://travis-ci.org/palkan/store_attribute)
|
2
3
|
|
3
4
|
## Store Attribute
|
@@ -44,24 +44,29 @@ module ActiveRecord
|
|
44
44
|
#
|
45
45
|
# +typed_keys+ The key-to-type hash of the accesors with type to the store.
|
46
46
|
#
|
47
|
+
# +prefix+ Accessor method name prefix
|
48
|
+
#
|
49
|
+
# +suffix+ Accessor method name suffix
|
50
|
+
#
|
47
51
|
# Examples:
|
48
52
|
#
|
49
53
|
# class SuperUser < User
|
50
54
|
# store_accessor :settings, :privileges, login_at: :datetime
|
51
55
|
# end
|
52
|
-
def store_accessor(store_name, *keys, **typed_keys)
|
56
|
+
def store_accessor(store_name, *keys, prefix: nil, suffix: nil, **typed_keys)
|
53
57
|
keys = keys.flatten
|
54
58
|
typed_keys = typed_keys.except(keys)
|
55
59
|
|
56
|
-
|
60
|
+
accessor_prefix, accessor_suffix = _normalize_prefix_suffix(store_name, prefix, suffix)
|
61
|
+
|
62
|
+
_define_accessors_methods(store_name, *keys, prefix: accessor_prefix, suffix: accessor_suffix)
|
57
63
|
|
58
|
-
_define_dirty_tracking_methods(store_name, keys)
|
59
|
-
_define_dirty_tracking_methods(store_name, typed_keys.keys)
|
64
|
+
_define_dirty_tracking_methods(store_name, keys + typed_keys.keys, prefix: accessor_prefix, suffix: accessor_suffix)
|
60
65
|
|
61
66
|
_prepare_local_stored_attributes(store_name, *keys)
|
62
67
|
|
63
68
|
typed_keys.each do |key, type|
|
64
|
-
store_attribute(store_name, key, type)
|
69
|
+
store_attribute(store_name, key, type, prefix: prefix, suffix: suffix)
|
65
70
|
end
|
66
71
|
end
|
67
72
|
|
@@ -78,6 +83,10 @@ module ActiveRecord
|
|
78
83
|
# +type+ A symbol such as +:string+ or +:integer+, or a type object
|
79
84
|
# to be used for the accessor.
|
80
85
|
#
|
86
|
+
# +prefix+ Accessor method name prefix
|
87
|
+
#
|
88
|
+
# +suffix+ Accessor method name suffix
|
89
|
+
#
|
81
90
|
# +options+ A hash of cast type options such as +precision+, +limit+, +scale+.
|
82
91
|
#
|
83
92
|
# Examples:
|
@@ -85,13 +94,16 @@ module ActiveRecord
|
|
85
94
|
# class MegaUser < User
|
86
95
|
# store_attribute :settings, :ratio, :integer, limit: 1
|
87
96
|
# store_attribute :settings, :login_at, :datetime
|
97
|
+
#
|
98
|
+
# store_attribute :extra, :version, :integer, prefix: :meta
|
88
99
|
# end
|
89
100
|
#
|
90
|
-
# u = MegaUser.new(active: false, login_at: '2015-01-01 00:01', ratio: "63.4608")
|
101
|
+
# u = MegaUser.new(active: false, login_at: '2015-01-01 00:01', ratio: "63.4608", meta_version: "1")
|
91
102
|
#
|
92
103
|
# u.login_at.is_a?(DateTime) # => true
|
93
104
|
# u.login_at = DateTime.new(2015,1,1,11,0,0)
|
94
105
|
# u.ratio # => 63
|
106
|
+
# u.meta_version #=> 1
|
95
107
|
# u.reload
|
96
108
|
#
|
97
109
|
# # After loading record from db store contains casted data
|
@@ -108,13 +120,24 @@ module ActiveRecord
|
|
108
120
|
# u.settings['ratio'] # => 3
|
109
121
|
#
|
110
122
|
# For more examples on using types, see documentation for ActiveRecord::Attributes.
|
111
|
-
def store_attribute(store_name, name, type, **options)
|
112
|
-
|
123
|
+
def store_attribute(store_name, name, type, prefix: nil, suffix: nil, **options)
|
124
|
+
prefix, suffix = _normalize_prefix_suffix(store_name, prefix, suffix)
|
125
|
+
|
126
|
+
_define_accessors_methods(store_name, name, prefix: prefix, suffix: suffix)
|
113
127
|
|
114
|
-
_define_predicate_method(name) if type == :boolean
|
128
|
+
_define_predicate_method(name, prefix: prefix, suffix: suffix) if type == :boolean
|
115
129
|
|
116
|
-
|
117
|
-
|
130
|
+
# Rails >6.0
|
131
|
+
if method(:decorate_attribute_type).parameters.count { |type, _| type == :req } == 1
|
132
|
+
attr_name = store_name.to_s
|
133
|
+
was_type = attributes_to_define_after_schema_loads[attr_name]&.first
|
134
|
+
attribute(attr_name) do |subtype|
|
135
|
+
Type::TypedStore.create_from_type(_lookup_cast_type(attr_name, was_type, {}), name, type, **options)
|
136
|
+
end
|
137
|
+
else
|
138
|
+
decorate_attribute_type(store_name, "typed_accessor_for_#{name}") do |subtype|
|
139
|
+
Type::TypedStore.create_from_type(subtype, name, type, **options)
|
140
|
+
end
|
118
141
|
end
|
119
142
|
|
120
143
|
_prepare_local_stored_attributes(store_name, name)
|
@@ -128,64 +151,69 @@ module ActiveRecord
|
|
128
151
|
self.local_stored_attributes[store_name] |= keys
|
129
152
|
end
|
130
153
|
|
131
|
-
def _define_accessors_methods(store_name, *keys) # :nodoc:
|
154
|
+
def _define_accessors_methods(store_name, *keys, prefix: nil, suffix: nil) # :nodoc:
|
132
155
|
_store_accessors_module.module_eval do
|
133
156
|
keys.each do |key|
|
134
|
-
|
157
|
+
accessor_key = "#{prefix}#{key}#{suffix}"
|
158
|
+
|
159
|
+
define_method("#{accessor_key}=") do |value|
|
135
160
|
write_store_attribute(store_name, key, value)
|
136
161
|
end
|
137
162
|
|
138
|
-
define_method(
|
163
|
+
define_method(accessor_key) do
|
139
164
|
read_store_attribute(store_name, key)
|
140
165
|
end
|
141
166
|
end
|
142
167
|
end
|
143
168
|
end
|
144
169
|
|
145
|
-
def _define_predicate_method(name)
|
170
|
+
def _define_predicate_method(name, prefix: nil, suffix: nil)
|
146
171
|
_store_accessors_module.module_eval do
|
172
|
+
name = "#{prefix}#{name}#{suffix}"
|
173
|
+
|
147
174
|
define_method("#{name}?") do
|
148
175
|
send(name) == true
|
149
176
|
end
|
150
177
|
end
|
151
178
|
end
|
152
179
|
|
153
|
-
def _define_dirty_tracking_methods(store_attribute, keys)
|
180
|
+
def _define_dirty_tracking_methods(store_attribute, keys, prefix: nil, suffix: nil)
|
154
181
|
_store_accessors_module.module_eval do
|
155
182
|
keys.flatten.each do |key|
|
156
183
|
key = key.to_s
|
184
|
+
accessor_key = "#{prefix}#{key}#{suffix}"
|
157
185
|
|
158
|
-
define_method("#{
|
186
|
+
define_method("#{accessor_key}_changed?") do
|
159
187
|
return false unless attribute_changed?(store_attribute)
|
160
188
|
prev_store, new_store = changes[store_attribute]
|
161
189
|
prev_store&.dig(key) != new_store&.dig(key)
|
162
190
|
end
|
163
191
|
|
164
|
-
define_method("#{
|
192
|
+
define_method("#{accessor_key}_change") do
|
165
193
|
return unless attribute_changed?(store_attribute)
|
166
194
|
prev_store, new_store = changes[store_attribute]
|
167
195
|
[prev_store&.dig(key), new_store&.dig(key)]
|
168
196
|
end
|
169
197
|
|
170
|
-
define_method("#{
|
198
|
+
define_method("#{accessor_key}_was") do
|
171
199
|
return unless attribute_changed?(store_attribute)
|
172
200
|
prev_store, _new_store = changes[store_attribute]
|
173
201
|
prev_store&.dig(key)
|
174
202
|
end
|
175
203
|
|
176
|
-
define_method("saved_change_to_#{
|
204
|
+
define_method("saved_change_to_#{accessor_key}?") do
|
177
205
|
return false unless saved_change_to_attribute?(store_attribute)
|
178
206
|
prev_store, new_store = saved_change_to_attribute(store_attribute)
|
179
207
|
prev_store&.dig(key) != new_store&.dig(key)
|
180
208
|
end
|
181
209
|
|
182
|
-
define_method("saved_change_to_#{
|
210
|
+
define_method("saved_change_to_#{accessor_key}") do
|
183
211
|
return unless saved_change_to_attribute?(store_attribute)
|
184
212
|
prev_store, new_store = saved_change_to_attribute(store_attribute)
|
185
213
|
[prev_store&.dig(key), new_store&.dig(key)]
|
186
214
|
end
|
187
215
|
|
188
|
-
define_method("#{
|
216
|
+
define_method("#{accessor_key}_before_last_save") do
|
189
217
|
return unless saved_change_to_attribute?(store_attribute)
|
190
218
|
prev_store, _new_store = saved_change_to_attribute(store_attribute)
|
191
219
|
prev_store&.dig(key)
|
@@ -193,6 +221,26 @@ module ActiveRecord
|
|
193
221
|
end
|
194
222
|
end
|
195
223
|
end
|
224
|
+
|
225
|
+
def _normalize_prefix_suffix(store_name, prefix, suffix)
|
226
|
+
prefix =
|
227
|
+
case prefix
|
228
|
+
when String, Symbol
|
229
|
+
"#{prefix}_"
|
230
|
+
when TrueClass
|
231
|
+
"#{store_name}_"
|
232
|
+
end
|
233
|
+
|
234
|
+
suffix =
|
235
|
+
case suffix
|
236
|
+
when String, Symbol
|
237
|
+
"_#{suffix}"
|
238
|
+
when TrueClass
|
239
|
+
"_#{store_name}"
|
240
|
+
end
|
241
|
+
|
242
|
+
[prefix, suffix]
|
243
|
+
end
|
196
244
|
end
|
197
245
|
end
|
198
246
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: store_attribute
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- palkan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-09-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|