Module: Kernel

Includes:
Crudboy::Concerns::GlobalDataDefinition
Defined in:
lib/crudboy/ext/kernel.rb

Constant Summary collapse

CSV_BOM =
"\xef\xbb\xbf"

Instance Method Summary collapse

Instance Method Details

#generate_csv(filename, **options, &block) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/crudboy/ext/kernel.rb', line 71

def generate_csv(filename, **options, &block)
  opts = {
    col_sep: "\t",
    row_sep: "\r\n"
  }
  opts.merge!(options.except(:encoding))
  encoding = options[:encoding] || 'UTF-16LE'
  File.open(File.expand_path(filename), "w:#{encoding}") do |file|
    file.write(CSV_BOM)
    file.write CSV.generate(**opts, &block)
  end
end

#generate_excel(filename) ⇒ Object



95
96
97
98
99
100
# File 'lib/crudboy/ext/kernel.rb', line 95

def generate_excel(filename)
  Axlsx::Package.new do |package|
    yield(package.workbook)
    package.serialize(File.expand_path(filename))
  end
end

#parse_csv(filename, **options) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/crudboy/ext/kernel.rb', line 84

def parse_csv(filename, **options)
  encoding = options[:encoding] || 'UTF-16'
  opts = {
    headers: false,
    col_sep: "\t",
    row_sep: "\r\n"
  }
  opts.merge!(options.except(:encoding))
  CSV.parse(IO.read(File.expand_path(filename), encoding: encoding, binmode: true).encode('UTF-8'), **opts).to_a
end

#parse_excel(filename) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/crudboy/ext/kernel.rb', line 102

def parse_excel(filename)
  xlsx = Roo::Excelx.new(File.expand_path(filename))
  xlsx.sheets.each_with_object({}) do |sheet_name, result|
    begin
      result[sheet_name] = xlsx.sheet(sheet_name).to_a
    rescue
    end
  end
end


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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/crudboy/ext/kernel.rb', line 11

def print_tables(format = :md)
  require 'terminal-table'

  tables = ActiveRecord::Base.connection.tables.map do |table_name|
    {
      table: table_name,
      table_comment: ActiveRecord::Base.connection.table_comment(table_name) || '',
      columns: ::ActiveRecord::Base.connection.columns(table_name)
    }
  end

  outputs = tables.map do |table|
    table_name = table[:table]
    table_comment = table[:table_comment]
    case format
    when :md
      "# #{table_name} #{table_comment}\n\n" +
        Terminal::Table.new { |t|
        t.headings = ['PK', 'Name', 'SQL Type', 'Limit', 'Precision', 'Scale', 'Default', 'Nullable', 'Comment']
        t.rows = table[:columns].map { |column|
          pk = if [::ActiveRecord::Base.connection.primary_key(table_name)].flatten.include?(column)
                 'Y'
               else
                 ''
               end
          [pk, "`#{column.name}`", column.sql_type, column..limit || '', column..precision || '',
           column..scale || '', column.default || '', column.null, column.comment || '']
        }
        t.style = {
          border_top: false,
          border_bottom: false,
          border_i: '|'
        }
      }.to_s.lines.map { |l| '  ' + l }.join
    when :org
      "* #{table_name} #{table_comment}\n\n" +
        Terminal::Table.new { |t|
        t.headings = ['PK', 'Name', 'SQL Type', 'Limit', 'Precision', 'Scale', 'Default', 'Nullable', 'Comment']
        t.rows = table[:columns].map { |column|
          pk = if [::ActiveRecord::Base.connection.primary_key(table_name)].flatten.include?(column)
                 'Y'
               else
                 ''
               end
          [pk, "=#{column.name}=", column.sql_type, column..limit || '', column..precision || '',
           column..scale || '', column.default || '', column.null, column.comment || '']
        }
        t.style = {
          border_top: false,
          border_bottom: false,
        }
      }.to_s.lines.map { |l| '  ' + l.gsub(/^\+|\+$/, '|') }.join
    when :sql
      "-- Table: #{table_name}\n\n" + ActiveRecord::Base.connection.exec_query("show create table `#{table_name}`").rows.last.last + ';'
    end
  end

  outputs.each { |out| puts out; puts }
end

#sql(sql) ⇒ Object



7
8
9
# File 'lib/crudboy/ext/kernel.rb', line 7

def sql(sql)
  ActiveRecord::Base.connection.exec_query(sql)
end