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 #-- # Inject the sweepers *after* the event to have a valid # oid. #++ 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. # # == Input # # * relative = if set to true, prepends the controller # mount path. #-- # 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, relative = false) if relative and controller = klass.ann.self[:controller] filename = "#{Server.public_root}/#{controller.mount_path}/#{name}.html".squeeze('/') else filename = "#{Server.public_root}/#{name}.html".squeeze('/') end Logger.debug "Sweeper expired cache file '#{filename}'" if $DBG FileUtils.rm_rf(filename) rescue Object => ex # ignore any error. 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 # Called from an action that modifies the model to expire # affected (dependend) cached pages. Generally you don't # override this method. # # == Input # # * relative = if set to true, prepends the controller # mount path. def expire_affected_output(name, relative = true) Sweeper.expire(name, self.class, relative) 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