class 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) { |a, b| ... } 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 = {}) { |asset| ... } 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()
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() { |path, filename| ... } 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 = {})
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 = {})
Alias for: resolve

Private Instance Methods

matches_filter(filters, logical_path, filename) click to toggle source

Deprecated: Seriously.

# File lib/sprockets/legacy.rb, line 139
def matches_filter(filters, logical_path, filename)
  return true if filters.empty?

  filters.any? do |filter|
    if filter.is_a?(Regexp)
      filter.match(logical_path)
    elsif filter.respond_to?(:call)
      if filter.arity == 1
        filter.call(logical_path)
      else
        filter.call(logical_path, filename.to_s)
      end
    else
      File.fnmatch(filter.to_s, logical_path)
    end
  end
end
unescape(str) click to toggle source
# File lib/sprockets/legacy.rb, line 160
def unescape(str)
  str = URI::DEFAULT_PARSER.unescape(str)
  str.force_encoding(Encoding.default_internal) if Encoding.default_internal
  str
end