module ThreeD::ClassMethodCache::ClassMethods

Public Instance Methods

activate_method_cache(methods_list = {}) click to toggle source
# File lib/three_d/class_method_cache.rb, line 11
def activate_method_cache(methods_list = {})

  if !self.method_cache_enabled?

    cattr_accessor  :cached_methods
    cattr_accessor  :cache_storage

    # Setting up Cache Storage Variable
    cache_storage_name = "$#{self.name.underscore.upcase}_METHOD_CACHE"
    eval("#{cache_storage_name} = {}") 
    self.cache_storage = eval(cache_storage_name)


    self.add_cache_methods(methods_list)


    # Instanzmethoden für den User laden
    self.send :include,  InstanceMethods

  end    
end
add_cache_methods(methods_list = {}) click to toggle source
# File lib/three_d/class_method_cache.rb, line 34
def add_cache_methods(methods_list = {})
  # Check cached methods list
  if methods_list.is_a?(Array)
    methods_list = {:default => methods_list}
  end

  if methods_list.is_a?(Hash)

    self.cached_methods ||= {}

    methods_list.each do |group, methods|
      if self.cached_methods[group.to_sym].nil?
        self.cached_methods[group.to_sym] = methods
      else
        self.cached_methods[group.to_sym] << methods
        self.cached_methods[group.to_sym] = self.cached_methods[group.to_sym].flatten
      end    
    end  
  else
    raise ArgumentError, "Wrong definition for cached methods: Use either an array (self.cached_methods will become {:default => array}) or a hash"
  end

  setup_cache_method_aliasing
end
method_cache_enabled?() click to toggle source
# File lib/three_d/class_method_cache.rb, line 132
def method_cache_enabled?
  self.included_modules.include?(InstanceMethods)
end
setup_cache_method_aliasing() click to toggle source
# File lib/three_d/class_method_cache.rb, line 59
def setup_cache_method_aliasing
  # Setup des MethodAliasing
  self.instance_eval do
    self.cached_methods.each do |method_stack, methods|
      methods.each_with_index do |method, i|
        puts [self.name, method, self.instance_methods.include?(method.to_sym)].inspect
          
        # Methodennamen mit ?/!  am ende fixen - Alias anlegen
        needs_alias = false
        if method.to_s.last == "?"
          fixed_name = method.to_s.gsub("?","_with_soi")
          needs_alias = true
        elsif method.to_s.last == "!"
          fixed_name = method.to_s.gsub("?","_with_bang")
          needs_alias = true
        end
          
        if needs_alias
          alias_method fixed_name, method    
          
          method = fixed_name
        end
          
        define_method("#{method}_with_caching") do |*args|
          #begin
          start = Time.now
          # Key für den Cache erzeugen - Alle Parameter mit ablegen
          args_list = *args.flatten.map {|a| a.is_a?(ActiveRecord::Base) ? "#{a.class.name}-#{a.id}" : a}
          meth_name = [method,args_list].inspect.parameterize.to_s
          
          
          if cached?(meth_name)
            x = self.cached_attribute(meth_name)
          
            # => ActiveRecordArray disabled (03.08.2013, 18:54, Florian Eck)
            # if x.is_a?(ActiveRecordArray)
            #                    x = x.parse_record_data
            #                  end
            CachingLog.info "  ... CALL cache_data #{meth_name} (#{Time.now-start})"
            return x
          else
            # Hier wird die Originalfunction aufgerufen
            orig = self.send("#{method}_without_caching", *args)
          
            # => ActiveRecordArray disabled
            #if orig.is_active_record_array?
            #parse = orig.parse_active_record_array
            #CachingLog.info "save ActiveRecordArray"
            #else
            parse = orig
            #end
          
            v = cache_attribute!(meth_name, parse)
            CachingLog.info "  ... CREATE cache_data #{meth_name} (#{Time.now-start})"
                
            return orig
                
                
          end
          #rescue Exception => e
          #  self.method_cache = {:last_error => e.backtrace, :last_error_class => e.class.inspect, :last_error_message => e.message}
          #  return nil #self.send("#{method}_without_caching", *args)
          #end
          #__send__("#{method}_without_caching", *args)
        end
          
        alias_method_chain method, :caching
      
      end
    end
  end
end