Class: Sprockets::Base

### Home Classes Methods

### In Files

- lib/sprockets/base.rb
- lib/sprockets/legacy.rb

### Parent

Object

### Methods

- #[]
- #cache=
- #cache_get
- #cache_set
- #cached
- #compress_from_root
- #each_logical_path
- #expand_from_root
- #file_digest
- #find_all_linked_assets
- #find_asset
- #index
- #inspect
- #logical_paths
- #normalize_logical_path
- #resolve
- #resolve_with_compat
- #resolve_without_compat

### Included Modules

- Sprockets::PathUtils
- Sprockets::PathDependencyUtils
- Sprockets::PathDigestUtils
- Sprockets::DigestUtils
- Sprockets::Configuration
- Sprockets::Server
- Sprockets::Resolve
- Sprockets::Loader
- Sprockets::Bower

### Class/Module Index

Quicksearch

- Sprockets
- Sprockets::ArgumentError
- Sprockets::Asset
- Sprockets::Autoload
- Sprockets::Base
- Sprockets::Bower
- Sprockets::Bundle
- Sprockets::Cache
- Sprockets::Cache::FileStore
- Sprockets::Cache::GetWrapper
- Sprockets::Cache::HashWrapper
- Sprockets::Cache::MemoryStore
- Sprockets::Cache::NullStore
- Sprockets::Cache::ReadWriteWrapper
- Sprockets::Cache::Wrapper
- Sprockets::CachedEnvironment
- Sprockets::ClosureCompressor
- Sprockets::CoffeeScriptProcessor
- Sprockets::CoffeeScriptTemplate
- Sprockets::Compressing
- Sprockets::Configuration
- Sprockets::ContentTypeMismatch
- Sprockets::Context
- Sprockets::ConversionError
- Sprockets::Dependencies
- Sprockets::DigestUtils
- Sprockets::DirectiveProcessor
- Sprockets::ERBProcessor
- Sprockets::ERBTemplate
- Sprockets::EcoProcessor
- Sprockets::EcoTemplate
- Sprockets::EjsProcessor
- Sprockets::EjsTemplate
- Sprockets::EncodingUtils
- Sprockets::Engines
- Sprockets::Environment
- Sprockets::Error
- Sprockets::FileNotFound
- Sprockets::FileOutsidePaths
- Sprockets::FileReader
- Sprockets::HTTPUtils
- Sprockets::Index
- Sprockets::JstProcessor
- Sprockets::LegacyProcProcessor
- Sprockets::LegacyTiltProcessor
- Sprockets::Loader
- Sprockets::Manifest
- Sprockets::ManifestUtils
- Sprockets::Mime
- Sprockets::NotFound
- Sprockets::NotImplementedError
- Sprockets::PathDependencyUtils
- Sprockets::PathDigestUtils
- Sprockets::PathUtils
- Sprockets::Paths
- Sprockets::Processing
- Sprockets::ProcessorUtils
- Sprockets::Resolve
- Sprockets::SassCompressor
- Sprockets::SassProcessor
- Sprockets::SassProcessor::CacheStore
- Sprockets::SassProcessor::Functions
- Sprockets::SassTemplate
- Sprockets::ScssProcessor
- Sprockets::ScssTemplate
- Sprockets::Server
- Sprockets::Transformers
- Sprockets::URITar
- Sprockets::URIUtils
- Sprockets::UglifierCompressor
- Sprockets::UnloadedAsset
- Sprockets::Utils
- Sprockets::YUICompressor
- Rake
- Rake::SprocketsTask

No matching classes.

# Sprockets::Base

`Base` class for `Environment` and `Cached`.

### Attributes

cache[R]

Get persistent cache store

### Public Instance Methods

[](*args) click to toggle source

Preferred `find_asset` shorthand.

    environment['application.js']

    # File lib/sprockets/base.rb, line 91
    def [](*args)
      find_asset(*args)
    end

cache=(cache) click to toggle source

Set persistent cache store

The cache store must implement a pair of getters and setters. Either `get(key)`/`set(key, value)`, `[key]`/`=value`, `read(key)`/`write(key, value)`.

    # File lib/sprockets/base.rb, line 33
    def cache=(cache)
      @cache = Cache.new(cache, logger)
    end

cache_get(key) click to toggle source

    # File lib/sprockets/legacy.rb, line 123
    def cache_get(key)
      cache.get(key)
    end

cache_set(key, value) click to toggle source

    # File lib/sprockets/legacy.rb, line 127
    def cache_set(key, value)
      cache.set(key, value)
    end

cached() click to toggle source

Return an `Cached`. Must be implemented by the subclass.

    # File lib/sprockets/base.rb, line 38
    def cached
      raise NotImplementedError
    end

Also aliased as: index
compress_from_root(uri) click to toggle source

    # File lib/sprockets/base.rb, line 102
    def compress_from_root(uri)
      URITar.new(uri, self).compress
    end

each_logical_path(*args, &block) click to toggle source

Deprecated: Iterate over all logical paths with a matcher.

