shadow_model 0.2.4 → 0.2.5

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: a440b755047339d9c19cb083d67e6a850c1daf1b
4
- data.tar.gz: 6f089365819678b32129e3e28a8b87cc5b13c6c7
3
+ metadata.gz: e255572a7b0b5d218d46279a38b089b0b5f6558b
4
+ data.tar.gz: 6d1886e2e310991e52706e123f84efd87efb61c7
5
5
  SHA512:
6
- metadata.gz: 37f50a5bae551d95d019f739abadddd804bc6ffe61dac2c62a16cf632c046d3ecddbf53e9b7c6ce85db8fc39035fa72bb5de39bfa451a3966b736932e212a86c
7
- data.tar.gz: 7322e40d43c14aee65124ad30dd454a5bb6769dc2024f9ee3df7891e657e565ccedb7196b27d32642bfb760a201818fc490ff68b56811f1a7e9e21bd720b35bc
6
+ metadata.gz: 9849e048bd70790ec511a96b3bc4a90f0e7241bffc11464832fd4eb56886e79b156398ea6af164ecc2fa38d57533970b6faba7a0f8e298d6a07c27fecb83a911
7
+ data.tar.gz: 32b0e4c220f0554258aa7739db64c6e92d38b9ae429f87e5b59ac1ef66584c207ef37be4e7e0f1ab3a022ad16b74298fa3adf81e619a2129ebb94a79ebe09dd1
data/README.md CHANGED
@@ -24,9 +24,6 @@ And use this to retrieve the model from redis.
24
24
 
25
25
  model = YourModelClass.find_by_shadow(primary_key)
26
26
 
27
- model.is_a?(YourModelClass) # true
28
- model.readonly? # true
29
-
30
27
  ### options
31
28
 
32
29
  <table>
@@ -61,6 +58,15 @@ And use this to retrieve the model from redis.
61
58
  end
62
59
  end
63
60
 
61
+ player = Player.create(name: "player one")
62
+ shadow = Player.find_by_shadow(player.id)
63
+ shadow.is_a?(Player) # true
64
+ shadow.shadow_model? # true
65
+ shadow.readonly? # true
66
+ shadow.reload # reload from database
67
+ shadow.shadow_model? # false
68
+ shadow.readonly? # false
69
+
64
70
  ## Associations
65
71
 
66
72
  ### has_many
@@ -9,60 +9,6 @@ module ShadowModel
9
9
  RUBY
10
10
  end
11
11
 
12
- def shadow_model?
13
- @shadow_model
14
- end
15
-
16
- def shadow_model=(value)
17
- @shadow_model = value
18
- end
19
-
20
- def shadow_cache_key
21
- @shadow_cache_key ||= self.class.build_shadow_cache_key(self[self.class.primary_key])
22
- end
23
-
24
- def shadow_data
25
- @shadow_data ||= self.class.find_shadow_data(self[self.class.primary_key])
26
- end
27
-
28
- def shadow_data=(data)
29
- @shadow_data = data
30
- self.class.attribute_names.each do |attribute_name|
31
- attribute_name = attribute_name.to_sym
32
- if value = data[attribute_name]
33
- self[attribute_name] = value
34
- end
35
- end
36
- end
37
-
38
- def clear_shadow_data
39
- Redis.current.del(shadow_cache_key)
40
- end
41
-
42
- def build_shadow_data
43
- Marshal.dump(self.class.shadow_keys.inject({}) { |data, attr| data[attr] = send(attr); data })
44
- end
45
-
46
- def update_shadow_cache
47
- return if shadow_options[:association_only]
48
- Redis.current.set(shadow_cache_key, build_shadow_data)
49
- update_expiration(shadow_cache_key)
50
- end
51
-
52
- def update_expiration(cache_key)
53
- if expiration = self.class.shadow_options[:expiration]
54
- if self.class.shadow_options[:update_expiration] || shadow_ttl < 0
55
- Redis.current.expire(cache_key, expiration)
56
- end
57
- elsif expireat = self.class.shadow_options[:expireat]
58
- Redis.current.expireat(cache_key, expireat.to_i) if shadow_ttl < 0
59
- end
60
- end
61
-
62
- def shadow_ttl
63
- Redis.current.ttl(shadow_cache_key)
64
- end
65
-
66
12
  module ClassMethods
67
13
  def set_shadow_keys(args, options = {})
68
14
  @shadow_attributes ||= []
@@ -111,6 +57,7 @@ module ShadowModel
111
57
  end
112
58
 
113
59
  def find_by_shadow(id)
60
+ raise "Cannot find seperate shadow cache with association_only option set" if shadow_options[:association_only]
114
61
  if shadow_data = find_shadow_data(id)
115
62
  instantiate_shadow_model(shadow_data)
116
63
  else
@@ -127,8 +74,8 @@ module ShadowModel
127
74
 
128
75
  def instantiate_shadow_model(shadow_data)
129
76
  instance = self.new
130
- instance.shadow_model = true
131
- instance.shadow_data = shadow_data
77
+ instance.instance_variable_set(:@shadow_model, true)
78
+ instance.send(:shadow_data=, shadow_data)
132
79
  instance.readonly!
133
80
  instance
134
81
  end
@@ -137,5 +84,63 @@ module ShadowModel
137
84
  "#{self.name.tableize}:Shadow:#{id}"
138
85
  end
139
86
  end
87
+
88
+ def shadow_model?
89
+ @shadow_model
90
+ end
91
+
92
+ def reload
93
+ self.instance_variable_set(:@readonly, false)
94
+ @shadow_model = false
95
+ super
96
+ end
97
+
98
+ def shadow_cache_key
99
+ @shadow_cache_key ||= self.class.build_shadow_cache_key(self[self.class.primary_key])
100
+ end
101
+
102
+ def shadow_data
103
+ @shadow_data ||= self.class.find_shadow_data(self[self.class.primary_key])
104
+ end
105
+
106
+ def clear_shadow_data
107
+ Redis.current.del(shadow_cache_key)
108
+ end
109
+
110
+ def build_shadow_data
111
+ Marshal.dump(self.class.shadow_keys.inject({}) { |data, attr| data[attr] = send(attr); data })
112
+ end
113
+
114
+ def update_shadow_cache
115
+ return if shadow_options[:association_only]
116
+ Redis.current.set(shadow_cache_key, build_shadow_data)
117
+ update_expiration(shadow_cache_key)
118
+ end
119
+
120
+ def update_expiration(cache_key)
121
+ if expiration = shadow_options[:expiration]
122
+ if shadow_options[:update_expiration] || shadow_ttl < 0
123
+ Redis.current.expire(cache_key, expiration)
124
+ end
125
+ elsif expireat = shadow_options[:expireat]
126
+ Redis.current.expireat(cache_key, expireat.to_i) if shadow_ttl < 0
127
+ end
128
+ end
129
+
130
+ def shadow_ttl
131
+ Redis.current.ttl(shadow_cache_key)
132
+ end
133
+
134
+ private
135
+
136
+ def shadow_data=(data)
137
+ @shadow_data = data
138
+ self.class.attribute_names.each do |attribute_name|
139
+ attribute_name = attribute_name.to_sym
140
+ if value = data[attribute_name]
141
+ self[attribute_name] = value
142
+ end
143
+ end
144
+ end
140
145
  end
141
146
  end
@@ -1,3 +1,3 @@
1
1
  module ShadowModel
2
- VERSION = "0.2.4"
2
+ VERSION = "0.2.5"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shadow_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Weihu Chen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-24 00:00:00.000000000 Z
11
+ date: 2013-10-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails