Class: Hanami::Assets
- Inherits:
-
Object
- Object
- Hanami::Assets
- Defined in:
- lib/hanami/assets.rb,
lib/hanami/assets/asset.rb,
lib/hanami/assets/config.rb,
lib/hanami/assets/errors.rb,
lib/hanami/assets/version.rb,
lib/hanami/assets/base_url.rb
Overview
Assets management for Ruby web applications
Defined Under Namespace
Classes: Asset, AssetMissingError, BaseUrl, Config, Error, ManifestMissingError
Constant Summary collapse
- VERSION =
Defines the version
"2.2.0"
Instance Attribute Summary collapse
- #config ⇒ Object readonly private
- #root ⇒ Object readonly private
Class Method Summary collapse
- .gem_loader ⇒ Object private
-
.public_assets_dir(slice) ⇒ Object
private
Returns the directory (under ‘public/assets/`) to be used for storing a slice’s compiled assets.
Instance Method Summary collapse
-
#[](path) ⇒ Hanami::Assets::Asset
Returns the asset at the given path.
-
#crossorigin?(source_path) ⇒ Boolean
Returns true if the given source path is a cross-origin request.
-
#initialize(config:, root:) ⇒ Assets
constructor
A new instance of Assets.
-
#subresource_integrity? ⇒ Boolean
Returns true if subresource integrity is configured.
Constructor Details
Instance Attribute Details
#config ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
53 54 55 |
# File 'lib/hanami/assets.rb', line 53 def config @config end |
#root ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
57 58 59 |
# File 'lib/hanami/assets.rb', line 57 def root @root end |
Class Method Details
.gem_loader ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/hanami/assets.rb', line 14 def self.gem_loader @gem_loader ||= Zeitwerk::Loader.new.tap do |loader| root = File.("..", __dir__) loader.tag = "hanami-assets" loader.push_dir(root) loader.ignore( "#{root}/hanami-assets.rb", "#{root}/hanami/assets/version.rb", "#{root}/hanami/assets/errors.rb" ) loader.inflector = Zeitwerk::GemInflector.new("#{root}/hanami-assets.rb") end end |
.public_assets_dir(slice) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the directory (under ‘public/assets/`) to be used for storing a slice’s compiled assets.
This is shared logic used by both Hanami (for the assets provider) and Hanami::CLI (for the assets commands).
40 41 42 43 44 |
# File 'lib/hanami/assets.rb', line 40 def self.public_assets_dir(slice) return nil if slice.app.eql?(slice) slice.slice_name.to_s.split("/").map { |name| "_#{name}" }.join("/") end |
Instance Method Details
#[](path) ⇒ Hanami::Assets::Asset
Returns the asset at the given path.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/hanami/assets.rb', line 74 def [](path) asset_attrs = manifest .fetch(path) { raise AssetMissingError.new(path) } .transform_keys(&:to_sym) .tap { |attrs| # The `url` attribute we receive from the manifest is actually a path; rename it as such # so our `Asset` attributes make more sense on their own. attrs[:path] = attrs.delete(:url) } Asset.new( **asset_attrs, base_url: config.base_url ) end |
#crossorigin?(source_path) ⇒ Boolean
Returns true if the given source path is a cross-origin request.
106 107 108 |
# File 'lib/hanami/assets.rb', line 106 def crossorigin?(source_path) config.crossorigin?(source_path) end |
#subresource_integrity? ⇒ Boolean
Returns true if subresource integrity is configured.
96 97 98 |
# File 'lib/hanami/assets.rb', line 96 def subresource_integrity? config.subresource_integrity.any? end |