Module: ShadowModel::ModelExtension
- Defined in:
- lib/shadow_model/model_extension.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #build_shadow_data ⇒ Object
- #clear_shadow_data ⇒ Object
- #reload ⇒ Object
- #shadow_cache_key ⇒ Object
- #shadow_data ⇒ Object
- #shadow_model? ⇒ Boolean
- #shadow_ttl ⇒ Object
- #update_expiration(cache_key) ⇒ Object
- #update_shadow_cache ⇒ Object
Class Method Details
.included(base) ⇒ Object
3 4 5 6 7 8 9 10 |
# File 'lib/shadow_model/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 after_destroy :clear_shadow_data\n RUBY\nend\n" |
Instance Method Details
#build_shadow_data ⇒ Object
112 113 114 |
# File 'lib/shadow_model/model_extension.rb', line 112 def build_shadow_data Marshal.dump(self.class.shadow_keys.inject({}) { |data, attr| data[attr] = send(attr); data }) end |
#clear_shadow_data ⇒ Object
108 109 110 |
# File 'lib/shadow_model/model_extension.rb', line 108 def clear_shadow_data Redis.current.del(shadow_cache_key) end |
#reload ⇒ Object
94 95 96 97 98 |
# File 'lib/shadow_model/model_extension.rb', line 94 def reload self.instance_variable_set(:@readonly, false) @shadow_model = false super end |
#shadow_cache_key ⇒ Object
100 101 102 |
# File 'lib/shadow_model/model_extension.rb', line 100 def shadow_cache_key @shadow_cache_key ||= self.class.build_shadow_cache_key(self[self.class.primary_key]) end |
#shadow_data ⇒ Object
104 105 106 |
# File 'lib/shadow_model/model_extension.rb', line 104 def shadow_data @shadow_data ||= self.class.find_shadow_data(self[self.class.primary_key]) end |
#shadow_model? ⇒ Boolean
90 91 92 |
# File 'lib/shadow_model/model_extension.rb', line 90 def shadow_model? @shadow_model end |
#shadow_ttl ⇒ Object
132 133 134 |
# File 'lib/shadow_model/model_extension.rb', line 132 def shadow_ttl Redis.current.ttl(shadow_cache_key) end |
#update_expiration(cache_key) ⇒ Object
122 123 124 125 126 127 128 129 130 |
# File 'lib/shadow_model/model_extension.rb', line 122 def update_expiration(cache_key) if expiration = [:expiration] if [:update_expiration] || shadow_ttl < 0 Redis.current.expire(cache_key, expiration) end elsif expireat = [:expireat] Redis.current.expireat(cache_key, expireat.to_i) if shadow_ttl < 0 end end |
#update_shadow_cache ⇒ Object
116 117 118 119 120 |
# File 'lib/shadow_model/model_extension.rb', line 116 def update_shadow_cache return if [:association_only] Redis.current.set(shadow_cache_key, build_shadow_data) update_expiration(shadow_cache_key) end |