Class: Ratbug::Generators::TypescriptGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/ratbug/generators/typescript_generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(table, options) ⇒ TypescriptGenerator

Returns a new instance of TypescriptGenerator.



6
7
8
9
# File 'lib/ratbug/generators/typescript_generator.rb', line 6

def initialize(table, options)
  @table = table
  @options = options
end

Instance Method Details

#generateObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ratbug/generators/typescript_generator.rb', line 11

def generate
  output = ""
  columns.values.filter { |c| c.enum.present? }.each do |column|
    output << enum_output(column)
  end

  output << "type #{@table.name.singularize.camelize} = {\n"
  columns.values.sort_by(&:name).each do |column|
    output << column_row(column)
  end
  output << "};"

  dirname = File.dirname(output_path)
  unless File.directory?(dirname)
    FileUtils.mkdir_p(dirname)
  end
  File.open(output_path, "w") do |f|
    f.puts(output)
  end

  puts "generated: #{output_path}"
end