Class: Lifer::Tag

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

Overview

A tag is a way to categorize entries. You’ve likely encountered tags in other software before. In Lifer, tags are sort of the inverse of collections. It’s a nice way to associate entries across many collections.

Because tags are used to link entries, we definitely do not want duplicate tags. So the only way to build or retrieve tags is via the ‘.build_or_update` class method, which helps us responsibly manage the global tag manifest.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, entries:) ⇒ Tag

Returns a new instance of Tag.



46
47
48
49
# File 'lib/lifer/tag.rb', line 46

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

Instance Attribute Details

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.build_or_update(name:, entries: []) ⇒ Lifer:Tag

Builds or updates a Lifer tag. On update, its list of entries gets freshened.

Parameters:

  • name (String)

    The name of the tag.

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

    A list of entries that should be associated with the tag. This parameter is not a true writer, in that if the tag already exists, old entry associations won’t be removed– only appended to.

Returns:

  • (Lifer:Tag)

    The new or updated tag.



22
23
24
# File 'lib/lifer/tag.rb', line 22

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

Instance Method Details

#entries(order: :latest) ⇒ Array<Lifer::Entry>

Returns the tag’s associated entries in order.

Parameters:

  • order (Symbol) (defaults to: :latest)

    Either :latest (descending) or :oldest (ascending).

Returns:

  • (Array<Lifer::Entry>)

    The entries for the current tag.



55
56
57
58
59
60
61
62
# File 'lib/lifer/tag.rb', line 55

def entries(order: :latest)
  case order
  when :latest
    @entries.sort_by { |entry| entry.published_at }.reverse
  when :oldest
    @entries.sort_by { |entry| entry.published_at }
  end
end