123456789_123456789_123456789_123456789_123456789_

Class: Sprockets::Exporters::Base

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Inherits: Object
Defined in: lib/sprockets/exporters/base.rb

Overview

Convenience class for all exporters to inherit from

An exporter is responsible for exporting a 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.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(asset: nil, environment: nil, directory: nil) ⇒ Base

Public: Creates new instance

Initialize will be called with keyword arguments:

  • asset: An instance of Asset.

  • environment: An instance of Environment.

  • directory: String representing the target directory to write to.

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.

[ GitHub ]

  
# 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

Instance Attribute Details

#asset (readonly)

[ GitHub ]

  
# File 'lib/sprockets/exporters/base.rb', line 10

attr_reader :asset, :environment, :directory, :target

#directory (readonly)

[ GitHub ]

  
# File 'lib/sprockets/exporters/base.rb', line 10

attr_reader :asset, :environment, :directory, :target

#environment (readonly)

[ GitHub ]

  
# File 'lib/sprockets/exporters/base.rb', line 10

attr_reader :asset, :environment, :directory, :target

#target (readonly)

[ GitHub ]

  
# File 'lib/sprockets/exporters/base.rb', line 10

attr_reader :asset, :environment, :directory, :target

Instance Method Details

#call

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.

[ GitHub ]

  
# File 'lib/sprockets/exporters/base.rb', line 55

def call
  raise "Must subclass and implement call"
end

#setup

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.

[ GitHub ]

  
# File 'lib/sprockets/exporters/base.rb', line 36

def setup
end

#skip?(logger) ⇒ Boolean

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.

[ GitHub ]

  
# File 'lib/sprockets/exporters/base.rb', line 46

def skip?(logger)
  false
end

#write(filename = target)

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.

[ GitHub ]

  
# 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