module ShadowModel::ModelExtension

Public Class Methods

included(base) click to toggle source
# File lib/shadow_model/model_extension.rb, line 3
    def self.included(base)
      base.class_eval <<-RUBY
        mattr_accessor :shadow_options
        extend ClassMethods
        after_save :update_shadow_cache
        after_destroy :clear_shadow_data
      RUBY
    end

Public Instance Methods

build_shadow_data() click to toggle source
# 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() click to toggle source
# File lib/shadow_model/model_extension.rb, line 108
def clear_shadow_data
  Redis.current.del(shadow_cache_key)
end
reload() click to toggle source
Calls superclass method
# 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() click to toggle source
# 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() click to toggle source
# 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?() click to toggle source
# File lib/shadow_model/model_extension.rb, line 90
def shadow_model?
  @shadow_model
end
shadow_ttl() click to toggle source
# File lib/shadow_model/model_extension.rb, line 132
def shadow_ttl
  Redis.current.ttl(shadow_cache_key)
end
update_expiration(cache_key) click to toggle source
# 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_cache() click to toggle source
# 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

Private Instance Methods

shadow_data=(data) click to toggle source
# File lib/shadow_model/model_extension.rb, line 138
def shadow_data=(data)
  @shadow_data = data
  self.class.attribute_names.each do |attribute_name|
    attribute_name = attribute_name.to_sym
    if value = data[attribute_name]
      self[attribute_name] = value
    end
  end
end