Class: Hanami::Assets

Inherits:
Object
  • Object
show all
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

Since:

  • 0.1.0

Defined Under Namespace

Classes: Asset, AssetMissingError, BaseUrl, Config, Error, ManifestMissingError

Constant Summary collapse

VERSION =

Defines the version

Since:

  • 0.1.0

"2.2.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:, root:) ⇒ Assets

Returns a new instance of Assets.

Since:

  • 2.1.0



61
62
63
64
# File 'lib/hanami/assets.rb', line 61

def initialize(config:, root:)
  @config = config
  @root = Pathname(root)
end

Instance Attribute Details

#configObject (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.

Since:

  • 2.1.0



53
54
55
# File 'lib/hanami/assets.rb', line 53

def config
  @config
end

#rootObject (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.

Since:

  • 2.1.0



57
58
59
# File 'lib/hanami/assets.rb', line 57

def root
  @root
end

Class Method Details

.gem_loaderObject

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.

Since:

  • 2.1.0



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.expand_path("..", __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).

Since:

  • 2.1.0



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.

Returns:

Raises:

  • AssetMissingError if no asset can be found at the path

Since:

  • 2.1.0



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.

Returns:

  • (Boolean)

Since:

  • 2.1.0



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.

Returns:

  • (Boolean)

Since:

  • 2.1.0



96
97
98
# File 'lib/hanami/assets.rb', line 96

def subresource_integrity?
  config.subresource_integrity.any?
end