Class: Erde::TextTransformer

Inherits:
Object
  • Object
show all
Defined in:
lib/erde/cli.rb

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ TextTransformer

Returns a new instance of TextTransformer.



50
51
52
# File 'lib/erde/cli.rb', line 50

def initialize(text)
  @lines = text.lines
end

Instance Method Details

#to_hashObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/erde/cli.rb', line 54

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