Class: Sprockets::Exporters::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/sprockets/exporters/base.rb

Overview

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.

Direct Known Subclasses

FileExporter, ZlibExporter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(asset: nil, environment: nil, directory: nil) ⇒ Base

Public: Creates new instance

Initialize will be called with keyword arguments:

  • asset: An instance of Sprockets::Asset.

  • environment: An instance of Sprockets::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.



24
25
26
27
28
29
30
# 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

#assetObject (readonly)

Returns the value of attribute asset.



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

def asset
  @asset
end

#directoryObject (readonly)

Returns the value of attribute directory.



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

def directory
  @directory
end

#environmentObject (readonly)

Returns the value of attribute environment.



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

def environment
  @environment
end

#targetObject (readonly)

Returns the value of attribute target.



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

def target
  @target
end

Instance Method Details

#callObject

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`.



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

def call
  raise "Must subclass and implement call"
end

#setupObject

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.



36
37
# 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.

Returns:

  • (Boolean)


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

def skip?(logger)
  false
end

#write(filename = target) ⇒ Object

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.



63
64
65
66
67
68
# 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