Class: Lifer::Tag
- Inherits:
-
Object
- Object
- Lifer::Tag
- 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
-
#name ⇒ Object
Returns the value of attribute name.
Class Method Summary collapse
-
.build_or_update(name:, entries: []) ⇒ Lifer:Tag
Builds or updates a Lifer tag.
Instance Method Summary collapse
-
#entries(order: :latest) ⇒ Array<Lifer::Entry>
Returns the tag’s associated entries in order.
-
#initialize(name:, entries:) ⇒ Tag
constructor
A new instance of Tag.
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
#name ⇒ Object
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.
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.
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 |