Class: Lifer::Author

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

Overview

An author is a representation of a unique author of entries in the current project. This allows us to refer to an author’s name in an entry’s frontmatter, i.e.:

---
title: My blog post
author: Nat McCartney
---

And let that reference load in a bunch of other metadata about the author. While it’s possible to add all of this metadata at the entry level, the first entry loaded will be the source of truth for every reference back to the author. So it’s preferrable to set the author metadata in your global configuration:

# my-lifer.conf
authors:
  - name: Nat McCartney
    url: https://example.com/nat
    avatar: https://example.com/nat.png

Within a Lifer project, this allows you to do powerful things like get a list of entries by a unique author (or set of authors). It also allows you to provide author-specific URLs and avatar images in your website’s JSON Feeds.

The author’s name is used as the primary identifier. We have tried to be smart about this so that, for example, “Nat McCartney”, “nat mccartney”, and “nat-mccartney” will all load up the same author object.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, url:, avatar:, entries:) ⇒ Author

Returns a new instance of Author.



70
71
72
73
74
75
# File 'lib/lifer/author.rb', line 70

def initialize(name:, url:, avatar:, entries:)
  @name = name
  @url = url
  @avatar = avatar
  @entries = entries
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



68
69
70
# File 'lib/lifer/author.rb', line 68

def entries
  @entries
end

#nameObject (readonly)

Returns the value of attribute name.



68
69
70
# File 'lib/lifer/author.rb', line 68

def name
  @name
end

Class Method Details

.build_or_update(name:, url: nil, avatar: nil, entries: []) ⇒ Lifer::Author

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

Parameters:

  • name (String)

    The name of the author.

  • url (String) (defaults to: nil)

    A relative or absolute URL to learn more about the author at.

  • avatar (String) (defaults to: nil)

    A relative or absolute URL to an image that represents the author.

Returns:



43
44
45
46
# File 'lib/lifer/author.rb', line 43

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

Instance Method Details

#avatar(host: Lifer.setting(:global, :host)) ⇒ String

An avatar image URL that represents the author. The URL can either be relative from the website’s root or an absolute URL. If the relative or absolute URL is ambiguous, it is sanitized and this method returns nil.

this is the Lifer project’s global host.

Parameters:

  • host (String) (defaults to: Lifer.setting(:global, :host))

    The host to prefix to relative URLs. By default,

Returns:

  • (String)

    The absolute URL to the avatar image.



84
85
86
# File 'lib/lifer/author.rb', line 84

def avatar(host: Lifer.setting(:global, :host))
  Lifer::Utilities.uri_from(@avatar, host:, object_type: self.class)
end

#idString

An identifier built from the author’s name. This uses our generic handle-izer function. So a name like “Nat McCartney” becomes “nat-mccartney”.

Returns:

  • (String)

    The identifier for the author.



93
# File 'lib/lifer/author.rb', line 93

def id = (@id ||= Lifer::Utilities.handleize(name))

#url(host: Lifer.setting(:global, :host)) ⇒ String

A URL that provides more info about the author. The URL can either be relative from the website’s root or an absolute URL. If the relative or absolute URL is ambiguous, it is sanitized and this method returns nil.

this is the Lifer project’s global host.

Parameters:

  • host (String) (defaults to: Lifer.setting(:global, :host))

    The host to prefix to relative URLs. By default,

Returns:

  • (String)

    The absolute version of the URL.



102
103
104
# File 'lib/lifer/author.rb', line 102

def url(host: Lifer.setting(:global, :host))
  Lifer::Utilities.uri_from(@url, host:, object_type: self.class)
end