Class: Kolla::Parser
- Inherits:
-
Object
- Object
- Kolla::Parser
- Defined in:
- lib/kolla/parser.rb
Defined Under Namespace
Constant Summary collapse
- DEFAULT_FILE =
File.join("db", "schema.rb").freeze
- IGNORED_PREFIXES =
%w[action_ active_].freeze
Instance Method Summary collapse
- #fields_for(name) ⇒ Object
-
#initialize(file_path: DEFAULT_FILE) ⇒ Parser
constructor
A new instance of Parser.
- #tables ⇒ Object
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.(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
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 |
#tables ⇒ Object
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 |