Parent

Sprockets::Base

`Base` class for `Environment` and `Index`.

Attributes

cache[R]

Get persistent cache store

context_class[R]

Get `Context` class.

This class maybe mutated and mixed in with custom helpers.

environment.context_class.instance_eval do
  include MyHelpers
  def asset_url; end
end
logger[RW]

Get and set `Logger` instance.

Public Instance Methods

[](*args) click to toggle source

Preferred `find_asset` shorthand.

environment['application.js']
# File lib/sprockets/base.rb, line 109
def [](*args)
  find_asset(*args)
end
attributes_for(path) click to toggle source

Internal. Return a `AssetAttributes` for `path`.

# File lib/sprockets/base.rb, line 85
def attributes_for(path)
  AssetAttributes.new(self, path)
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 39
def cache=(cache)
  expire_index!
  @cache = cache
end
content_type_of(path) click to toggle source

Internal. Return content type of `path`.

# File lib/sprockets/base.rb, line 90
def content_type_of(path)
  attributes_for(path).content_type
end
each_entry(root, &block) click to toggle source
# File lib/sprockets/base.rb, line 113
def each_entry(root, &block)
  return to_enum(__method__, root) unless block_given?
  root = Pathname.new(root) unless root.is_a?(Pathname)

  paths = []
  entries(root).sort.each do |filename|
    path = root.join(filename)
    paths << path

    if stat(path).directory?
      each_entry(path) do |subpath|
        paths << subpath
      end
    end
  end

  paths.sort_by(&:to_s).each(&block)

  nil
end
each_file() click to toggle source
# File lib/sprockets/base.rb, line 134
def each_file
  return to_enum(__method__) unless block_given?
  paths.each do |root|
    each_entry(root) do |path|
      if !stat(path).directory?
        yield path
      end
    end
  end
  nil
end
each_logical_path() click to toggle source
# File lib/sprockets/base.rb, line 146
def each_logical_path
  return to_enum(__method__) unless block_given?
  files = {}
  each_file do |filename|
    logical_path = attributes_for(filename).logical_path
    yield logical_path unless files[logical_path]
    files[logical_path] = true
  end
  nil
end
entries(pathname) click to toggle source

Works like `Dir.entries`.

Subclasses may cache this method.

# File lib/sprockets/base.rb, line 52
def entries(pathname)
  trail.entries(pathname)
end
file_digest(path, data = nil) click to toggle source

Read and compute digest of filename.

Subclasses may cache this method.

# File lib/sprockets/base.rb, line 66
def file_digest(path, data = nil)
  if stat = self.stat(path)
    # `data` maybe provided
    if data
      digest.update(data)

    # If its a file, digest the contents
    elsif stat.file?
      digest.file(path.to_s)

    # If its a directive, digest the list of filenames
    elsif stat.directory?
      contents = self.entries(path).join(',')
      digest.update(contents)
    end
  end
end
find_asset(path, options = {}) click to toggle source

Find asset by logical path or expanded path.

# File lib/sprockets/base.rb, line 95
def find_asset(path, options = {})
  pathname = Pathname.new(path)

  if pathname.absolute?
    build_asset(attributes_for(pathname).logical_path, pathname, options)
  else
    find_asset_in_path(pathname, options)
  end
end
index() click to toggle source

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

# File lib/sprockets/base.rb, line 45
def index
  raise NotImplementedError
end
inspect() click to toggle source

Pretty inspect

# File lib/sprockets/base.rb, line 158
def inspect
  "#<#{self.class}:0x#{object_id.to_s(16)} " +
    "root=#{root.to_s.inspect}, " +
    "paths=#{paths.inspect}, " +
    "digest=#{digest.to_s.inspect}" +
    ">"
end
stat(path) click to toggle source

Works like `File.stat`.

Subclasses may cache this method.

# File lib/sprockets/base.rb, line 59
def stat(path)
  trail.stat(path)
end

Protected Instance Methods

build_asset(logical_path, pathname, options) click to toggle source
# File lib/sprockets/base.rb, line 172
def build_asset(logical_path, pathname, options)
  pathname = Pathname.new(pathname)

  return unless stat(pathname)

  # If there are any processors to run on the pathname, use
  # `BundledAsset`. Otherwise use `StaticAsset` and treat is as binary.
  if attributes_for(pathname).processors.any?
    BundledAsset.new(self, logical_path, pathname, options)
  else
    StaticAsset.new(self, logical_path, pathname)
  end
end
expire_index!() click to toggle source

Clear index after mutating state. Must be implemented by the subclass.

# File lib/sprockets/base.rb, line 168
def expire_index!
  raise NotImplementedError
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.