module ShadowModel::ModelExtension::ClassMethods
Public Instance Methods
build_shadow_cache_key(id)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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 = {})
click to toggle source
# 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_keys()
click to toggle source
# File lib/shadow_model/model_extension.rb, line 28 def shadow_keys (@shadow_attributes || []) + (@shadow_methods || []) end