Sha256: 2642915c7d7238d29b37cedc752a38ca2cadddf48389dfda0a083f1379b485ec

Contents?: true

Size: 960 Bytes

Versions: 3

Compression:

Stored size: 960 Bytes

Contents

module ThreeD
  module Once
    
    def self.included(base)
      base.extend(ClassMethods)
    end
    
    module ClassMethods
    
      def once(*ids)
        # Auch Array kann übergeben werde
        ids = ids.first if ids.first.is_a?(Array)
      
        ids.each do |id|
          id = id.to_s
          self.instance_eval do
            
            alias_method "__#{id}", id 
            
            define_method(id) do |*args|
              ident_name = *args.hash.to_i
            
              # Use @once_cache[]
              key_name = "#{id}_#{ident_name}_#{self.id}".gsub(/[^a-zA-Z0-9_@]/, '')
            
              @once_cache ||= {}
            
              if @once_cache[key_name].nil?
                @once_cache[key_name] = self.send("__#{id}", *args)
              end
            
              return @once_cache[key_name]
        
            end  
          end  
        end 
      end
    
    end
    
  end
end    

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
3d_cache-0.0.021 lib/three_d/once.rb
3d_cache-0.0.02a lib/three_d/once.rb
3d_cache-0.0.01a lib/three_d/once.rb