Module: ShadowModel::ModelExtension::ClassMethods

Defined in:
lib/shadow_model/model_extension.rb

Instance Method Summary collapse

Instance Method Details

#build_shadow_cache_key(id) ⇒ Object



85
86
87
# File 'lib/shadow_model/model_extension.rb', line 85

def build_shadow_cache_key(id)
  "#{self.name.tableize}:Shadow:#{id}"
end

#define_shadow_attributes(attribute_name) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/shadow_model/model_extension.rb', line 36

def define_shadow_attributes(attribute_name)
  self.class_eval <<-RUBY
    def #{attribute_name}_without_shadow
      self.read_attribute(:#{attribute_name})
    end

    def #{attribute_name}
      shadow_model? ? shadow_data[:#{attribute_name}] : #{attribute_name}_without_shadow
    end
  RUBY
end

#define_shadow_methods(method_name) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/shadow_model/model_extension.rb', line 48

def define_shadow_methods(method_name)
  if self.shadow_keys.include?(method_name.to_sym) && !self.instance_methods.include?("#{method_name}_without_shadow".to_sym)
    self.class_eval <<-RUBY
      def #{method_name}_with_shadow
        shadow_model? ? shadow_data[:#{method_name}] : #{method_name}_without_shadow
      end
      alias_method_chain :#{method_name}, :shadow
    RUBY
  end
end

#find_by_shadow(id) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/shadow_model/model_extension.rb', line 59

def find_by_shadow(id)
  raise "Cannot find seperate shadow cache with association_only option set" if shadow_options[:association_only]
  if shadow_data = find_shadow_data(id)
    instantiate_shadow_model(shadow_data)
  else
    instance = self.find(id)
    instance.update_shadow_cache
    find_by_shadow(id)
  end
end

#find_shadow_data(id) ⇒ Object



70
71
72
73
# File 'lib/shadow_model/model_extension.rb', line 70

def find_shadow_data(id)
  data = Redis.current.get(build_shadow_cache_key(id))
  data ? Marshal.load(data) : nil
end

#instantiate_shadow_model(shadow_data) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/shadow_model/model_extension.rb', line 75

def instantiate_shadow_model(shadow_data)
  instance = self.new
  instance.instance_variable_set(:@shadow_model, true)
  instance.instance_variable_set(:@persisted, true)     # rails3
  instance.instance_variable_set(:@new_record, false)   # rails4
  instance.send(:shadow_data=, shadow_data)
  instance.readonly!
  instance
end

#method_added(method_name) ⇒ Object



32
33
34
# File 'lib/shadow_model/model_extension.rb', line 32

def method_added(method_name)
  define_shadow_methods(method_name)
end

#set_shadow_keys(args, options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/shadow_model/model_extension.rb', line 13

def set_shadow_keys(args, options = {})
  @shadow_attributes ||= []
  @shadow_methods ||= []
  self.shadow_options = options
  args.each do |arg|
    if self.attribute_names.include?(arg.to_s)
      @shadow_attributes << arg.to_sym
    else
      @shadow_methods << arg.to_sym
    end
  end
  @shadow_attributes << primary_key.to_sym if !@shadow_attributes.include?(primary_key.to_sym)
  @shadow_attributes.each { |attr| define_shadow_attributes(attr) }
end

#shadow_keysObject



28
29
30
# File 'lib/shadow_model/model_extension.rb', line 28

def shadow_keys
  (@shadow_attributes || []) + (@shadow_methods || [])
end