From d5a707e8a303506dc24d1e582458c013e600dcb4 Mon Sep 17 00:00:00 2001 From: 0x1eef <0x1eef@protonmail.com> Date: Thu, 6 Oct 2022 17:42:45 -0300 Subject: [PATCH] binlib/sql: add SQL::Template --- bin/sql/create-sql-seed-file | 4 +--- binlib/sql/join_row.rb | 20 -------------------- binlib/sql/template.rb | 29 +++++++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 23 deletions(-) delete mode 100644 binlib/sql/join_row.rb create mode 100644 binlib/sql/template.rb diff --git a/bin/sql/create-sql-seed-file b/bin/sql/create-sql-seed-file index 6f7dab3..a3c9392 100755 --- a/bin/sql/create-sql-seed-file +++ b/bin/sql/create-sql-seed-file @@ -13,12 +13,10 @@ require "json" require "erb" require_relative "../../binlib/sql" -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 { SQL::Language.new(_1) } result = ERB.new(template) - .result_with_hash(chapter_id: 1, languages:) + .result(SQL::Template.context) .each_line.map(&:strip) .reject(&:empty?).join($/) File.write File.join(base_dir, "seed.sql"), result diff --git a/binlib/sql/join_row.rb b/binlib/sql/join_row.rb deleted file mode 100644 index c58312d..0000000 --- a/binlib/sql/join_row.rb +++ /dev/null @@ -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 diff --git a/binlib/sql/template.rb b/binlib/sql/template.rb new file mode 100644 index 0000000..2d6a2b9 --- /dev/null +++ b/binlib/sql/template.rb @@ -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