class Erde::TextTransformer

Public Class Methods

new(text) click to toggle source
# File lib/erde/cli.rb, line 90
def initialize(text)
  @lines = text.lines
end

Public Instance Methods

to_hash() click to toggle source
# File lib/erde/cli.rb, line 94
def to_hash
  generated_hash = {}
  current_table = nil

  @lines.each do |line|
    cleaned_line = line.strip

    if current_table && cleaned_line.length > 0
      generated_hash[current_table]['columns'] << cleaned_line
    end

    if cleaned_line.length == 0
      current_table = nil
    end

    if match = cleaned_line.match(/^\[(\w+)\]/)
      current_table = match[1]
      generated_hash[current_table] = {}
      generated_hash[current_table]['columns'] = []
      generated_hash[current_table]['relations'] = {}
    end

    if match = cleaned_line.match(/^(\w+):(\w+) -- (\w+):(\w+)/)
      generated_hash[match[1]]['relations'][match[2]] = { 'table' => match[3], 'column' => match[4] }
    end
  end

  generated_hash
end