Module: ShadowModel::ModelExtension

Defined in:
lib/shadow_model/model_extension.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

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_dataObject



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_dataObject



108
109
110
# File 'lib/shadow_model/model_extension.rb', line 108

def clear_shadow_data
  Redis.current.del(shadow_cache_key)
end

#reloadObject



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_keyObject



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_dataObject



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

Returns:

  • (Boolean)


90
91
92
# File 'lib/shadow_model/model_extension.rb', line 90

def shadow_model?
  @shadow_model
end

#shadow_ttlObject



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 = shadow_options[:expiration]
    if shadow_options[:update_expiration] || shadow_ttl < 0
      Redis.current.expire(cache_key, expiration)
    end
  elsif expireat = shadow_options[:expireat]
    Redis.current.expireat(cache_key, expireat.to_i) if shadow_ttl < 0
  end
end

#update_shadow_cacheObject



116
117
118
119
120
# File 'lib/shadow_model/model_extension.rb', line 116

def update_shadow_cache
  return if shadow_options[:association_only]
  Redis.current.set(shadow_cache_key, build_shadow_data)
  update_expiration(shadow_cache_key)
end