Class: Hanami::Assets::Asset

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/sprockets/asset.rb

Overview

Represents a single front end asset.

Since:

  • 0.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, base_url:, sri: nil, logical_path: nil, digest_path: nil, content_type: nil, source: nil) ⇒ Asset

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 a new instance of Asset.

Since:

  • 0.1.0



76
77
78
79
80
81
82
83
84
# File 'lib/hanami/sprockets/asset.rb', line 76

def initialize(path:, base_url:, sri: nil, logical_path: nil, digest_path: nil, content_type: nil, source: nil)
  @path = path
  @base_url = base_url
  @sri = sri
  @logical_path = logical_path
  @digest_path = digest_path
  @content_type = content_type
  @source = source
end

Instance Attribute Details

#content_typeString (readonly)

Returns the asset’s content type

Returns:

  • (String)

Since:

  • 0.1.0



64
65
66
# File 'lib/hanami/sprockets/asset.rb', line 64

def content_type
  @content_type
end

#digest_pathString (readonly)

Returns the asset’s digest path (fingerprinted path)

Returns:

  • (String)

Since:

  • 0.1.0



56
57
58
# File 'lib/hanami/sprockets/asset.rb', line 56

def digest_path
  @digest_path
end

#logical_pathString (readonly)

Returns the asset’s logical path (original path without fingerprinting)

Returns:

  • (String)

Since:

  • 0.1.0



48
49
50
# File 'lib/hanami/sprockets/asset.rb', line 48

def logical_path
  @logical_path
end

#pathString (readonly)

Returns the asset’s absolute URL path.

Examples:

Asset from local dev server

asset.path # => "/assets/app.js"

Deployed asset with fingerprinted name

asset.path # => "/assets/app-28a6b886de2372ee3922fcaf3f78f2d8.js"

Returns:

  • (String)

Since:

  • 0.1.0



27
28
29
# File 'lib/hanami/sprockets/asset.rb', line 27

def path
  @path
end

#sourceString (readonly)

Returns the asset’s source content

Returns:

  • (String)

Since:

  • 0.1.0



72
73
74
# File 'lib/hanami/sprockets/asset.rb', line 72

def source
  @source
end

#sriString? (readonly) Also known as: subresource_integrity_value

Returns the asset’s subresource integrity value, or nil if none is available.

Returns:

  • (String, nil)

Since:

  • 0.1.0



40
41
42
# File 'lib/hanami/sprockets/asset.rb', line 40

def sri
  @sri
end

Instance Method Details

#to_sString

Returns the asset’s full URL

Returns:

  • (String)

See Also:

Since:

  • 0.1.0



114
115
116
# File 'lib/hanami/sprockets/asset.rb', line 114

def to_s
  url
end

#urlString

Returns the asset’s full URL.

Examples:

Asset from local dev server

asset.url # => "https://example.com/assets/app.js"

Deployed asset with fingerprinted name

asset.url # => "https://example.com/assets/app-28a6b886de2372ee3922fcaf3f78f2d8.js"

Returns:

  • (String)

Since:

  • 0.1.0



102
103
104
# File 'lib/hanami/sprockets/asset.rb', line 102

def url
  base_url.join(path)
end