Module: WaxTasks::Collection::Metadata
- Included in:
- WaxTasks::Collection
- Defined in:
- lib/wax_tasks/collection/metadata.rb
Instance Method Summary collapse
- #consolidate_records(original, new) ⇒ Object
- #csv_string(records) ⇒ Object
- #json_string(records) ⇒ Object
- #records_from_metadata ⇒ Object
- #records_from_pages ⇒ Object
- #search_fields=(fields) ⇒ Object
- #update_metadata(update) ⇒ Object
- #yaml_string(records) ⇒ Object
Instance Method Details
#consolidate_records(original, new) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/wax_tasks/collection/metadata.rb', line 66 def consolidate_records(original, new) lost_record_pids = original.map(&:pid) - new.map(&:pid) lost_record_pids.each do |pid| new << original.find { |r| r.pid == pid } end new.sort_by(&:order) end |
#csv_string(records) ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/wax_tasks/collection/metadata.rb', line 76 def csv_string(records) keys = records.flat_map(&:keys).uniq CSV.generate do |csv| csv << keys records.each do |r| csv << keys.map { |k| r.hash.fetch(k, '') } end end end |
#json_string(records) ⇒ Object
88 89 90 91 |
# File 'lib/wax_tasks/collection/metadata.rb', line 88 def json_string(records) hashes = records.map(&:hash) JSON.pretty_generate hashes end |
#records_from_metadata ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/wax_tasks/collection/metadata.rb', line 36 def raise Error::MissingSource, "Cannot find metadata source '#{@metadata_source}'" unless File.exist? @metadata_source = Utils.ingest @metadata_source .each_with_index.map do |, i| Record.new().tap do |r| r.set 'order', Utils.padded_int(i, .length) unless r.order? r.set 'layout', @config['layout'] if @config.key? 'layout' r.set 'collection', @name end end end |
#records_from_pages ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/wax_tasks/collection/metadata.rb', line 17 def records_from_pages paths = Dir.glob("#{@page_source}/*.{md, markdown}") warn Rainbow("There are no pages in #{@page_source} to index.").orange if paths.empty? paths.map do |path| begin content = WaxTasks::Utils.content_clean File.read(path) Record.new(SafeYAML.load_file(path)).tap do |r| r.set 'content', content r.set 'permalink', "/#{@name}/#{r.pid}#{@ext}" unless r.permalink? end rescue StandardError => e raise Error::PageLoad, "Cannot load page #{path}\n#{e}" end end end |
#search_fields=(fields) ⇒ Object
11 12 13 |
# File 'lib/wax_tasks/collection/metadata.rb', line 11 def search_fields=(fields) @search_fields.concat(fields).flatten.compact.uniq end |
#update_metadata(update) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/wax_tasks/collection/metadata.rb', line 51 def (update) records = consolidate_records , update reformatted = case File.extname @metadata_source when '.csv' csv_string records when '.json' json_string records when /\.ya?ml/ yaml_string records end File.open(@metadata_source, 'w') { |f| f.puts reformatted } end |
#yaml_string(records) ⇒ Object
95 96 97 98 |
# File 'lib/wax_tasks/collection/metadata.rb', line 95 def yaml_string(records) hashes = records.map(&:hash) hashes.to_yaml end |