Class: WaxTasks::PagemasterCollection

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

Overview

A Jekyll collection with a data source file that can generate markdown pages from that data.

Instance Attribute Summary collapse

Attributes inherited from Collection

#config, #name, #page_dir, #site

Instance Method Summary collapse

Methods inherited from Collection

#collection_config, #ingest_file, inherited

Constructor Details

#initialize(name, site) ⇒ PagemasterCollection

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



13
14
15
16
17
18
19
20
# File 'lib/wax_tasks/pagemaster_collection.rb', line 13

def initialize(name, site)
  super(name, site)

  @source   = source_path
  @layout   = assert_layout
  @data     = ingest_file(@source)
  @ordered  = @config.fetch('keep_order', false)
end

Instance Attribute Details

#dataArray

array of hashes representing the ingested data file

Returns:

  • (Array)

    the current value of data



9
10
11
# File 'lib/wax_tasks/pagemaster_collection.rb', line 9

def data
  @data
end

#layoutString

the Jekyll layout to be used by the generated pages

Returns:

  • (String)

    the current value of layout



9
10
11
# File 'lib/wax_tasks/pagemaster_collection.rb', line 9

def layout
  @layout
end

#orderedBoolean

whether/not the order of items should be preserved

Returns:

  • (Boolean)

    the current value of ordered



9
10
11
# File 'lib/wax_tasks/pagemaster_collection.rb', line 9

def ordered
  @ordered
end

#sourceString

the path to the data source file

Returns:

  • (String)

    the current value of source



9
10
11
# File 'lib/wax_tasks/pagemaster_collection.rb', line 9

def source
  @source
end

Instance Method Details

#assert_layoutString

Confirms + requires ‘layout` value in the collection @config

Returns:

  • (String)

    the Jekyll layout to be used by the generated pages

Raises:



33
34
35
36
# File 'lib/wax_tasks/pagemaster_collection.rb', line 33

def assert_layout
  raise WaxTasks::Error::MissingLayout, "Missing collection layout in _config.yml for #{@name}" unless @config.key? 'layout'
  @config['layout']
end

#generate_pagesArray

Writes markdown pages from the ingested data to @page_dir with layout, permalink, and order info added (if applicable)

Returns:

  • (Array)

    a copy of the pages as hashes, for testing



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/wax_tasks/pagemaster_collection.rb', line 42

def generate_pages
  FileUtils.mkdir_p(@page_dir)
  pages = []
  @data.each_with_index do |item, idx|
    page_slug         = Utils.slug(item.fetch('pid'))
    path              = "#{@page_dir}/#{page_slug}.md"
    item['permalink'] = "/#{@name}/#{page_slug}#{@site[:permalink]}"
    item['layout']    = @layout
    item['order']     = padded_int(idx, @data.length) if @ordered
    pages << item
    next "#{page_slug}.md already exits. Skipping." if File.exist?(path)
    File.open(path, 'w') { |f| f.write("#{item.to_yaml}---") }
  end
  puts "#{@data.length} pages were generated to #{@page_dir} directory.".cyan
  pages
end

#padded_int(idx, max_idx) ⇒ Integer

Constructs the order variable for each page (if the collection needs to preserve the order of items from the file)

Returns:

  • (Integer)

    the order if the item padded with ‘0’s for sorting



63
64
65
# File 'lib/wax_tasks/pagemaster_collection.rb', line 63

def padded_int(idx, max_idx)
  idx.to_s.rjust(Math.log10(max_idx).to_i + 1, '0')
end

#source_pathString

Constructs the path to the data source file

Returns:

  • (String)

    the path to the data source file

Raises:



25
26
27
28
# File 'lib/wax_tasks/pagemaster_collection.rb', line 25

def source_path
  raise WaxTasks::Error::MissingSource, "Missing collection source in _config.yml for #{@name}" unless @config.key? 'source'
  WaxTasks::Utils.make_path(@site[:source_dir], '_data', @config['source'])
end