Class: RailsAiContext::Hydrators::SchemaHintBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_ai_context/hydrators/schema_hint_builder.rb

Overview

Builds SchemaHint objects from cached introspection context. Single point of truth for resolving a model name into a structured hydration payload.

Class Method Summary collapse

Class Method Details

.build(model_name, context:) ⇒ Object

Build a SchemaHint for a single model name. Returns nil if the model is not found in context.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rails_ai_context/hydrators/schema_hint_builder.rb', line 11

def self.build(model_name, context:)
  models_data = context[:models]
  schema_data = context[:schema]
  return nil unless models_data.is_a?(Hash) && schema_data.is_a?(Hash)

  # Find model in models context (case-insensitive)
  model_key = models_data.keys.find { |k| k.to_s.casecmp?(model_name) }
  return nil unless model_key

  model_info = models_data[model_key]
  return nil unless model_info.is_a?(Hash)

  table_name = model_info[:table_name]
  table_data = schema_data.dig(:tables, table_name) if table_name

  columns = if table_data
    (table_data[:columns] || []).map do |col|
      { name: col[:name], type: col[:type], null: col[:null] }.compact
    end
  else
    []
  end

  associations = (model_info[:associations] || []).map do |assoc|
    {
      name: assoc[:name],
      type: assoc[:type],
      class_name: assoc[:class_name],
      foreign_key: assoc[:foreign_key]
    }.compact
  end

  validations = (model_info[:validations] || []).map do |val|
    {
      kind: val[:kind],
      attributes: val[:attributes]
    }.compact
  end

  primary_key = table_data&.dig(:primary_key) || "id"

  # Confidence: verified if we have both model data and schema table
  confidence = table_data ? "[VERIFIED]" : "[INFERRED]"

  SchemaHint.new(
    model_name: model_key.to_s,
    table_name: table_name.to_s,
    columns: columns,
    associations: associations,
    validations: validations,
    primary_key: primary_key.to_s,
    confidence: confidence
  )
end

.build_many(model_names, context:, max: 5) ⇒ Object

Build SchemaHints for multiple model names. Returns only the ones that resolved successfully.



68
69
70
# File 'lib/rails_ai_context/hydrators/schema_hint_builder.rb', line 68

def self.build_many(model_names, context:, max: 5)
  model_names.first(max).filter_map { |name| build(name, context: context) }
end