diff --git a/bin/sql/create-sql-seed-file b/bin/sql/create-sql-seed-file index 9d7931f..fe986a3 100755 --- a/bin/sql/create-sql-seed-file +++ b/bin/sql/create-sql-seed-file @@ -1,57 +1,22 @@ #!/usr/bin/env ruby # 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 "erb" +require_relative "../../binlib/sql/language" +require_relative "../../binlib/sql/chapter" +require_relative "../../binlib/sql/verse" +require_relative "../../binlib/sql/sql_utils" -module SQLUtils - 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") +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) } result = ERB.new(template) diff --git a/binlib/sql/chapter.rb b/binlib/sql/chapter.rb new file mode 100644 index 0000000..5c00a29 --- /dev/null +++ b/binlib/sql/chapter.rb @@ -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 diff --git a/binlib/sql/language.rb b/binlib/sql/language.rb new file mode 100644 index 0000000..e567904 --- /dev/null +++ b/binlib/sql/language.rb @@ -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 diff --git a/binlib/sql/sql_utils.rb b/binlib/sql/sql_utils.rb new file mode 100644 index 0000000..234c05e --- /dev/null +++ b/binlib/sql/sql_utils.rb @@ -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 diff --git a/binlib/sql/verse.rb b/binlib/sql/verse.rb new file mode 100644 index 0000000..0a5bcd2 --- /dev/null +++ b/binlib/sql/verse.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +class Verse + attr_reader :number, :content + + def initialize(number, content) + @number = number + @content = content + end +end