binlib/sql: add SQL::Template
This commit is contained in:
parent
f0f0483af8
commit
d5a707e8a3
3 changed files with 30 additions and 23 deletions
|
@ -13,12 +13,10 @@ require "json"
|
||||||
require "erb"
|
require "erb"
|
||||||
require_relative "../../binlib/sql"
|
require_relative "../../binlib/sql"
|
||||||
|
|
||||||
extend SQL::JoinRow
|
|
||||||
base_dir = File.join("src", "sql")
|
base_dir = File.join("src", "sql")
|
||||||
template = File.read File.join(base_dir, "seed.sql.erb")
|
template = File.read File.join(base_dir, "seed.sql.erb")
|
||||||
languages = %w[ar en pt fa].map { SQL::Language.new(_1) }
|
|
||||||
result = ERB.new(template)
|
result = ERB.new(template)
|
||||||
.result_with_hash(chapter_id: 1, languages:)
|
.result(SQL::Template.context)
|
||||||
.each_line.map(&:strip)
|
.each_line.map(&:strip)
|
||||||
.reject(&:empty?).join($/)
|
.reject(&:empty?).join($/)
|
||||||
File.write File.join(base_dir, "seed.sql"), result
|
File.write File.join(base_dir, "seed.sql"), result
|
||||||
|
|
|
@ -1,20 +0,0 @@
|
||||||
# frozen_string_literal: true
|
|
||||||
|
|
||||||
module SQL::JoinRow
|
|
||||||
def chapter_row(quran_id, chapter_id, chapter)
|
|
||||||
[
|
|
||||||
chapter_id, chapter.number, chapter.name,
|
|
||||||
chapter.tr_name, chapter.slug, chapter.city,
|
|
||||||
quran_id
|
|
||||||
].map { Integer === _1 ? _1 : SQL::Utils.escape(_1) }
|
|
||||||
.join(",")
|
|
||||||
end
|
|
||||||
|
|
||||||
def verse_row(verse, quran_id, chapter_id)
|
|
||||||
[
|
|
||||||
verse.number, quran_id,
|
|
||||||
chapter_id, verse.content
|
|
||||||
].map { Integer === _1 ? _1 : SQL::Utils.escape(_1) }
|
|
||||||
.join(",")
|
|
||||||
end
|
|
||||||
end
|
|
29
binlib/sql/template.rb
Normal file
29
binlib/sql/template.rb
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class SQL::Template
|
||||||
|
def self.context
|
||||||
|
new.context
|
||||||
|
end
|
||||||
|
|
||||||
|
def context
|
||||||
|
context = binding
|
||||||
|
context.local_variable_set(:languages, %w[ar en pt fa].map { SQL::Language.new(_1) })
|
||||||
|
context.local_variable_set(:chapter_id, 1)
|
||||||
|
context
|
||||||
|
end
|
||||||
|
|
||||||
|
def chapter_row(quran_id, chapter_id, chapter)
|
||||||
|
[
|
||||||
|
chapter_id, chapter.number, chapter.name,
|
||||||
|
chapter.tr_name, chapter.slug, chapter.city,
|
||||||
|
quran_id
|
||||||
|
].map { Integer === _1 ? _1 : SQL::Utils.escape(_1) }.join(",")
|
||||||
|
end
|
||||||
|
|
||||||
|
def verse_row(verse, quran_id, chapter_id)
|
||||||
|
[
|
||||||
|
verse.number, quran_id,
|
||||||
|
chapter_id, verse.content
|
||||||
|
].map { Integer === _1 ? _1 : SQL::Utils.escape(_1) }.join(",")
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue