Class: Propshaft::Manifest
- Inherits:
-
Object
- Object
- Propshaft::Manifest
- Defined in:
- lib/propshaft/manifest.rb
Overview
Manages the manifest file that maps logical asset paths to their digested counterparts.
The manifest is used to track assets that have been processed and digested, storing their logical paths, digested paths, and optional integrity hashes.
Defined Under Namespace
Classes: ManifestEntry
Class Method Summary collapse
-
.from_path(manifest_path) ⇒ Object
Creates a new Manifest instance from a manifest file.
Instance Method Summary collapse
-
#[](logical_path) ⇒ Object
Retrieves a manifest entry by its logical path.
-
#delete(logical_path) ⇒ Object
Removes a manifest entry by its logical path.
-
#initialize(integrity_hash_algorithm: nil) ⇒ Manifest
constructor
Creates a new Manifest instance.
-
#push(entry) ⇒ Object
(also: #<<)
Adds a manifest entry to the manifest.
-
#push_asset(asset) ⇒ Object
Adds an asset to the manifest.
-
#to_json ⇒ Object
Converts the manifest to JSON format.
-
#transform_values(&block) ⇒ Object
Transforms the values of all manifest entries using the given block.
Constructor Details
#initialize(integrity_hash_algorithm: nil) ⇒ Manifest
Creates a new Manifest instance.
Parameters
-
integrity_hash_algorithm- The algorithm to use for generating integrity hashes (e.g., ‘sha256’, ‘sha384’, ‘sha512’). Ifnil, integrity hashes will not be generated.
82 83 84 85 |
# File 'lib/propshaft/manifest.rb', line 82 def initialize(integrity_hash_algorithm: nil) @integrity_hash_algorithm = integrity_hash_algorithm @entries = {} end |
Class Method Details
.from_path(manifest_path) ⇒ Object
Creates a new Manifest instance from a manifest file.
Reads and parses a manifest file, supporting both the current format (with digested_path and integrity keys) and the legacy format (simple string values for backwards compatibility).
Parameters
-
manifest_path- The path to the manifest file
Returns
A new manifest instance populated with entries from the file.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/propshaft/manifest.rb', line 50 def from_path(manifest_path) manifest = Manifest.new serialized_manifest = JSON.parse(manifest_path.read, symbolize_names: false) serialized_manifest.each_pair do |key, value| # Compatibility mode to be able to # read the old "simple manifest" format digested_path, integrity = if value.is_a?(String) [value, nil] else [value["digested_path"], value["integrity"]] end entry = ManifestEntry.new( logical_path: key, digested_path: digested_path, integrity: integrity ) manifest.push(entry) end manifest end |
Instance Method Details
#[](logical_path) ⇒ Object
Retrieves a manifest entry by its logical path.
Parameters
-
logical_path- The logical path of the asset to retrieve
Returns
The manifest entry, or nil if not found.
133 134 135 |
# File 'lib/propshaft/manifest.rb', line 133 def [](logical_path) @entries[logical_path] end |
#delete(logical_path) ⇒ Object
Removes a manifest entry by its logical path.
Parameters
-
logical_path- The logical path of the asset to remove
Returns
The removed manifest entry, or nil if not found.
146 147 148 |
# File 'lib/propshaft/manifest.rb', line 146 def delete(logical_path) @entries.delete(logical_path) end |
#push(entry) ⇒ Object Also known as: <<
Adds a manifest entry to the manifest.
Parameters
-
entry- The manifest entry to add
Returns
The entry that was added.
119 120 121 |
# File 'lib/propshaft/manifest.rb', line 119 def push(entry) @entries[entry.logical_path] = entry end |
#push_asset(asset) ⇒ Object
Adds an asset to the manifest.
Creates a manifest entry from the given asset and adds it to the manifest. The entry will include the asset’s logical path, digested path, and optionally an integrity hash if an integrity hash algorithm is configured.
Parameters
-
asset- The asset to add to the manifest
Returns
The manifest entry that was added.
100 101 102 103 104 105 106 107 108 |
# File 'lib/propshaft/manifest.rb', line 100 def push_asset(asset) entry = ManifestEntry.new( logical_path: asset.logical_path.to_s, digested_path: asset.digested_path.to_s, integrity: integrity_hash_algorithm && asset.integrity(hash_algorithm: integrity_hash_algorithm) ) push(entry) end |
#to_json ⇒ Object
Converts the manifest to JSON format.
The JSON representation maps logical paths to hash representations of manifest entries, containing digested_path and integrity information.
Returns
The JSON representation of the manifest.
158 159 160 161 162 |
# File 'lib/propshaft/manifest.rb', line 158 def to_json @entries.transform_values do |manifest_entry| manifest_entry.to_h end.to_json end |
#transform_values(&block) ⇒ Object
Transforms the values of all manifest entries using the given block.
This method is useful for applying transformations to all manifest entries while preserving the logical path keys.
Parameters
-
block- A block that will receive each manifest entry
Returns
A new hash with the same keys but transformed values.
176 177 178 |
# File 'lib/propshaft/manifest.rb', line 176 def transform_values(&block) @entries.transform_values(&block) end |