bin/sql/create-sql-seed-file: improve

This commit is contained in:
0x1eef 2022-07-16 17:23:16 -03:00
parent 2c521b6d70
commit a077db4b7f
5 changed files with 63 additions and 48 deletions

View file

@ -1,57 +1,22 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
# frozen_string_literal: true # frozen_string_literal: true
##
# This script is responsible for the creation "src/sql/seed.sql" -
# a SQL file that contains the contents of The Qur'an in Arabic,
# and as translations in English, Portuguese and Farsi. The
# "src/json/" directory is used as the data source.
##
# Dependencies
require "json" require "json"
require "erb" require "erb"
require_relative "../../binlib/sql/language"
require_relative "../../binlib/sql/chapter"
require_relative "../../binlib/sql/verse"
require_relative "../../binlib/sql/sql_utils"
module SQLUtils base_dir = File.join("src", "sql")
module_function
def escape(str)
char = "'"
char + str.gsub(char, char * 2) + char
end
end
class Language
attr_reader :locale
def initialize(locale)
@locale = locale
end
def chapters
Dir.glob(File.join(
__dir__, "..", "src",
"json", @locale, "*.json"
)).map { Chapter.new(_1) }.sort_by(&:number)
end
end
class Chapter
def initialize(path)
@path = path
end
def number
File.basename(@path, ".json").to_i
end
def verses
JSON.parse(File.read(@path)).map { Verse.new(*_1) }
end
end
class Verse
attr_reader :number, :content
def initialize(number, content)
@number = number
@content = content
end
end
base_dir = File.join(__dir__, "..", "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 { Language.new(_1) } languages = %w[ar en pt fa].map { Language.new(_1) }
result = ERB.new(template) result = ERB.new(template)

15
binlib/sql/chapter.rb Normal file
View file

@ -0,0 +1,15 @@
# frozen_string_literal: true
class Chapter
def initialize(path)
@path = path
end
def number
File.basename(@path, ".json").to_i
end
def verses
JSON.parse(File.read(@path)).map { Verse.new(*_1) }
end
end

15
binlib/sql/language.rb Normal file
View file

@ -0,0 +1,15 @@
# frozen_string_literal: true
class Language
attr_reader :locale
def initialize(locale)
@locale = locale
end
def chapters
Dir.glob(File.join(
"src", "json", @locale, "*.json"
)).map { Chapter.new(_1) }.sort_by(&:number)
end
end

10
binlib/sql/sql_utils.rb Normal file
View file

@ -0,0 +1,10 @@
# frozen_string_literal: true
module SQLUtils
module_function
def escape(str)
char = "'"
char + str.gsub(char, char * 2) + char
end
end

10
binlib/sql/verse.rb Normal file
View file

@ -0,0 +1,10 @@
# frozen_string_literal: true
class Verse
attr_reader :number, :content
def initialize(number, content)
@number = number
@content = content
end
end