Class: WaxTasks::Collection

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

Overview

Parent class representing a Jekyll collection that cannot be created directly. Only child classes (IiifCollection, LunrCollection, PagemasterCollection) can be initialized.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, site) ⇒ Collection

Creates a new collection with name @name given site config @site

Parameters:

  • name (String)

    the name of the collection in site:collections

  • site (Hash)

    the site config



25
26
27
28
29
30
31
32
# File 'lib/wax_tasks/collection.rb', line 25

def initialize(name, site)
  @name     = name
  @site     = site
  @config   = collection_config
  @page_dir = Utils.make_path(@site[:source_dir],
                              @site[:collections_dir],
                              "_#{@name}")
end

Instance Attribute Details

#configHash

the collection config within site config

Returns:

  • (Hash)

    the current value of config



11
12
13
# File 'lib/wax_tasks/collection.rb', line 11

def config
  @config
end

#nameString

the name of the collection in site:collections

Returns:

  • (String)

    the current value of name



11
12
13
# File 'lib/wax_tasks/collection.rb', line 11

def name
  @name
end

#page_dirString

the directory path for generated collection pages

Returns:

  • (String)

    the current value of page_dir



11
12
13
# File 'lib/wax_tasks/collection.rb', line 11

def page_dir
  @page_dir
end

#siteHash

the site config

Returns:

  • (Hash)

    the current value of site



11
12
13
# File 'lib/wax_tasks/collection.rb', line 11

def site
  @site
end

Class Method Details

.inheritedObject

This method ensures child classes can be instantiated though Collection.new cannot be.



17
18
19
# File 'lib/wax_tasks/collection.rb', line 17

def self.inherited(*)
  public_class_method :new
end

Instance Method Details

#collection_configHash

Finds the collection config within the site config

Returns:

  • (Hash)

    the config for the collection



37
38
39
40
41
# File 'lib/wax_tasks/collection.rb', line 37

def collection_config
  @site[:collections].fetch(@name)
rescue StandardError => e
  raise Error::InvalidCollection, "Cannot load collection config for #{@name}.\n#{e}"
end

#ingest_file(source) ⇒ Array

Ingests the collection source data as an Array of Hashes

Parameters:

  • source (String)

    the path to the CSV, JSON, or YAML source file

Returns:

  • (Array)

    the collection data

Raises:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/wax_tasks/collection.rb', line 47

def ingest_file(source)
  raise Error::MissingSource, "Cannot find #{source}" unless File.exist? source

  data = case File.extname(source)
         when '.csv'
           WaxTasks::Utils.validate_csv(source)
         when '.json'
           WaxTasks::Utils.validate_json(source)
         when /\.ya?ml/
           WaxTasks::Utils.validate_yaml(source)
         else
           raise Error::InvalidSource, "Can't load #{File.extname(source)} files. Culprit: #{source}"
         end

  WaxTasks::Utils.assert_pids(data)
  WaxTasks::Utils.assert_unique(data)
end