Class: KweerieGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/generators/kweerie/kweerie_generator.rb

Instance Method Summary collapse

Instance Method Details

#create_query_fileObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/generators/kweerie/kweerie_generator.rb', line 9

def create_query_file
  # Convert parameters into bind statements
  bind_statements = parameters.map.with_index(1) do |param, index|
    "  bind :#{param.underscore}, as: '$#{index}'"
  end.join("\n")

  # Create the query class
  template_content = <<~RUBY
    # frozen_string_literal: true

    class #{class_name} < Kweerie::Base
      #{bind_statements}
    end
  RUBY

  # Ensure the queries directory exists
  FileUtils.mkdir_p("app/queries")

  # Create the Ruby file
  create_file "app/queries/#{file_name}.rb", template_content

  # Create the SQL file
  create_file "app/queries/#{file_name}.sql", <<~SQL
    -- Write your SQL query here
    -- Available parameters: #{parameters.map { |p| "$#{parameters.index(p) + 1} (#{p})" }.join(", ")}

    -- SELECT
      -- your columns here
    -- FROM
      -- your tables here
    -- WHERE
      -- your conditions here
  SQL
end