Class: Sprockets::Exporters::Base
- Inherits:
-
Object
- Object
- Sprockets::Exporters::Base
- 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
Instance Attribute Summary collapse
-
#asset ⇒ Object
readonly
Returns the value of attribute asset.
-
#directory ⇒ Object
readonly
Returns the value of attribute directory.
-
#environment ⇒ Object
readonly
Returns the value of attribute environment.
-
#target ⇒ Object
readonly
Returns the value of attribute target.
Instance Method Summary collapse
-
#call ⇒ Object
Public: Contains logic for writing “exporting” asset to disk.
-
#initialize(asset: nil, environment: nil, directory: nil) ⇒ Base
constructor
Public: Creates new instance.
-
#setup ⇒ Object
Public: Callback that is executed after initialization.
-
#skip?(logger) ⇒ Boolean
Public: Handles logic for skipping exporter and notifying logger.
-
#write(filename = target) ⇒ Object
Public: Yields a file that can be written to with the input.
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
#asset ⇒ Object (readonly)
Returns the value of attribute asset.
10 11 12 |
# File 'lib/sprockets/exporters/base.rb', line 10 def asset @asset end |
#directory ⇒ Object (readonly)
Returns the value of attribute directory.
10 11 12 |
# File 'lib/sprockets/exporters/base.rb', line 10 def directory @directory end |
#environment ⇒ Object (readonly)
Returns the value of attribute environment.
10 11 12 |
# File 'lib/sprockets/exporters/base.rb', line 10 def environment @environment end |
#target ⇒ Object (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
#call ⇒ Object
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 |
#setup ⇒ Object
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.
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 |