Class: Tembin::Exporter

Inherits:
Object
  • Object
show all
Defined in:
lib/tembin/exporter.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(out_dir, split_sql: true, split_file: true, out: STDOUT) ⇒ Exporter

Returns a new instance of Exporter.



6
7
8
9
10
11
# File 'lib/tembin/exporter.rb', line 6

def initialize(out_dir, split_sql: true, split_file: true, out: STDOUT)
  @out_dir = out_dir
  @split_sql_mode = split_sql
  @split_file_mode = split_file
  @out = out
end

Class Method Details

.run(out_dir, split_sql: true, split_file: true, out: STDOUT) ⇒ Object



2
3
4
# File 'lib/tembin/exporter.rb', line 2

def self.run(out_dir, split_sql: true, split_file: true, out: STDOUT)
  new(out_dir, split_sql: split_sql, split_file: split_file, out: STDOUT).run
end

Instance Method Details

#runObject



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
43
44
45
46
47
48
49
# File 'lib/tembin/exporter.rb', line 13

def run
  FileUtils.rm_f(@out_dir.join('tembin.rb')) if File.exist?(@out_dir.join('tembin.rb'))

  export_head_file if @split_file_mode

  Tembin::Redash::Query.created_by_me.each do |query|
    @out.puts(h.color("export #{query.name}", :green))

    file =
      if @split_file_mode
        File.open(@out_dir.join("#{query.filename}.rb"), "w")
      else
        File.open(@out_dir.join("tembin.rb"), "a")
      end

    @out.puts(h.color("   redash query file --> #{file.path}", :yellow))

    sql =
      if @split_sql_mode
        path = @out_dir.join("#{query.filename}.sql")
        File.write(path, query.sql)
        @out.puts(h.color("   sql --> #{path}", :yellow))
        "open(File.join(__dir__, '#{query.filename}.sql')).read"
      else
        "%(#{query.sql})"
      end

    file.puts(<<EOS)
query "#{query.name}" do
sql #{sql}
end\n
EOS
    file.close

    @out.puts
  end
end