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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bf08bdba63148d597aa5e6715f84f653e08fe9f5
4
- data.tar.gz: 38f161d7f1a5b3aa48177c26eb449de3b7f752bd
3
+ metadata.gz: aa974f5c3893ad1f5dea40c681fce0a0a9c74106
4
+ data.tar.gz: e6ce3ed9689ee1d5111d992b16da1a47249d07ec
5
5
  SHA512:
6
- metadata.gz: 58ad5075998f12a353e940eacfdd413b2e37645fa8a929803057bc2855eaa172179fa7a1771a27b150e60b203dea331ddcf229fb303bb8271231c08a13997614
7
- data.tar.gz: b51695d413eeae6c9e723560b742801b9ccd7223366884970ad8344710c97e80393b9a49527d26e9b43b37cb5b24d881814293063d3a66a8ad03a0d90e847c4a
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
- model.class_eval <<-RUBY
16
- def #{name}_shadow_cache_key
17
- "#{model_name}_\#{self[self.class.primary_key]}_#{name}_shadow_cache"
18
- end
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
- def #{name}_by_shadow
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
- def clear_#{name}_shadow_cache
28
- Redis.current.del(#{name}_shadow_cache_key)
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
- RUBY
35
+ end
31
36
 
32
- reflection.klass.class_eval <<-RUBY
33
- after_save :update_shadow_cache_of_#{model_name}
34
- after_destroy :delete_shadow_cache_of_#{model_name}
37
+ def clear_#{name}_shadow_cache
38
+ Redis.current.del(#{name}_shadow_cache_key)
39
+ end
40
+ RUBY
41
+ end
35
42
 
36
- def belongs_to_#{model_name}_shadow_cache_key
37
- "#{model_name}_\#{self[:#{reflection.foreign_key}]}_#{name}_shadow_cache"
38
- end
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
- def update_shadow_cache_of_#{model_name}
41
- cache_key = belongs_to_#{model_name}_shadow_cache_key
42
- Redis.current.hset(cache_key, self[self.class.primary_key], self.build_shadow_data)
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
- def delete_shadow_cache_of_#{model_name}
47
- Redis.current.hdel(belongs_to_#{model_name}_shadow_cache_key, self[self.class.primary_key])
48
- end
49
- RUBY
50
- end
51
- reflection
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
@@ -1,3 +1,3 @@
1
1
  module ShadowModel
2
- VERSION = "0.2.6"
2
+ VERSION = "0.2.7"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shadow_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.6
4
+ version: 0.2.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Weihu Chen