class Sprockets::Exporters::Base

Convenience class for all exporters to inherit from

An exporter is responsible for exporting a Sprockets::Asset to a file system. For example the Exporters::File class writes the asset to it’s destination. The Exporters::Zlib class writes a gzip copy of the asset to disk.

Attributes

asset[R]
directory[R]
environment[R]
target[R]

Public Class Methods

new(asset: nil, environment: nil, directory: nil) click to toggle source

Public: Creates new instance

Initialize will be called with keyword arguments:

These will all be stored as accessible values. In addition a target will be available which is the target directory and the asset’s digest path combined.

# File lib/sprockets/exporters/base.rb, line 24
def initialize(asset: nil, environment: nil, directory: nil)
  @asset       = asset
  @environment = environment
  @directory   = directory
  @target      = ::File.join(directory, asset.digest_path)
  setup
end

Public Instance Methods

call() click to toggle source

Public: Contains logic for writing “exporting” asset to disk

If the exporter is not skipped it then Sprockets will execute it’s ‘call` method. This method takes no arguments and should only use elements passed in via initialize or stored in `setup`.

# File lib/sprockets/exporters/base.rb, line 55
def call
  raise "Must subclass and implement call"
end
setup() click to toggle source

Public: Callback that is executed after initialization

Any setup that needs to be done can be performed in the setup method. It will be called immediately after initialization.

# File lib/sprockets/exporters/base.rb, line 36
def setup
end
skip?(logger) click to toggle source

Public: Handles logic for skipping exporter and notifying logger

The ‘skip?` will be called before anything will be written. If `skip?` returns truthy it will not continue. This method takes a `logger` that responds to debug and info. The `skip?` method is the only place expected to write to a logger, any other messages may produce jumbled logs.

# File lib/sprockets/exporters/base.rb, line 46
def skip?(logger)
  false
end
write(filename = target) { |f| ... } click to toggle source

Public: Yields a file that can be written to with the input

‘filename`. Defaults to the `target`. Method is safe to use in forked or threaded environments.

# File lib/sprockets/exporters/base.rb, line 63
def write(filename = target)
  FileUtils.mkdir_p File.dirname(filename)
  PathUtils.atomic_write(filename) do |f|
    yield f
  end
end