Module: WaxTasks::Utils
- Defined in:
- lib/wax_tasks/utils.rb
Overview
Utility helper methods
Class Method Summary collapse
-
.assert_pids(data) ⇒ Array
Checks and asserts presence of ‘pid` value for each item.
-
.assert_unique(data) ⇒ Array
Checks and asserts uniqueness of ‘pid` value for each item.
-
.construct_permalink(site) ⇒ String
Contructs permalink extension from site ‘permalink` variable.
-
.get_lunr_collections(site) ⇒ Array
Finds collections in site config where ‘lunr_index` is enabled.
-
.make_path(*args) ⇒ String
Creates a file path valid file path with empty strings and null values dropped.
-
.validate_csv(source) ⇒ Array
Checks that a CSV file is valid.
-
.validate_json(source) ⇒ Array
Checks that a JSON file is valid.
-
.validate_yaml(source) ⇒ Array
Checks that a YAML file is valid.
Class Method Details
.assert_pids(data) ⇒ Array
Checks and asserts presence of ‘pid` value for each item
22 23 24 25 |
# File 'lib/wax_tasks/utils.rb', line 22 def self.assert_pids(data) data.each_with_index { |d, i| raise Error::MissingPid, "Collection #{@name} is missing pid for item #{i}." unless d.key? 'pid' } data end |
.assert_unique(data) ⇒ Array
Checks and asserts uniqueness of ‘pid` value for each item
32 33 34 35 36 37 |
# File 'lib/wax_tasks/utils.rb', line 32 def self.assert_unique(data) pids = data.map { |d| d['pid'] } not_unique = pids.select { |p| pids.count(p) > 1 }.uniq! || [] raise Error::NonUniquePid, "#{@name} has the following nonunique pids:\n#{not_unique}" unless not_unique.empty? data end |
.construct_permalink(site) ⇒ String
Contructs permalink extension from site ‘permalink` variable
8 9 10 11 12 13 14 15 |
# File 'lib/wax_tasks/utils.rb', line 8 def self.construct_permalink(site) case site.fetch(:permalink, false) when 'pretty' || '/' '/' else '.html' end end |
.get_lunr_collections(site) ⇒ Array
Finds collections in site config where ‘lunr_index` is enabled
87 88 89 90 91 |
# File 'lib/wax_tasks/utils.rb', line 87 def self.get_lunr_collections(site) to_index = site[:collections].find_all { |c| c[1].key?('lunr_index') } raise Error::NoLunrCollections, 'There are no lunr collections to index.' if to_index.nil? to_index.map { |c| c[0] } end |
.make_path(*args) ⇒ String
Creates a file path valid file path with empty strings and null values dropped
78 79 80 |
# File 'lib/wax_tasks/utils.rb', line 78 def self.make_path(*args) args.compact.reject(&:empty?).join('/') end |
.validate_csv(source) ⇒ Array
Checks that a CSV file is valid
44 45 46 47 48 |
# File 'lib/wax_tasks/utils.rb', line 44 def self.validate_csv(source) CSV.read(source, headers: true).map(&:to_hash) rescue StandardError => e raise Error::InvalidCSV, " #{e}" end |
.validate_json(source) ⇒ Array
Checks that a JSON file is valid
55 56 57 58 59 60 |
# File 'lib/wax_tasks/utils.rb', line 55 def self.validate_json(source) file = File.read(source) JSON.parse(file) rescue StandardError => e raise Error::InvalidJSON, " #{e}" end |
.validate_yaml(source) ⇒ Array
Checks that a YAML file is valid
67 68 69 70 71 |
# File 'lib/wax_tasks/utils.rb', line 67 def self.validate_yaml(source) YAML.load_file(source) rescue StandardError => e raise WaxTasks::Error::InvalidYAML, " #{e}" end |