class Yarrow::Assets::Manifest

Provides access to the bundle of compiled CSS and JS assets.

This is currently based on the output structure of the JSON manifest file generated by Sprockets, but this class isn’t coupled to the Sprockets API so could be used as a generic manifest reader.

Public Class Methods

new(config) click to toggle source

Initializes the manifest from a Sprockets-style JSON file.

If no assets directory is given, looks for a manifest in the main output directory.

@param config [Yarrow::Configuration]

# File lib/yarrow/assets/manifest.rb, line 23
def initialize(config)
  raise Yarrow::ConfigurationError if config.assets.nil?

  if config.assets.output_dir
    manifest_path = Pathname.new(config.assets.output_dir) + config.assets.manifest_file
  else
    manifest_path = Pathname.new(config.output_dir) + config.assets.manifest_file
  end

  if File.exists?(manifest_path)
    manifest_data = JSON.parse(File.read(manifest_path))

    @manifest_index = if manifest_data.key?('assets')
      manifest_data['assets']
    else
      manifest_data
    end
  else
    @manifest_index = {}
  end
end

Public Instance Methods

css_digest_paths() click to toggle source

Returns the list of generated CSS assets in the manifest.

@return [Array<String>]

# File lib/yarrow/assets/manifest.rb, line 99
def css_digest_paths
  select_by_extension(digest_paths, '.css')
end
css_logical_paths() click to toggle source

Returns the list of named CSS assets in the manifest.

@return [Array<String>]

# File lib/yarrow/assets/manifest.rb, line 83
def css_logical_paths
  select_by_extension(logical_paths, '.css')
end
digest_path(logical_path) click to toggle source

Returns the generated digest path to a named asset.

@param logical_path [String] @return [String]

# File lib/yarrow/assets/manifest.rb, line 59
def digest_path(logical_path)
  @manifest_index[logical_path]
end
digest_paths() click to toggle source

Returns the list of generated digest paths in the manifest.

@return [Array<String>]

# File lib/yarrow/assets/manifest.rb, line 75
def digest_paths
  @manifest_index.values
end
exists?(logical_path) click to toggle source

True if the named asset exists.

@param logical_path [String] @return [Boolean]

# File lib/yarrow/assets/manifest.rb, line 50
def exists?(logical_path)
  @manifest_index.key? logical_path
end
js_digest_paths() click to toggle source

Returns the list of generated JS assets in the manifest.

@return [Array<String>]

# File lib/yarrow/assets/manifest.rb, line 107
def js_digest_paths
  select_by_extension(digest_paths, '.js')
end
js_logical_paths() click to toggle source

Returns the list of named JS assets in the manifest.

@return [Array<String>]

# File lib/yarrow/assets/manifest.rb, line 91
def js_logical_paths
  select_by_extension(logical_paths, '.js')
end
logical_paths() click to toggle source

Returns the list of named assets in the manifest.

@return [Array<String>]

# File lib/yarrow/assets/manifest.rb, line 67
def logical_paths
  @manifest_index.keys
end

Private Instance Methods

select_by_extension(collection, ext) click to toggle source
# File lib/yarrow/assets/manifest.rb, line 113
def select_by_extension(collection, ext)
  collection.select { |asset| asset.end_with?(ext) }
end