Class: Lifer::Collection

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

Overview

A collection collects entries. Every entry can only be included in a single collection. Collections let the user group entries together into logical units. For example, if the user wants to present blog posts in one way and wiki pages in a different way, it would make sense to create separate “blog” and “wiki” collections.

Each collection can have its own feeds and settings. The only special collection is the “root” collection, which is where all entries that don’t fall into other collections end up.

Direct Known Subclasses

Selection

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/lifer/collection.rb', line 12

def name
  @name
end

Class Method Details

.generate(name:, directory:) ⇒ Lifer::Collection

Generate a new collection.

Parameters:

  • name (String)

    The name of the new collection.

  • directory (String)

    The absolute path to the root directory of the collection.

Returns:



21
22
23
24
25
# File 'lib/lifer/collection.rb', line 21

def generate(name:, directory:)
  collection = new(name: name, directory:)
  build_collection_entries!(collection, directory:)
  collection
end

Instance Method Details

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

Each collection has a collection of entries. An entry only belongs to one collection.

Returns:



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/lifer/collection.rb', line 53

def entries(order: :latest)
  cached_entries_variable = "@collection_entries_#{order}"
  instance_variable_get(cached_entries_variable) ||
    instance_variable_set(
      cached_entries_variable,
      case order
      when :latest
        @entries_collection.sort_by { |entry| entry.published_at }.reverse
      when :oldest
        @entries_collection.sort_by { |entry| entry.published_at }
      end
    )
end

#layout_fileString

To allow for flexible configuration, a layout file may be set by users to either an absolute path or a path relative to the configuration file’s location. This method, though, always returns the absolute path.

Returns:

  • (String)

    The absolute path to the collection’s layout file.



72
73
74
75
76
77
78
79
# File 'lib/lifer/collection.rb', line 72

def layout_file
  return setting :layout_file if setting(:layout_file).include?(Lifer.gem_root)
  return setting :layout_file if setting(:layout_file).include?(Lifer.root)

  config_directory = File.dirname Lifer.config_file

  [config_directory, setting(:layout_file)].join "/"
end

#root?boolean

Check whether the current collection is the root collection.

Returns:

  • (boolean)


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

def root?
  name == :root
end

#setting(*name, strict: false) ⇒ String, Nil

Gets a Lifer setting, scoped to the current collection.

Parameters:

  • name (*Symbol)

    A list of symbols that map to a nested Lifer setting (for the current collection).

Returns:

  • (String, Nil)

    The setting as set in the Lifer project’s configuration file.



94
95
96
# File 'lib/lifer/collection.rb', line 94

def setting(*name, strict: false)
  Lifer.setting *name, collection: self, strict: strict
end