Class: Lifer::Asset

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url:, entries:) ⇒ Asset

Returns a new instance of Asset.



51
52
53
54
55
56
57
58
# File 'lib/lifer/asset.rb', line 51

def initialize(url:, entries:)
  normalized_url = Lifer::Utilities.uri_from url,
    host: self.class.default_host,
    object_type: self

  @url = normalized_url
  @entries = entries
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



49
50
51
# File 'lib/lifer/asset.rb', line 49

def entries
  @entries
end

#url(host: self.class.default_host) ⇒ String (readonly)

Gets the current URL. If given a host, the asset’s true host will be replaced with the given host.

Parameters:

  • host (String) (defaults to: self.class.default_host)

    A host URL. (Default: The configured global host URL.)

Returns:

  • (String)

    The URL to the current asset.



75
76
77
# File 'lib/lifer/asset.rb', line 75

def url
  @url
end

Class Method Details

.build_or_update(url: nil, entries: []) ⇒ Lifer::Asset

Builds or updates a Lifer asset. On update, this list of an asset’s entries would get freshened.

Parameters:

  • url (String) (defaults to: nil)

    An absolute URL or path relative to the host root.

  • entries (Array<Lifer::Entry>) (defaults to: [])

    An array of entries that the asset belongs to.

Returns:



17
18
19
# File 'lib/lifer/asset.rb', line 17

def build_or_update(url: nil, entries: [])
  update(url:, entries:) || build(url:, entries:)
end

.default_hostString

The default host for all assets.

Returns:

  • (String)

    A URL to a host. (Default: the configured global host.)



24
# File 'lib/lifer/asset.rb', line 24

def default_host = Lifer.setting(:global, :host)

Instance Method Details

#match?(url:, host: self.class.default_host) ⇒ boolean

Checks whether a given URL matches the current asset’s URL.

Parameters:

  • url (String)

    A URL.

  • host (String) (defaults to: self.class.default_host)

    The host URL. (Default: The configured global host URL.)

Returns:

  • (boolean)

    Whether the given URL matches the object’s URL.



66
67
68
# File 'lib/lifer/asset.rb', line 66

def match?(url:, host: self.class.default_host)
  @url == Lifer::Utilities.uri_from(url, host:, object_type: self.class)
end