Class: EagerEye::SchemaParser

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

Overview

Parses db/schema.rb to learn the real column names of each table. Columns are the dominant false-positive source: when a receiver’s model can’t be inferred, the name heuristics flag any method whose name collides with an association name — but a DB column (‘comsn_rate`, `vat_rate`, `service_fee_rate`) is never an association. Knowing the column names lets the detectors disambiguate.

Schema lookup walks up from the analyzed path so ‘eager_eye app/` still finds `<root>/db/schema.rb`. Parsing is AST-based (a `create_table “x” do |t| … end` block whose body holds `t.<type> “col”` / `t.column “col”` / `t.references “y”`).

Constant Summary collapse

COLUMN_DEFINERS =
%i[
  string text integer bigint float decimal boolean date datetime time timestamp
  binary json jsonb uuid inet cidr money hstore citext virtual primary_key column
].freeze
REFERENCE_DEFINERS =
%i[references belongs_to].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSchemaParser

Returns a new instance of SchemaParser.



24
25
26
27
# File 'lib/eager_eye/schema_parser.rb', line 24

def initialize
  @columns_by_table = {}
  @all_columns = Set.new
end

Instance Attribute Details

#all_columnsObject (readonly)

Returns the value of attribute all_columns.



22
23
24
# File 'lib/eager_eye/schema_parser.rb', line 22

def all_columns
  @all_columns
end

#columns_by_tableObject (readonly)

Returns the value of attribute columns_by_table.



22
23
24
# File 'lib/eager_eye/schema_parser.rb', line 22

def columns_by_table
  @columns_by_table
end

Instance Method Details

#columns_by_modelObject

Column names mapped onto the model a table name classifies to (“eod_items” -> “EodItem”). Used for per-model column resolution.



45
46
47
# File 'lib/eager_eye/schema_parser.rb', line 45

def columns_by_model
  @columns_by_model ||= @columns_by_table.transform_keys { |table| classify(table) }
end

#parse_from_path(start_path) ⇒ Object

Returns true if a schema was found and parsed.



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/eager_eye/schema_parser.rb', line 30

def parse_from_path(start_path)
  schema = locate_schema(start_path)
  return false unless schema

  ast = parse(File.read(schema))
  return false unless ast

  walk(ast)
  true
rescue Errno::ENOENT, Errno::EACCES
  false
end