class Sprockets::Base

‘Base` class for `Environment` and `CachedEnvironment`.

Attributes

cache[R]

Get persistent cache store

Public Instance Methods

[](*args, **options) click to toggle source

Preferred ‘find_asset` shorthand.

environment['application.js']
# File lib/sprockets/base.rb, line 118
def [](*args, **options)
  find_asset(*args, **options)
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 48
def cache=(cache)
  @cache = Cache.new(cache, logger)
end
cached() click to toggle source

Return an ‘CachedEnvironment`. Must be implemented by the subclass.

# File lib/sprockets/base.rb, line 53
def cached
  raise NotImplementedError
end
Also aliased as: index
compress_from_root(uri) click to toggle source
# File lib/sprockets/base.rb, line 139
def compress_from_root(uri)
  URITar.new(uri, self).compress
end
expand_from_root(uri) click to toggle source
# File lib/sprockets/base.rb, line 143
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 63
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(*args) { |asset| ... } click to toggle source
# File lib/sprockets/base.rb, line 85
def find_all_linked_assets(*args)
  return to_enum(__method__, *args) unless block_given?

  parent_asset = asset = find_asset(*args)
  return unless asset

  yield asset
  stack = asset.links.to_a
  linked_paths = {}

  while uri = stack.shift
    yield asset = load(uri)

    last_filename = linked_paths[asset.logical_path]
    if last_filename && last_filename != asset.filename
      raise DoubleLinkError.new(
        parent_filename: parent_asset.filename,
        last_filename:   last_filename,
        logical_path:    asset.logical_path,
        filename:        asset.filename
      )
    end
    linked_paths[asset.logical_path] = asset.filename
    stack = asset.links.to_a + stack
  end

  nil
end
find_asset(*args, **options) click to toggle source

Find asset by logical path or expanded path.

# File lib/sprockets/base.rb, line 78
def find_asset(*args, **options)
  uri, _ = resolve(*args, **options)
  if uri
    load(uri)
  end
end
find_asset!(*args) click to toggle source

Find asset by logical path or expanded path.

If the asset is not found an error will be raised.

# File lib/sprockets/base.rb, line 125
def find_asset!(*args)
  uri, _ = resolve!(*args)
  if uri
    load(uri)
  end
end
index()
Alias for: cached
inspect() click to toggle source

Pretty inspect

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