require 'facets/more/aspects' module Glue # This module adds cleanup functionality to managed # classes. Override and implement sweep_affected. # Typically used to cleanup output caching files from # the filesystem. But you can also use it to clean up # temp objects in the database or other temp files. #-- # FIXME: find a better name. #++ module Sweeper include Aspects before "sweep_affected(:insert)", :on => :og_insert before "sweep_affected(:update)", :on => :og_update before "sweep_affected(:delete)", :on => :og_delete # Expires (deletes) a cached page from the file system. #-- # FIXME: replace with 'extend Nitro::Caching::Output' ? # this way we wont have to sync the same code in two different # places. # If you change this method, don't forget the Caching::Output # expire method. #++ def self.expire(name, klass) begin filename = "#{Server.public_root}/#{klass.ann.self.controller.mount_path}/#{name}".squeeze('/') Logger.debug "Sweeper expired cache file '#{filename}'" if $DBG FileUtils.rm_rf(filename) rescue Object # gmosx: is this the right thing to do? end end private # If needed pass the calling action symbol to allow # for conditional expiration. # Action takes values from { :insert, :delete, :update } # or your own custom enumeration. # # Generally add lines like the following: # # expire_affected_output('file_to_expire') def sweep_affected(action = :all) end alias_method :expire_affected, :sweep_affected #-- # Generally you don't override this. #++ def expire_affected_output(name) Sweeper.expire(name, self.class) end alias_method :expire_output, :expire_affected_output # Expire affected cached fragments. def expire_affected_fragment(name, options = {}) Nitro::Caching::Fragments.cache.delete(name, options) end alias_method :expire_fragment, :expire_affected_fragment end end # * George Moschovitis