Class: Kolla::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/kolla/parser.rb

Defined Under Namespace

Classes: Field, Index

Constant Summary collapse

DEFAULT_FILE =
File.join("db", "schema.rb").freeze
IGNORED_PREFIXES =
%w[action_ active_].freeze

Instance Method Summary collapse

Constructor Details

#initialize(file_path: DEFAULT_FILE) ⇒ Parser

Returns a new instance of Parser.



11
12
13
14
15
16
# File 'lib/kolla/parser.rb', line 11

def initialize(file_path: DEFAULT_FILE)
  @file_path = File.expand_path(file_path)
  @content = File.read(@file_path)
rescue Errno::ENOENT
  raise SchemaNotFound, "Schema file not found at #{@file_path}"
end

Instance Method Details

#fields_for(name) ⇒ Object

Raises:



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/kolla/parser.rb', line 23

def fields_for(name)
  escaped = Regexp.escape(name)
  table_regex = /create_table "#{escaped}".*?do \|t\|(.*?)end/m
  match = @content.match(table_regex)
  raise TableNotFound, "Table '#{name}' not found in schema" unless match

  block = match[1]
  fields = parse_fields(block)
  indexes = parse_indexes(block)

  format_output(name, fields, indexes)
end

#tablesObject



18
19
20
21
# File 'lib/kolla/parser.rb', line 18

def tables
  matches = @content.scan(/create_table\s+"(\w+)"/)
  matches.map(&:first).reject { |table| IGNORED_PREFIXES.any? { |prefix| table.start_with?(prefix) } }
end