Class: Mbeditor::SchemaService

Inherits:
Object
  • Object
show all
Defined in:
app/services/mbeditor/schema_service.rb

Overview

Parses db/schema.rb to extract the table definition for a given model name. Returns nil when the schema file is missing or the table is not found.

Return format:

{
  table:   "users",
  model:   "User",
  columns: [{ name: "id", type: "integer" }, ...],
  indexes: [{ name: "idx_users_on_email", columns: ["email"], unique: true }]
}

Constant Summary collapse

SCHEMA_PATH =
"db/schema.rb"

Instance Method Summary collapse

Constructor Details

#initialize(model_name, workspace_root) ⇒ SchemaService

Returns a new instance of SchemaService.



17
18
19
20
# File 'app/services/mbeditor/schema_service.rb', line 17

def initialize(model_name, workspace_root)
  @model_name     = model_name.to_s.strip
  @workspace_root = workspace_root.to_s
end

Instance Method Details

#callObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/services/mbeditor/schema_service.rb', line 22

def call
  return nil if @model_name.empty? || @workspace_root.empty?

  table_name  = derive_table_name(@model_name)
  schema_path = File.join(@workspace_root, SCHEMA_PATH)
  return nil unless File.exist?(schema_path)

  begin
    content = File.read(schema_path, encoding: "utf-8")
  rescue Errno::ENOENT, Errno::EACCES
    return nil
  end

  parse_table(content, table_name)
end