Class: JekyllSQlite::Generator
- Inherits:
-
Jekyll::Generator
- Object
- Jekyll::Generator
- JekyllSQlite::Generator
- Defined in:
- lib/jekyll-sqlite/generator.rb
Overview
Main generator class
Instance Method Summary collapse
-
#_prepare_query(stmt, params) ⇒ Object
Prepare the query by binding the parameters Since we don’t know if the query needs them we ignore all errors about “no such bind parameter”.
-
#attach_nested_data(root, path_segments, db, query) ⇒ Object
Recursively attach query results to nested data structures Supports arbitrary levels of nesting (e.g., regions.territories.EmployeeIDs) Handles both arrays and hashes at each level.
- #close_all_databases ⇒ Object
-
#gen(root, config_holder) ⇒ Object
Generate the data from the configuration Takes as input the root where the data will be attached and a configuration holder, where the sqlite key can be found Root is either site.data or page.data and config_holder is either site.config or page itself.
-
#generate(site) ⇒ Object
Entrpoint to the generator, called by Jekyll.
- #generate_data_from_config(root, config) ⇒ Object
-
#get_bind_params(dict) ⇒ Object
pick bindable parameters from the root All primitive values are bound to the query Arrays and Hashes are ignored.
- #get_database(file) ⇒ Object
-
#valid_config?(config) ⇒ Boolean
Validate given configuration object.
Instance Method Details
#_prepare_query(stmt, params) ⇒ Object
Prepare the query by binding the parameters Since we don’t know if the query needs them we ignore all errors about “no such bind parameter”
51 52 53 54 55 56 57 |
# File 'lib/jekyll-sqlite/generator.rb', line 51 def _prepare_query(stmt, params) params.each do |key, value| stmt.bind_param key, value rescue StandardError => e raise e unless e..include? "no such bind parameter" end end |
#attach_nested_data(root, path_segments, db, query) ⇒ Object
Recursively attach query results to nested data structures Supports arbitrary levels of nesting (e.g., regions.territories.EmployeeIDs) Handles both arrays and hashes at each level
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/jekyll-sqlite/generator.rb', line 25 def attach_nested_data(root, path_segments, db, query) return 0 if path_segments.empty? if path_segments.size == 1 key = path_segments.first db.prepare(query) do |stmt| _prepare_query(stmt, get_bind_params(root)) root[key] = stmt.execute.to_a end return root[key].size end first, *remaining = path_segments current_level = root[first] if current_level.is_a?(Array) current_level.sum { |item| attach_nested_data(item, remaining, db, query) } else attach_nested_data(current_level, remaining, db, query) end end |
#close_all_databases ⇒ Object
17 18 19 |
# File 'lib/jekyll-sqlite/generator.rb', line 17 def close_all_databases @db.each_value(&:close) end |
#gen(root, config_holder) ⇒ Object
Generate the data from the configuration Takes as input the root where the data will be attached and a configuration holder, where the sqlite key can be found Root is either site.data or page.data and config_holder is either site.config or page itself.
96 97 98 99 100 101 102 103 104 |
# File 'lib/jekyll-sqlite/generator.rb', line 96 def gen(root, config_holder) (config_holder["sqlite"] || []).each do |config| unless valid_config?(config) Jekyll.logger.error "Jekyll SQLite:", "Invalid Configuration. Skipping" next end generate_data_from_config(root, config) end end |
#generate(site) ⇒ Object
Entrpoint to the generator, called by Jekyll
108 109 110 111 112 113 114 115 116 |
# File 'lib/jekyll-sqlite/generator.rb', line 108 def generate(site) @db = {} gen(site.data, site.config) site.pages.each do |page| gen(page.data, page) end ensure close_all_databases end |
#generate_data_from_config(root, config) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/jekyll-sqlite/generator.rb', line 78 def generate_data_from_config(root, config) key = config["data"] query = config["query"] file = config["file"] db = get_database(file) db.results_as_hash = config.fetch("results_as_hash", true) path_segments = key.split(".") count = attach_nested_data(root, path_segments, db, query) Jekyll.logger.info "Jekyll SQLite:", "Loaded #{key}. Count=#{count}" end |
#get_bind_params(dict) ⇒ Object
pick bindable parameters from the root All primitive values are bound to the query Arrays and Hashes are ignored
74 75 76 |
# File 'lib/jekyll-sqlite/generator.rb', line 74 def get_bind_params(dict) dict.select { |_key, value| !value.is_a?(Array) && !value.is_a?(Hash) } end |
#get_database(file) ⇒ Object
11 12 13 14 15 |
# File 'lib/jekyll-sqlite/generator.rb', line 11 def get_database(file) return @db[file] if @db.key?(file) @db[file] = SQLite3::Database.new file, readonly: true end |
#valid_config?(config) ⇒ Boolean
Validate given configuration object
61 62 63 64 65 66 67 68 |
# File 'lib/jekyll-sqlite/generator.rb', line 61 def valid_config?(config) return false unless config.is_a? Hash return false unless config.key?("query") return false unless File.exist?(config["file"]) return false unless config.key?("data") true end |