Class: Erdf::TextTransformer

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

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ TextTransformer

Returns a new instance of TextTransformer.



110
111
112
# File 'lib/erdf/cli.rb', line 110

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

Instance Method Details

#to_hashObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/erdf/cli.rb', line 114

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