Class: RailsPulse::Generators::SchemaParser

Inherits:
Object
  • Object
show all
Defined in:
lib/generators/rails_pulse/schema_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema_path) ⇒ SchemaParser

Returns a new instance of SchemaParser.



10
11
12
# File 'lib/generators/rails_pulse/schema_parser.rb', line 10

def initialize(schema_path)
  @schema_path = schema_path
end

Instance Attribute Details

#schema_pathObject (readonly)

Returns the value of attribute schema_path.



8
9
10
# File 'lib/generators/rails_pulse/schema_parser.rb', line 8

def schema_path
  @schema_path
end

Instance Method Details

#extract_expected_schemaObject

Parse schema to extract expected columns for all tables Returns hash of table_name => { column_name => { type:, comment: } }



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/generators/rails_pulse/schema_parser.rb', line 30

def extract_expected_schema
  return {} unless File.exist?(schema_path)

  schema_content = File.read(schema_path)
  expected_columns = {}

  # Find each create_table block and parse contents
  table_blocks = schema_content.scan(
    /connection\.create_table\s+:(\w+).*?do\s*\|t\|(.*?)(?:connection\.(?:add_index|create_table)|\z)/m
  )

  table_blocks.each do |table_name, table_block|
    columns = parse_table_columns(table_block)
    expected_columns[table_name] = columns if columns.any?
  end

  expected_columns
end

#extract_table_namesObject

Extract table names from schema file Returns array of table names from required_tables or fallback to default list



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/generators/rails_pulse/schema_parser.rb', line 16

def extract_table_names
  return BaseMethods::RAILS_PULSE_TABLES unless File.exist?(schema_path)

  schema_content = File.read(schema_path)
  if match = schema_content.match(/required_tables\s*=\s*\[(.*?)\]/m)
    table_names = match[1].scan(/:(\w+)/).flatten
    return table_names.map(&:to_s)
  end

  BaseMethods::RAILS_PULSE_TABLES
end