Sha256: 58c88123a1abf6e3b606f9b2340398cafc9b21f45a47d07f119482ca2561c301

Contents?: true

Size: 1.71 KB

Versions: 14

Compression:

Stored size: 1.71 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))
        begin
          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)
        rescue Errno::ENOENT
          # Already deleted. Race condition?
        end
        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, 0600) 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

14 entries across 12 versions & 3 rubygems

Version Path
sass-4.0.0.alpha.1 lib/sass/cache_stores/filesystem.rb
sass-3.4.20 lib/sass/cache_stores/filesystem.rb
sc_core-0.0.7 test/dummy/vendor/bundle/ruby/2.2.0/gems/sass-3.4.18/lib/sass/cache_stores/filesystem.rb
sc_core-0.0.7 test/dummy/vendor/bundle/ruby/2.2.0/gems/sass-3.4.19/lib/sass/cache_stores/filesystem.rb
sass-3.4.19 lib/sass/cache_stores/filesystem.rb
sass-3.4.18 lib/sass/cache_stores/filesystem.rb
sass-3.4.17 lib/sass/cache_stores/filesystem.rb
solidus_backend-1.0.0.pre3 vendor/bundle/gems/sass-3.4.15/lib/sass/cache_stores/filesystem.rb
solidus_backend-1.0.0.pre2 vendor/bundle/gems/sass-3.4.15/lib/sass/cache_stores/filesystem.rb
sass-3.4.16 lib/sass/cache_stores/filesystem.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/sass-3.4.15/lib/sass/cache_stores/filesystem.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/sass-3.4.14/lib/sass/cache_stores/filesystem.rb
sass-3.4.15 lib/sass/cache_stores/filesystem.rb
sass-3.4.14 lib/sass/cache_stores/filesystem.rb