Class: Skeema::Regex

Inherits:
Object
  • Object
show all
Defined in:
lib/skeema/regex.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRegex

Returns a new instance of Regex.



5
6
7
# File 'lib/skeema/regex.rb', line 5

def initialize
  @schema = {}
end

Instance Attribute Details

#schemaObject (readonly)

Returns the value of attribute schema.



3
4
5
# File 'lib/skeema/regex.rb', line 3

def schema
  @schema
end

Class Method Details

.parse(filename) ⇒ Object



56
57
58
# File 'lib/skeema/regex.rb', line 56

def self.parse(filename)
  new.parse(filename).schema
end

Instance Method Details

#append_columns(columns) ⇒ Object



39
40
41
# File 'lib/skeema/regex.rb', line 39

def append_columns(columns)
  @schema[@table] += columns
end

#end_tableObject



48
49
50
# File 'lib/skeema/regex.rb', line 48

def end_table
  @table = nil
end

#extract_column_names(line) ⇒ Object



32
33
34
35
36
37
# File 'lib/skeema/regex.rb', line 32

def extract_column_names(line)
  return [] unless line =~ /^\s*t\.(integer|string|text|boolean|date)/
  without_options = line.split(/(\w+:|:\w+\s*=>)/).first
  without_options.gsub!(/t\.\w+\s+/, '')
  without_options.split(/\,\s*/).map{|c| c.gsub(/:/, '').strip }
end

#extract_table_name(line) ⇒ Object



28
29
30
# File 'lib/skeema/regex.rb', line 28

def extract_table_name(line)
  line.scan(/create_table :(\S+)/).flatten.first
end

#in_table?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/skeema/regex.rb', line 52

def in_table?
  ! @table.nil?
end

#parse(filename) ⇒ Object



9
10
11
12
# File 'lib/skeema/regex.rb', line 9

def parse(filename)
  File.open(filename).each_line { |line| process_line(line) }
  self
end

#process_line(line) ⇒ Object



14
15
16
# File 'lib/skeema/regex.rb', line 14

def process_line(line)
  in_table? ? process_line_in_table(line) : process_line_outside(line)
end

#process_line_in_table(line) ⇒ Object



18
19
20
21
# File 'lib/skeema/regex.rb', line 18

def process_line_in_table(line)
  append_columns extract_column_names(line)
  end_table if line.strip == "end"
end

#process_line_outside(line) ⇒ Object



23
24
25
26
# File 'lib/skeema/regex.rb', line 23

def process_line_outside(line)
  table_name = extract_table_name(line)
  start_table(table_name) if table_name
end

#start_table(table_name) ⇒ Object



43
44
45
46
# File 'lib/skeema/regex.rb', line 43

def start_table(table_name)
  @table = table_name
  @schema[@table] = []
end