Module: Cybertrain::Assets
- Defined in:
- cybertrain/assets.rb
Overview
The digested asset paths and the import map gen/assets.rb installs: read by the asset helpers and the static file server.
Defined Under Namespace
Classes: UnknownAsset
Class Method Summary collapse
-
.build_digest ⇒ String
A digest gen writes into gen/assets.rb: of the digested assets and the import map, and in a binary
cybertrain buildmade, of the views and components embedded in it too. -
.digest_path(logical) ⇒ String
The digested path under /assets, “” for a name that is not an asset.
-
.importmap_json ⇒ String
The import map of config/importmap.rb as compact JSON (
{"imports":{...}}),</written\u003c/so it can sit inside a -
.install(resolver, importmap_json, preload_paths, build_digest = "") ⇒ nil
Installs gen/assets.rb’s tables; gen/assets.rb calls it when the application loads.
-
.kept_file?(file_name) ⇒ Boolean
Whether file_name (the path below /assets) carries a digest gen wrote: the current one of gen/assets.rb, or one .manifest keeps.
-
.load_kept(public_root) ⇒ nil
Reads public_root/assets/.manifest (written by
spin run gen): the digests it keeps for each logical path, so that a page served before a deploy still fetches its files as immutable. -
.path(logical) ⇒ String
The digested path of a logical one, what asset_path and the tag helpers write.
-
.preload_paths ⇒ Array<String>
The paths (and URLs) of the pins to preload, in config/importmap.rb’s order.
-
.raise_on_unknown ⇒ Boolean
Whether an unknown name raises.
-
.raise_on_unknown=(value) ⇒ Object
Whether an unknown name raises (true, development and test) or is logged once and written undigested (false, production).
Class Method Details
.build_digest ⇒ String
A digest gen writes into gen/assets.rb: of the digested assets and
the import map, and in a binary cybertrain build made, of the views
and components embedded in it too. It changes when a deploy changes
what a page renders, so Controller#fresh_when mixes it into every
ETag; "" until gen/assets.rb installs one.
82 83 84 |
# File 'cybertrain/assets.rb', line 82 def self.build_digest @build_digest end |
.digest_path(logical) ⇒ String
Returns the digested path under /assets, “” for a name that is not an asset.
92 93 94 |
# File 'cybertrain/assets.rb', line 92 def self.digest_path(logical) @resolver.call(logical) end |
.importmap_json ⇒ String
Returns the import map of config/importmap.rb as compact
JSON ({"imports":{...}}), </ written \u003c/ so it can sit
inside a
210 211 212 |
# File 'cybertrain/assets.rb', line 210 def self.importmap_json @importmap_json end |
.install(resolver, importmap_json, preload_paths, build_digest = "") ⇒ nil
Installs gen/assets.rb’s tables; gen/assets.rb calls it when the application loads.
67 68 69 70 71 72 73 |
# File 'cybertrain/assets.rb', line 67 def self.install(resolver, importmap_json, preload_paths, build_digest = "") @resolver = resolver @importmap_json = importmap_json @preload_paths = preload_paths @build_digest = build_digest nil end |
.kept_file?(file_name) ⇒ Boolean
Whether file_name (the path below /assets) carries a digest gen wrote: the current one of gen/assets.rb, or one .manifest keeps. Only such a file is served immutable (Static).
196 197 198 199 200 201 202 203 204 |
# File 'cybertrain/assets.rb', line 196 def self.kept_file?(file_name) name = file_name.to_s return true if @kept.key?(name) logical = AssetManifest.logical_of(name) return false if logical.empty? @resolver.call(logical) == "/assets/#{name}" end |
.load_kept(public_root) ⇒ nil
Reads public_root/assets/.manifest (written by spin run gen): the
digests it keeps for each logical path, so that a page served before
a deploy still fetches its files as immutable. Application#boot calls
it once; a missing file keeps none. A malformed line is skipped
whole, as gen skips it (AssetManifest.read_kept).
158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'cybertrain/assets.rb', line 158 def self.load_kept(public_root) kept = { "" => true } kept.delete("") file = "#{public_root}/assets/#{AssetManifest::FILE}" if File.file?(file) lines = AssetManifest.read_kept(File.read(file)) lines.each_key do |logical| lines[logical].each { |digest| kept[AssetManifest.digested_name(logical, digest)] = true } end end @kept = kept nil end |
.path(logical) ⇒ String
The digested path of a logical one, what asset_path and the tag helpers write.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'cybertrain/assets.rb', line 105 def self.path(logical) name = logical.to_s digested = @resolver.call(name) return digested unless digested.empty? if @raise_on_unknown = "asset \"#{name}\" is not in app/assets, app/javascript or vendor/javascript (run spin run gen)" raise UnknownAsset, end # name.inspect: a name from a request cannot write a line of its own. log_unknown(name, "asset #{name.inspect} is not in app/assets, app/javascript or vendor/javascript (run spin run gen)") "/assets/#{name}" end |
.preload_paths ⇒ Array<String>
Returns the paths (and URLs) of the pins to preload, in config/importmap.rb’s order.
217 218 219 |
# File 'cybertrain/assets.rb', line 217 def self.preload_paths @preload_paths end |
.raise_on_unknown ⇒ Boolean
Returns whether an unknown name raises.
146 147 148 |
# File 'cybertrain/assets.rb', line 146 def self.raise_on_unknown @raise_on_unknown end |
.raise_on_unknown=(value) ⇒ Object
Whether an unknown name raises (true, development and test) or is logged once and written undigested (false, production).
140 141 142 |
# File 'cybertrain/assets.rb', line 140 def self.raise_on_unknown=(value) @raise_on_unknown = value end |