Sha256: fe7d355cb84995b5fd4dbcbbf4870955a0bd1e153234b09050d640d8550f9c52

Contents?: true

Size: 1.6 KB

Versions: 30

Compression:

Stored size: 1.6 KB

Contents

require 'fileutils'

module Sass
  module CacheStores
    # A backend for the Sass cache using the filesystem.
    class Filesystem < Base
      # The directory where the cached files will be stored.
      #
      # @return [String]
      attr_accessor :cache_location

      # @param cache_location [String] see \{#cache\_location}
      def initialize(cache_location)
        @cache_location = cache_location
      end

      # @see Base#\_retrieve
      def _retrieve(key, version, sha)
        return unless File.readable?(path_to(key))
        File.open(path_to(key), "rb") do |f|
          if f.readline("\n").strip == version && f.readline("\n").strip == sha
            return f.read
          end
        end
        File.unlink path_to(key)
        nil
      rescue EOFError, TypeError, ArgumentError => e
        Sass::Util.sass_warn "Warning. Error encountered while reading cache #{path_to(key)}: #{e}"
      end

      # @see Base#\_store
      def _store(key, version, sha, contents)
        compiled_filename = path_to(key)
        FileUtils.mkdir_p(File.dirname(compiled_filename))
        Sass::Util.atomic_create_and_write_file(compiled_filename) do |f|
          f.puts(version)
          f.puts(sha)
          f.write(contents)
        end
      rescue Errno::EACCES
        # pass
      end

      private

      # Returns the path to a file for the given key.
      #
      # @param key [String]
      # @return [String] The path to the cache file.
      def path_to(key)
        key = key.gsub(/[<>:\\|?*%]/) {|c| "%%%03d" % Sass::Util.ord(c)}
        File.join(cache_location, key)
      end
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
sass-3.3.3 lib/sass/cache_stores/filesystem.rb
sass-3.3.2 lib/sass/cache_stores/filesystem.rb
sass-3.3.1 lib/sass/cache_stores/filesystem.rb
sass-3.3.0 lib/sass/cache_stores/filesystem.rb
sass-3.3.0.rc.6 lib/sass/cache_stores/filesystem.rb
sass-3.3.0.rc.5 lib/sass/cache_stores/filesystem.rb
sass-3.3.0.rc.4 lib/sass/cache_stores/filesystem.rb
sass-3.3.0.rc.3 lib/sass/cache_stores/filesystem.rb
sass-3.3.0.rc.2 lib/sass/cache_stores/filesystem.rb
sass-3.3.0.rc.1 lib/sass/cache_stores/filesystem.rb
sass-3.3.0.alpha.392 lib/sass/cache_stores/filesystem.rb
sass-3.3.0.alpha.391 lib/sass/cache_stores/filesystem.rb
sass-3.3.0.alpha.390 lib/sass/cache_stores/filesystem.rb
sass-3.3.0.alpha.389 lib/sass/cache_stores/filesystem.rb
sass-3.3.0.alpha.388 lib/sass/cache_stores/filesystem.rb
sass-3.3.0.alpha.382 lib/sass/cache_stores/filesystem.rb
sass-3.3.0.alpha.380 lib/sass/cache_stores/filesystem.rb
sass-3.3.0.alpha.378 lib/sass/cache_stores/filesystem.rb
sass-3.3.0.alpha.377 lib/sass/cache_stores/filesystem.rb
sass-3.3.0.alpha.376 lib/sass/cache_stores/filesystem.rb