Remove from 4.x.

args - List of matcher objects.

Returns Enumerator if no block is given.

    # File lib/sprockets/legacy.rb, line 79
    def each_logical_path(*args, &block)
      return to_enum(__method__, *args) unless block_given?

      filters = args.flatten.map { |arg| Manifest.compile_match_filter(arg) }
      logical_paths.each do |a, b|
        if filters.any? { |f| f.call(a, b) }
          if block.arity == 2
            yield a, b
          else
            yield a
          end
        end
      end

      nil
    end

expand_from_root(uri) click to toggle source

    # File lib/sprockets/base.rb, line 106
    def expand_from_root(uri)
      URITar.new(uri, self).expand
    end

file_digest(path) click to toggle source

Internal: Compute digest for path.

path - String filename or directory path.

Returns a String digest or nil.

    # File lib/sprockets/base.rb, line 48
    def file_digest(path)
      if stat = self.stat(path)
        # Caveat: Digests are cached by the path's current mtime. Its possible
        # for a files contents to have changed and its mtime to have been
        # negligently reset thus appearing as if the file hasn't changed on
        # disk. Also, the mtime is only read to the nearest second. It's
        # also possible the file was updated more than once in a given second.
        key = UnloadedAsset.new(path, self).file_digest_key(stat.mtime.to_i)
        cache.fetch(key) do
          self.stat_digest(path, stat)
        end
      end
    end

find_all_linked_assets(path, options = {}) click to toggle source

    # File lib/sprockets/base.rb, line 70
    def find_all_linked_assets(path, options = {})
      return to_enum(__method__, path, options) unless block_given?

      asset = find_asset(path, options)
      return unless asset

      yield asset
      stack = asset.links.to_a

      while uri = stack.shift
        yield asset = load(uri)
        stack = asset.links.to_a + stack
      end

      nil
    end

find_asset(path, options = {}) click to toggle source

Find asset by logical path or expanded path.

    # File lib/sprockets/base.rb, line 63
    def find_asset(path, options = {})
      uri, _ = resolve(path, options.merge(compat: false))
      if uri
        load(uri)
      end
    end

index() click to toggle source
Alias for: cached
inspect() click to toggle source

Pretty inspect

    # File lib/sprockets/base.rb, line 96
    def inspect
      "#<#{self.class}:0x#{object_id.to_s(16)} " +
        "root=#{root.to_s.inspect}, " +
        "paths=#{paths.inspect}>"
    end

logical_paths() click to toggle source

Deprecated: Enumerate over all logical paths in the environment.

Returns an Enumerator of [logical_path, filename].

    # File lib/sprockets/legacy.rb, line 99
    def logical_paths
      return to_enum(__method__) unless block_given?

      seen = Set.new

      paths.each do |load_path|
        stat_tree(load_path).each do |filename, stat|
          next unless stat.file?

          path = split_subpath(load_path, filename)
          path, mime_type, _, _ = parse_path_extnames(path)
          path = normalize_logical_path(path)
          path += mime_types[mime_type][:extensions].first if mime_type

          if !seen.include?(path)
            yield path, filename
            seen << path
          end
        end
      end

      nil
    end

normalize_logical_path(path) click to toggle source

    # File lib/sprockets/legacy.rb, line 131
    def normalize_logical_path(path)
      dirname, basename = File.split(path)
      path = dirname if basename == 'index'
      path
    end

resolve(path, options = {}) click to toggle source
Also aliased as: resolve_without_compat
Alias for: resolve_with_compat
resolve_with_compat(path, options = {}) click to toggle source

Deprecated: Change default return type of resolve() to return 2.x compatible plain filename String. 4.x will always return an Asset URI and a set of file system dependencies that had to be read to compute the result.

    2.x

      resolve("foo.js")
      # => "/path/to/app/javascripts/foo.js"

    3.x

      resolve("foo.js")
      # => "/path/to/app/javascripts/foo.js"

      resolve("foo.js", compat: true)
      # => "/path/to/app/javascripts/foo.js"

      resolve("foo.js", compat: false)
      # => [
      #   "file:///path/to/app/javascripts/foo.js?type=application/javascript"
      #    #<Set: {"file-digest:/path/to/app/javascripts/foo.js"}>
      # ]

    4.x

      resolve("foo.js")
      # => [
      #   "file:///path/to/app/javascripts/foo.js?type=application/javascript"
      #    #<Set: {"file-digest:/path/to/app/javascripts/foo.js"}>
      # ]

    # File lib/sprockets/legacy.rb, line 55
    def resolve_with_compat(path, options = {})
      options = options.dup
      if options.delete(:compat) { true }
        uri, _ = resolve_without_compat(path, options)
        if uri
          path, _ = parse_asset_uri(uri)
          path
        else
          nil
        end
      else
        resolve_without_compat(path, options)
      end
    end

Also aliased as: resolve
resolve_without_compat(path, options = {}) click to toggle source
Alias for: resolve

[Validate]

Generated with the Darkfish Rdoc Generator 2.