cached_serializer 0.1.0 → 0.1.2
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bf526cbee1a252e2821fab724ba9a1edd00aa3c85beeb52846b9cc25dc2648de
|
4
|
+
data.tar.gz: 263aad0b673d71ff631f6d0cbe0ae27db278009df9ae8b3835943847d523bd9a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '059c0ebf3794f4de80a3a3b6c66a54617e96350002951730fd867f7668ac4a27f17e0a91368c8e34beb80a0afdddf1bda71f3d698bc1a7785e27e9bfb0c3f6b3'
|
7
|
+
data.tar.gz: 38f0b76d94df50ce19f56a326c34fe3ad757ac0f80d1f39ebb68bd881622ead577690cc6da32fea21e8f2473551c5ce1924653ce3360adcfb27820bfb6afe9ec
|
@@ -8,7 +8,7 @@ module CachedSerializer
|
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
|
-
def initialize(attr_name, recompute_ifs = nil,
|
11
|
+
def initialize(attr_name, recompute_ifs = nil, expires_in = nil, &recompute)
|
12
12
|
self.attr_name = attr_name
|
13
13
|
self.recompute_ifs = [recompute_ifs].flatten.compact
|
14
14
|
self.expires_in = expires_in
|
@@ -25,7 +25,7 @@ module CachedSerializer
|
|
25
25
|
should_recompute = recompute_ifs.any? { |recompute_if| recompute_if.call(subject) }
|
26
26
|
cache_key = self.class.cache_key(subject, attr_name)
|
27
27
|
Rails.cache.fetch(cache_key, expires_in: expires_in, force: should_recompute) do
|
28
|
-
|
28
|
+
recompute.call(subject)
|
29
29
|
end
|
30
30
|
end
|
31
31
|
end
|
@@ -14,7 +14,7 @@ module CachedSerializer
|
|
14
14
|
existing.recompute = attr_serializer.recompute # shadowed attrs override the blocks of previously-declared ones
|
15
15
|
existing.expires_in = attr_serializer.expires_in # shadowed attrs override the expires_in of previously-declared ones
|
16
16
|
else
|
17
|
-
|
17
|
+
collection << attr_serializer
|
18
18
|
end
|
19
19
|
end
|
20
20
|
end
|
@@ -6,11 +6,9 @@ module CachedSerializer
|
|
6
6
|
class Base
|
7
7
|
attr_accessor :subject
|
8
8
|
|
9
|
-
@serializers = AttrSerializerCollection.new
|
10
|
-
|
11
9
|
class << self
|
12
10
|
def serializers
|
13
|
-
@serializers
|
11
|
+
@serializers ||= AttrSerializerCollection.new
|
14
12
|
end
|
15
13
|
|
16
14
|
# Example (in a UserSerializer):
|
@@ -121,7 +119,7 @@ module CachedSerializer
|
|
121
119
|
private
|
122
120
|
|
123
121
|
def subject_class
|
124
|
-
@subject_class ||= self.
|
122
|
+
@subject_class ||= self.to_s.gsub(/[Ss]erializer\z/, '').constantize
|
125
123
|
end
|
126
124
|
|
127
125
|
def add_column_changed_cache_invalidator_callback(attr_name, dependent_attr_name)
|
@@ -129,10 +127,10 @@ module CachedSerializer
|
|
129
127
|
@already_added_callback[attr_name.to_sym] ||= {}
|
130
128
|
return if @already_added_callback[attr_name.to_sym][dependent_attr_name.to_sym]
|
131
129
|
|
132
|
-
subject_class.
|
133
|
-
|
130
|
+
subject_class.instance_eval do
|
131
|
+
after_save do
|
134
132
|
if changes[dependent_attr_name.to_s]
|
135
|
-
Rails.cache.delete(CachedSerializer::AttrSerializer.cache_key(
|
133
|
+
Rails.cache.delete(CachedSerializer::AttrSerializer.cache_key(self, attr_name))
|
136
134
|
end
|
137
135
|
end
|
138
136
|
end
|