Module: ShadowModel::Extension
- Defined in:
- lib/shadow_model/extension.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #build_shadow_data ⇒ Object
- #clear_shadow_data ⇒ Object
- #shadow_cache_key ⇒ Object
- #shadow_data ⇒ Object
- #shadow_data=(data) ⇒ Object
- #shadow_model=(value) ⇒ Object
- #shadow_model? ⇒ Boolean
- #shadow_ttl ⇒ Object
- #update_expiration ⇒ Object
- #update_shadow_cache ⇒ Object
Class Method Details
.included(base) ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/shadow_model/extension.rb', line 3 def self.included(base) base.class_eval " mattr_accessor :shadow_options\n extend ClassMethods\n after_save :update_shadow_cache\n RUBY\nend\n" |
Instance Method Details
#build_shadow_data ⇒ Object
35 36 37 |
# File 'lib/shadow_model/extension.rb', line 35 def build_shadow_data Marshal.dump(self.class.shadow_keys.inject({}) { |data, attr| data[attr] = send(attr); data }) end |
#clear_shadow_data ⇒ Object
31 32 33 |
# File 'lib/shadow_model/extension.rb', line 31 def clear_shadow_data Redis.current.del(shadow_cache_key) end |
#shadow_cache_key ⇒ Object
19 20 21 |
# File 'lib/shadow_model/extension.rb', line 19 def shadow_cache_key @shadow_cache_key ||= self.class.build_shadow_cache_key(self[self.class.primary_key]) end |
#shadow_data ⇒ Object
23 24 25 |
# File 'lib/shadow_model/extension.rb', line 23 def shadow_data @shadow_data ||= self.class.find_shadow_data(self[self.class.primary_key]) end |
#shadow_data=(data) ⇒ Object
27 28 29 |
# File 'lib/shadow_model/extension.rb', line 27 def shadow_data=(data) @shadow_data = data end |
#shadow_model=(value) ⇒ Object
15 16 17 |
# File 'lib/shadow_model/extension.rb', line 15 def shadow_model=(value) @shadow_model = value end |
#shadow_model? ⇒ Boolean
11 12 13 |
# File 'lib/shadow_model/extension.rb', line 11 def shadow_model? @shadow_model end |
#shadow_ttl ⇒ Object
54 55 56 |
# File 'lib/shadow_model/extension.rb', line 54 def shadow_ttl Redis.current.ttl(shadow_cache_key) end |
#update_expiration ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/shadow_model/extension.rb', line 44 def update_expiration if self.class.[:expiration].present? if self.class.[:update_expiration] || shadow_ttl < 0 Redis.current.expire(shadow_cache_key, self.class.[:expiration]) end elsif self.class.[:expireat].present? Redis.current.expireat(shadow_cache_key, self.class.[:expireat].to_i) if shadow_ttl < 0 end end |
#update_shadow_cache ⇒ Object
39 40 41 42 |
# File 'lib/shadow_model/extension.rb', line 39 def update_shadow_cache Redis.current.set(shadow_cache_key, build_shadow_data) update_expiration end |