Class: WaxTasks::PagemasterCollection
- Inherits:
-
Collection
- Object
- Collection
- WaxTasks::PagemasterCollection
- 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
-
#data ⇒ Array
array of hashes representing the ingested data file.
-
#layout ⇒ String
the Jekyll layout to be used by the generated pages.
-
#ordered ⇒ Boolean
whether/not the order of items should be preserved.
-
#source ⇒ String
the path to the data source file.
Attributes inherited from Collection
#config, #name, #page_dir, #site
Instance Method Summary collapse
-
#assert_layout ⇒ String
Confirms + requires ‘layout` value in the collection @config.
-
#generate_pages ⇒ Array
Writes markdown pages from the ingested data to @page_dir with layout, permalink, and order info added (if applicable).
-
#initialize(name, site) ⇒ PagemasterCollection
constructor
Creates a new PagemasterCollection with name @name given site config @site.
-
#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).
-
#source_path ⇒ String
Constructs the path to the data source file.
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
#data ⇒ Array
array of hashes representing the ingested data file
9 10 11 |
# File 'lib/wax_tasks/pagemaster_collection.rb', line 9 def data @data end |
#layout ⇒ String
the Jekyll layout to be used by the generated pages
9 10 11 |
# File 'lib/wax_tasks/pagemaster_collection.rb', line 9 def layout @layout end |
#ordered ⇒ Boolean
whether/not the order of items should be preserved
9 10 11 |
# File 'lib/wax_tasks/pagemaster_collection.rb', line 9 def ordered @ordered end |
#source ⇒ String
the path to the data source file
9 10 11 |
# File 'lib/wax_tasks/pagemaster_collection.rb', line 9 def source @source end |
Instance Method Details
#assert_layout ⇒ String
Confirms + requires ‘layout` value in the collection @config
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_pages ⇒ Array
Writes markdown pages from the ingested data to @page_dir with layout, permalink, and order info added (if applicable)
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)
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_path ⇒ String
Constructs the path to the data source file
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 |