Class: Lifer::Author
- Inherits:
-
Object
- Object
- Lifer::Author
- 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
-
#entries ⇒ Object
readonly
Returns the value of attribute entries.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
-
.build_or_update(name:, url: nil, avatar: nil, entries: []) ⇒ Lifer::Author
Builds or updates a Lifer author.
Instance Method Summary collapse
-
#avatar(host: Lifer.setting(:global, :host)) ⇒ String
An avatar image URL that represents the author.
-
#id ⇒ String
An identifier built from the author’s name.
-
#initialize(name:, url:, avatar:, entries:) ⇒ Author
constructor
A new instance of Author.
-
#url(host: Lifer.setting(:global, :host)) ⇒ String
A URL that provides more info about the author.
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
#entries ⇒ Object (readonly)
Returns the value of attribute entries.
68 69 70 |
# File 'lib/lifer/author.rb', line 68 def entries @entries end |
#name ⇒ Object (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.
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.
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 |
#id ⇒ String
An identifier built from the author’s name. This uses our generic handle-izer function. So a name like “Nat McCartney” becomes “nat-mccartney”.
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.
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 |