lib/juicer/cache_buster.rb in psyho_juicer-1.0.0 vs lib/juicer/cache_buster.rb in psyho_juicer-1.0.7

- old
+ new

@@ -76,56 +76,96 @@ # is specified, <tt>:soft</tt> will be used. # # See <tt>#hard</tt> and <tt>#soft</tt> for explanation of the parameter # argument. # - def self.path(file, type = :soft, parameter = DEFAULT_PARAMETER) + def self.path(file, opts = {}) return file if file =~ /data:.*;base64/ + + type = opts[:type] + type = [:soft, :hard, :rails].include?(type) ? type : :soft + + parameter = opts.key?(:parameter) ? opts[:parameter] : DEFAULT_PARAMETER + parameter = nil if type == :rails + file = self.clean(file, parameter) filename = file.split("?").first + + revision_type = opts[:revision_type] + revision_type = :mtime if type == :rails + revision_type = [:git, :mtime].include?(revision_type) ? revision_type : :mtime + raise ArgumentError.new("#{file} could not be found") unless File.exists?(filename) - mtime = File.mtime(filename).to_i - type = [:soft, :hard].include?(type) ? type : :soft + revision = Revision.send(revision_type, filename) + if type == :soft parameter = "#{parameter}=".sub(/^=$/, '') - return "#{file}#{file.index('?') ? '&' : '?'}#{parameter}#{mtime}" + return "#{file}#{file.index('?') ? '&' : '?'}#{parameter}#{revision}" + elsif type == :rails + return "#{file}#{file.index('?') ? '' : "?#{revision}"}" end - file.sub(/(\.[^\.]+$)/, "-#{parameter}#{mtime}" + '\1') + file.sub(/(\.[^\.]+$)/, "-#{parameter}#{revision}" + '\1') end # # Add a hard cache buster to a filename. The parameter is an optional prefix # that is added before the mtime timestamp. It results in filenames of the form: # <tt>file-[parameter name][timestamp].suffix</tt>, ie # <tt>images/logo-cb1234567890.png</tt> which is the case for the default # parameter name "cb" (as in *c*ache *b*uster). # - def self.hard(file, parameter = DEFAULT_PARAMETER) - self.path(file, :hard, parameter) + def self.hard(file, opts = {}) + self.path(file, opts.merge(:type => :hard)) end # # Add a soft cache buster to a filename. The parameter is an optional name # for the mtime timestamp value. It results in filenames of the form: # <tt>file.suffix?[parameter name]=[timestamp]</tt>, ie # <tt>images/logo.png?cb=1234567890</tt> which is the case for the default # parameter name "cb" (as in *c*ache *b*uster). # - def self.soft(file, parameter = DEFAULT_PARAMETER) - self.path(file, :soft, parameter) + def self.soft(file, opts = {}) + self.path(file, opts.merge(:type => :soft)) end # + # Add a Rails-style cache buster to a filename. Results in filenames of the + # form: <tt>file.suffix?[timestamp]</tt>, ie <tt>images/logo.png?1234567890</tt> + # which is the format used by Rails' image_tag helper. + # + def self.rails(file, opts = {}) + self.path(file, opts.merge(:type => :rails)) + end + + # # Remove cache buster from a URL for a given parameter name. Parameter name is # "cb" by default. # def self.clean(file, parameter = DEFAULT_PARAMETER) - query_param = "#{parameter}".length == 0 ? "" : "#{parameter}=" - new_file = file.sub(/#{query_param}\d+&?/, "").sub(/(\?|&)$/, "") - return new_file unless new_file == file + if "#{parameter}".length == 0 + return file.sub(/\?\d+$/, '') + else + query_param = "#{parameter}=" + new_file = file.sub(/#{query_param}\d+&?/, "").sub(/(\?|&)$/, "") + return new_file unless new_file == file - file.sub(/-#{parameter}\d+(\.\w+)($|\?)/, '\1\2') + file.sub(/-#{parameter}\d+(\.\w+)($|\?)/, '\1\2') + end end + + module Revision + def self.git(filename) + version = %x{git log --pretty=format:%H -1 -- #{filename} 2> /dev/null} + version = %x{git log --pretty=format:%H -1 2> /dev/null} if version == '' || version == nil + version = 'unknown' if version == '' || version == nil + return version + end + + def self.mtime(filename) + File.mtime(filename).to_i + end + end end end