Class: JekyllSQlite::Generator

Inherits:
Jekyll::Generator
  • Object
show all
Defined in:
lib/jekyll-sqlite/generator.rb

Overview

Main generator class

Instance Method Summary collapse

Instance Method Details

#_gen_data(root, key, db, query) ⇒ Object

Internal function to generate data given root: a Hash-Like root object (site.data, site.data.*, page.data) key: string as the key to use to attach the data to the root db: SQLite3 Database object to execute the query on query: string containing the query to execute Sets root = ResultSet of the query, as an array Returns the count of the result set



49
50
51
52
53
54
55
# File 'lib/jekyll-sqlite/generator.rb', line 49

def _gen_data(root, key, db, query)
  db.prepare(query) do |stmt|
    _prepare_query stmt, get_bind_params(root)
    root[key] = stmt.execute.to_a
  end
  root[key].count
end

#_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”



33
34
35
36
37
38
39
# File 'lib/jekyll-sqlite/generator.rb', line 33

def _prepare_query(stmt, params)
  params.each do |key, value|
    stmt.bind_param key, value
  rescue StandardError => e
    raise e unless e.message.include? "no such bind parameter"
  end
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.



122
123
124
125
126
127
128
129
130
131
# File 'lib/jekyll-sqlite/generator.rb', line 122

def gen(root, config_holder)
  sqlite_configs = config_holder["sqlite"] || []
  sqlite_configs.each do |config|
    unless validate_config(config)
      Jekyll.logger.error "Jekyll SQLite:", "Invalid Configuration. Skipping"
      next
    end
    generate_data_from_config(root, config)
  end
end

#gen_data(root) ⇒ Object

Calls _gen_data for the given root iterates through the array if root is an array



60
61
62
63
64
65
66
67
68
# File 'lib/jekyll-sqlite/generator.rb', line 60

def gen_data(root, ...)
  if root.is_a? Array
    # call gen_data for each item in the array
    # and return the sum of all the counts
    root.map { |item| gen_data(item, ...) }.sum
  else
    _gen_data(root, ...)
  end
end

#gen_pages(site) ⇒ Object

Iterate through all the pages in the site and generate the data from the configuration



110
111
112
113
114
# File 'lib/jekyll-sqlite/generator.rb', line 110

def gen_pages(site)
  site.pages.each do |page|
    gen(page.data, page)
  end
end

#generate(site) ⇒ Object

Entrpoint to the generator, called by Jekyll



135
136
137
138
# File 'lib/jekyll-sqlite/generator.rb', line 135

def generate(site)
  gen(site.data, site.config)
  gen_pages(site)
end

#generate_data_from_config(root, config) ⇒ Object

Given a configuration, generate the data and attach it to the given data_root



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/jekyll-sqlite/generator.rb', line 92

def generate_data_from_config(root, config)
  key = config["data"]
  query = config["query"]
  file = config["file"]
  SQLite3::Database.new file, readonly: true do |db|
    db.results_as_hash = config.fetch("results_as_hash", true)

    branch = get_root(root, key)
    tip = get_tip(config["data"])

    count = gen_data(branch, tip, db, query)
    Jekyll.logger.info "Jekyll SQLite:", "Loaded #{key}. Count=#{count}"
  end
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



85
86
87
# File 'lib/jekyll-sqlite/generator.rb', line 85

def get_bind_params(dict)
  dict.select { |_key, value| !value.is_a?(Array) && !value.is_a?(Hash) }
end

#get_root(root, db_name) ⇒ Object

Get the root of where we are generating the data



20
21
22
23
24
25
26
27
# File 'lib/jekyll-sqlite/generator.rb', line 20

def get_root(root, db_name)
  db_name.split(".")[0..-2].each do |p|
    root = root[p]
  rescue KeyError
    raise "Jekyll SQLite: Invalid root. #{p} not found while iterating to #{db_name}"
  end
  root
end

#get_tip(name) ⇒ Object

Split the given key using dots and return the last part

customers.order -> order


14
15
16
# File 'lib/jekyll-sqlite/generator.rb', line 14

def get_tip(name)
  name.split(".")[-1]
end

#validate_config(config) ⇒ Object

Validate given configuration object



72
73
74
75
76
77
78
79
# File 'lib/jekyll-sqlite/generator.rb', line 72

def validate_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