Sha256: 1f8841db214b2b83af1c8d15511a79ca6d790085c05ea9016630ff54ef5449b6

Contents?: true

Size: 1.7 KB

Versions: 33

Compression:

Stored size: 1.7 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
        begin
          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

33 entries across 33 versions & 2 rubygems

Version Path
sass-3.4.13 lib/sass/cache_stores/filesystem.rb
sass-3.4.12 lib/sass/cache_stores/filesystem.rb
sass-3.4.11 lib/sass/cache_stores/filesystem.rb
sass-3.4.10 lib/sass/cache_stores/filesystem.rb
sass-3.4.9 lib/sass/cache_stores/filesystem.rb
sass-3.4.8 lib/sass/cache_stores/filesystem.rb
sass-3.4.7 lib/sass/cache_stores/filesystem.rb
sass-3.4.6 lib/sass/cache_stores/filesystem.rb
sass-3.4.5 lib/sass/cache_stores/filesystem.rb
sass-3.4.4 lib/sass/cache_stores/filesystem.rb
sass-3.4.3 lib/sass/cache_stores/filesystem.rb
sass-3.4.2 lib/sass/cache_stores/filesystem.rb
sass-3.4.1 lib/sass/cache_stores/filesystem.rb
oreorenasass-3.4.4 lib/sass/cache_stores/filesystem.rb
oreorenasass-3.4.3 lib/sass/cache_stores/filesystem.rb
oreorenasass-3.4.2 lib/sass/cache_stores/filesystem.rb
oreorenasass-3.4.1 lib/sass/cache_stores/filesystem.rb
oreorenasass-3.4.0 lib/sass/cache_stores/filesystem.rb
sass-3.4.0 lib/sass/cache_stores/filesystem.rb
sass-3.4.0.rc.3 lib/sass/cache_stores/filesystem.rb