shadow_model 0.2.6 → 0.2.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/shadow_model/associations/has_many.rb +40 -31
- data/lib/shadow_model/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aa974f5c3893ad1f5dea40c681fce0a0a9c74106
|
4
|
+
data.tar.gz: e6ce3ed9689ee1d5111d992b16da1a47249d07ec
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 00a3a7a0ebce17e67cd58fca294399cbda1ff53b171fe14ca5b9751fba7f62a9d1d8d6290fd757a9b58915cf7143e881e2cd1d4cd39753b4bf3e2c11e98bee74
|
7
|
+
data.tar.gz: 8f338e84131c743f146728f17b034388094f711dc66adb18cf4b2323b0c9343295e8e8fda9d622ef5c205f09f219b74ff3f9e33be4b23148625fe6677cea316d
|
@@ -12,43 +12,52 @@ class ::ActiveRecord::Associations::Builder::HasMany
|
|
12
12
|
reflection = build_without_shadow_option
|
13
13
|
if options[:shadow]
|
14
14
|
model_name = model.name.tr('::', '').underscore
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
15
|
+
define_shadow_methods_of_model_class(reflection, model_name)
|
16
|
+
define_shadow_methods_of_reflection_class(reflection, model_name)
|
17
|
+
end
|
18
|
+
reflection
|
19
|
+
end
|
20
|
+
alias_method_chain :build, :shadow_option
|
19
21
|
|
20
|
-
|
21
|
-
Redis.current.hvals(#{name}_shadow_cache_key).map do |val|
|
22
|
-
shadow_data = Marshal.load(val)
|
23
|
-
#{reflection.klass.name}.instantiate_shadow_model(shadow_data)
|
24
|
-
end
|
25
|
-
end
|
22
|
+
private
|
26
23
|
|
27
|
-
|
28
|
-
|
24
|
+
def define_shadow_methods_of_model_class(reflection, model_name)
|
25
|
+
model.class_eval <<-RUBY
|
26
|
+
def #{name}_shadow_cache_key
|
27
|
+
"#{model_name}_\#{self[self.class.primary_key]}_#{name}_shadow_cache"
|
28
|
+
end
|
29
|
+
|
30
|
+
def #{name}_by_shadow
|
31
|
+
Redis.current.hvals(#{name}_shadow_cache_key).map do |val|
|
32
|
+
shadow_data = Marshal.load(val)
|
33
|
+
#{reflection.klass.name}.instantiate_shadow_model(shadow_data)
|
29
34
|
end
|
30
|
-
|
35
|
+
end
|
31
36
|
|
32
|
-
|
33
|
-
|
34
|
-
|
37
|
+
def clear_#{name}_shadow_cache
|
38
|
+
Redis.current.del(#{name}_shadow_cache_key)
|
39
|
+
end
|
40
|
+
RUBY
|
41
|
+
end
|
35
42
|
|
36
|
-
|
37
|
-
|
38
|
-
|
43
|
+
def define_shadow_methods_of_reflection_class(reflection, model_name)
|
44
|
+
reflection.klass.class_eval <<-RUBY
|
45
|
+
after_save :update_shadow_cache_of_#{model_name}
|
46
|
+
after_destroy :delete_shadow_cache_of_#{model_name}
|
39
47
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
update_expiration(cache_key)
|
44
|
-
end
|
48
|
+
def belongs_to_#{model_name}_shadow_cache_key
|
49
|
+
"#{model_name}_\#{self[:#{reflection.foreign_key}]}_#{name}_shadow_cache"
|
50
|
+
end
|
45
51
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
+
def update_shadow_cache_of_#{model_name}
|
53
|
+
cache_key = belongs_to_#{model_name}_shadow_cache_key
|
54
|
+
Redis.current.hset(cache_key, self[self.class.primary_key], self.build_shadow_data)
|
55
|
+
update_expiration(cache_key)
|
56
|
+
end
|
57
|
+
|
58
|
+
def delete_shadow_cache_of_#{model_name}
|
59
|
+
Redis.current.hdel(belongs_to_#{model_name}_shadow_cache_key, self[self.class.primary_key])
|
60
|
+
end
|
61
|
+
RUBY
|
52
62
|
end
|
53
|
-
alias_method_chain :build, :shadow_option
|
54
63
|
end
|
data/lib/shadow_model/version.rb
CHANGED