module ActsAsCaching::Hook
  def acts_as_caching(*args)
    @acts_as_caching_options = args.extract_options!
    
    
    def self.qi_cache_fields
      @acts_as_caching_options[:fields]
      
    end
    
    
    @acts_as_caching_options[:fields].each do |s|
      define_method("#{s}") {
        puts "CCC"
        if self.methods.include?(eval(":ca_#{s}"))
          puts "AAAA"
          puts self.methods.include?(eval(":ca_#{s}"))
          return eval("!self.ac_#{s} ? self.ca_#{s} : self.ac_#{s}")
          
        end
      }
    end
    
    
    
    
    
    
   
    define_method("qi_dynamics_cache"){
      puts "qi_dynamics_cache"
      puts "hook"
      puts self.class
      
    
      if self.methods.include?(:to_no_archive)
        self.to_no_archive.each do |noc|
          eval("self.ac_#{noc} = nil")
        end
      
      
      end
    
    
      self.class.qi_cache_fields.each do |s|
        if self.methods.include?(eval(":ca_#{s}"))
          eval("self.cc_#{s} = self.#{s}")
        end
      end
    }
  
    after_validation :qi_dynamics_cache
  
    define_method("archive_now"){
      self.qi_dynamics_cache
      self.archived = true
    
      self.class.qi_cache_fields.each do |s|
        eval("self.ac_#{s} = self.cc_#{s} ")
      end
    
      self.personalised_archive if self.methods.include?(:personalised_archive)
      self.save
    }
  
    define_method("unarchive_now"){
      self.archived = false
      self.qi_dynamics_cache
    
      self.class.qi_cache_fields.each do |s|
        eval("self.ac_#{s} = nil ")
      end
    
      self.personalised_unarchive if self.methods.include?(:personalised_unarchive)
    
      self.save
    }
    
    
  end
  
  
  
  
end
