Sha256: deed1c6bdcc797fb7b5f03a1aa4f6c3d98cc3059a5b75872889f7a8b7b508318

Contents?: true

Size: 1.17 KB

Versions: 9

Compression:

Stored size: 1.17 KB

Contents

module Sinatra
  module AssetPack
    module BusterHelpers
      extend self
      # Returns the MD5 for all of the files concatenated together
      def cache_buster_hash(*files)
        content = files.sort.map do |f|
          Digest::MD5.file(f).hexdigest if f.is_a?(String) && File.file?(f)
        end.compact
        Digest::MD5.hexdigest(content.join) if content.any?
      end

      # Returns the maximum mtime for a given list of files.
      # It will return nil if none of them are found.
      def mtime_for(files)
        time = files.map { |f| File.mtime(f).to_i if f.is_a?(String) && File.file?(f) }.compact.max
        Time.at(time) if time
      end

      # Adds a cache buster for the given path.
      #
      # The 2nd parameter (and beyond) are the files to take mtime from.
      # If the files are not found, the paths will be left as they are.
      #
      #   add_cache_buster('/images/email.png', '/var/www/x/public/images/email.png')
      #
      def add_cache_buster(path, *files)
        hash = cache_buster_hash *files

        if hash
          path.gsub(/(\.[^.]+)$/) { |ext| ".#{hash}#{ext}" }
        else
          path
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
sinatra-assetpack-0.3.5 lib/sinatra/assetpack/buster_helpers.rb
sinatra-assetpack-0.3.3 lib/sinatra/assetpack/buster_helpers.rb
sinatra-assetpack-0.3.2 lib/sinatra/assetpack/buster_helpers.rb
sinatra-assetpack-0.3.1 lib/sinatra/assetpack/buster_helpers.rb
sinatra-assetpack-0.3.0 lib/sinatra/assetpack/buster_helpers.rb
sinatra-assetpack-0.2.8 lib/sinatra/assetpack/buster_helpers.rb
sinatra-assetpack-0.2.7 lib/sinatra/assetpack/buster_helpers.rb
sinatra-assetpack-0.2.6 lib/sinatra/assetpack/buster_helpers.rb
sinatra-assetpack-0.2.5 lib/sinatra/assetpack/buster_helpers.rb