binlib/sql: namespace SQL-related classes, and modules.
This commit is contained in:
parent
22a2791260
commit
f0f0483af8
8 changed files with 32 additions and 39 deletions
|
@ -11,16 +11,12 @@
|
|||
# Dependencies
|
||||
require "json"
|
||||
require "erb"
|
||||
require_relative "../../binlib/sql/language"
|
||||
require_relative "../../binlib/sql/chapter"
|
||||
require_relative "../../binlib/sql/verse"
|
||||
require_relative "../../binlib/sql/sql_utils"
|
||||
require_relative "../../binlib/sql/row_mapper"
|
||||
require_relative "../../binlib/sql"
|
||||
|
||||
extend RowMapper
|
||||
extend SQL::JoinRow
|
||||
base_dir = File.join("src", "sql")
|
||||
template = File.read File.join(base_dir, "seed.sql.erb")
|
||||
languages = %w[ar en pt fa].map { Language.new(_1) }
|
||||
languages = %w[ar en pt fa].map { SQL::Language.new(_1) }
|
||||
result = ERB.new(template)
|
||||
.result_with_hash(chapter_id: 1, languages:)
|
||||
.each_line.map(&:strip)
|
||||
|
|
9
binlib/sql.rb
Normal file
9
binlib/sql.rb
Normal file
|
@ -0,0 +1,9 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module SQL
|
||||
require_relative "sql/language"
|
||||
require_relative "sql/chapter"
|
||||
require_relative "sql/verse"
|
||||
require_relative "sql/utils"
|
||||
require_relative "sql/join_row"
|
||||
end
|
|
@ -1,6 +1,6 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Chapter
|
||||
class SQL::Chapter
|
||||
def initialize(path)
|
||||
@contents = JSON.parse(File.read(path))
|
||||
@metadata = @contents.shift
|
||||
|
@ -13,9 +13,9 @@ class Chapter
|
|||
|
||||
def name
|
||||
if @locale == "ar"
|
||||
SQLUtils.escape(@metadata['codepoints'].pack('U*'))
|
||||
@metadata["codepoints"].pack("U*")
|
||||
else
|
||||
SQLUtils.escape(@metadata['translated_name'])
|
||||
@metadata["translated_name"]
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -23,19 +23,19 @@ class Chapter
|
|||
if @locale == "ar"
|
||||
name
|
||||
else
|
||||
SQLUtils.escape(@metadata['transliterated_name'])
|
||||
@metadata["transliterated_name"]
|
||||
end
|
||||
end
|
||||
|
||||
def slug
|
||||
SQLUtils.escape(@metadata['slug'])
|
||||
@metadata["slug"]
|
||||
end
|
||||
|
||||
def city
|
||||
SQLUtils.escape(@metadata['place_of_revelation'].capitalize)
|
||||
@metadata["place_of_revelation"].capitalize
|
||||
end
|
||||
|
||||
def verses
|
||||
@contents.map { Verse.new(*_1) }
|
||||
@contents.map { SQL::Verse.new(*_1) }
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,16 +1,20 @@
|
|||
module RowMapper
|
||||
# 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
|
||||
].join(",")
|
||||
].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
|
||||
].join(",")
|
||||
].map { Integer === _1 ? _1 : SQL::Utils.escape(_1) }
|
||||
.join(",")
|
||||
end
|
||||
end
|
|
@ -1,15 +1,9 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Language
|
||||
attr_reader :locale
|
||||
|
||||
def initialize(locale)
|
||||
@locale = locale
|
||||
end
|
||||
|
||||
class SQL::Language < Struct.new(:locale)
|
||||
def chapters
|
||||
Dir.glob(File.join(
|
||||
"src", "json", @locale, "*.json"
|
||||
)).map { Chapter.new(_1) }.sort_by(&:number)
|
||||
"src", "json", locale, "*.json"
|
||||
)).map { SQL::Chapter.new(_1) }.sort_by(&:number)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module SQLUtils
|
||||
module SQL::Utils
|
||||
module_function
|
||||
|
||||
def escape(str)
|
|
@ -1,14 +1,4 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Verse
|
||||
attr_reader :number, :content
|
||||
|
||||
def initialize(number, content)
|
||||
@number = number
|
||||
@content = content
|
||||
end
|
||||
|
||||
def content
|
||||
SQLUtils.escape(@content)
|
||||
end
|
||||
class SQL::Verse < Struct.new(:number, :content)
|
||||
end
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
<% languages.each.with_index(1) do |language, quran_id| %>
|
||||
INSERT INTO qurans (locale) VALUES (<%= SQLUtils.escape(language.locale) %>);
|
||||
INSERT INTO qurans (locale) VALUES (<%= SQL::Utils.escape(language.locale) %>);
|
||||
<% language.chapters.each do |chapter| %>
|
||||
INSERT INTO chapters (
|
||||
id, number, name, tr_name, slug, city, quran_id
|
||||
|
|
Loading…
Reference in a new issue