Class: Pagemaster::Collection
- Inherits:
-
Object
- Object
- Pagemaster::Collection
- Defined in:
- lib/pagemaster/collection.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#dir ⇒ Object
readonly
Returns the value of attribute dir.
-
#id_key ⇒ Object
readonly
Returns the value of attribute id_key.
-
#layout ⇒ Object
readonly
Returns the value of attribute layout.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Instance Method Summary collapse
- #fetch(key) ⇒ Object
- #generate_pages(opts, collections_dir, source_dir) ⇒ Object
- #ingest_source ⇒ Object
-
#initialize(name, config) ⇒ Collection
constructor
A new instance of Collection.
- #overwrite_pages ⇒ Object
- #slug(str) ⇒ Object
- #validate_data ⇒ Object
Constructor Details
#initialize(name, config) ⇒ Collection
Returns a new instance of Collection.
11 12 13 14 15 16 |
# File 'lib/pagemaster/collection.rb', line 11 def initialize(name, config) @name = name @config = config @source = fetch 'source' @id_key = fetch 'id_key' end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
7 8 9 |
# File 'lib/pagemaster/collection.rb', line 7 def data @data end |
#dir ⇒ Object (readonly)
Returns the value of attribute dir.
7 8 9 |
# File 'lib/pagemaster/collection.rb', line 7 def dir @dir end |
#id_key ⇒ Object (readonly)
Returns the value of attribute id_key.
7 8 9 |
# File 'lib/pagemaster/collection.rb', line 7 def id_key @id_key end |
#layout ⇒ Object (readonly)
Returns the value of attribute layout.
7 8 9 |
# File 'lib/pagemaster/collection.rb', line 7 def layout @layout end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
7 8 9 |
# File 'lib/pagemaster/collection.rb', line 7 def source @source end |
Instance Method Details
#fetch(key) ⇒ Object
20 21 22 23 24 |
# File 'lib/pagemaster/collection.rb', line 20 def fetch(key) raise Error::InvalidCollection unless @config.key? key @config.dig key end |
#generate_pages(opts, collections_dir, source_dir) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/pagemaster/collection.rb', line 67 def generate_pages(opts, collections_dir, source_dir) @opts = opts @dir = File.join [source_dir, collections_dir, "_#{@name}"].compact overwrite_pages if @opts.fetch :force, false FileUtils.mkdir_p @dir @data = ingest_source validate_data @data.map do |d| path = "#{@dir}/#{slug d[@id_key]}.md" d['layout'] = @config['layout'] if @config.key? 'layout' if File.exist? path puts Rainbow("#{path} already exits. Skipping.").cyan else File.open(path, 'w') { |f| f.write("#{d.to_yaml}---") } end path end end |
#ingest_source ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/pagemaster/collection.rb', line 28 def ingest_source file = "_data/#{@source}" raise Error::InvalidSource, "Cannot find source file #{file}" unless File.exist? file case File.extname file when '.csv' CSV.read(file, headers: true).map(&:to_hash) when '.json' JSON.parse(File.read(file).encode('UTF-8')) when /\.ya?ml/ YAML.load_file file else raise Error::InvalidSource, "Collection source #{file} must have a valid extension (.csv, .yml, or .json)" end rescue StandardError raise Error::InvalidSource, "Cannot load #{file}. check for typos and rebuild." end |
#overwrite_pages ⇒ Object
58 59 60 61 62 63 |
# File 'lib/pagemaster/collection.rb', line 58 def overwrite_pages return unless @dir FileUtils.rm_rf @dir puts Rainbow("Overwriting #{@dir} directory with --force.").cyan end |
#slug(str) ⇒ Object
90 91 92 |
# File 'lib/pagemaster/collection.rb', line 90 def slug(str) str.downcase.tr(' ', '_').gsub(/[^:\w-]/, '') end |
#validate_data ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/pagemaster/collection.rb', line 48 def validate_data ids = @data.map { |d| d.dig @id_key } raise Error::InvalidCollection, "One or more items in collection '#{@name}' is missing required id for the id_key '#{@id_key}'" unless ids.all? duplicates = ids.detect { |i| ids.count(i) > 1 } || [] raise Error::InvalidCollection, "The collection '#{@name}' has the follwing duplicate ids for id_key #{@id_key}: \n#{duplicates}" unless duplicates.empty? end |