Class: RailsAiBridge::Tools::ModelDetails::SingleModelFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_ai_bridge/tools/model_details/single_model_formatter.rb

Overview

Renders complete detail for a single ActiveRecord model.

Constant Summary collapse

SOURCE_MACRO_KEYS =

Source-regex macros. Rendered as [INFERRED] — never [VERIFIED].

%i[
  has_secure_password encrypts normalizes has_one_attached
  has_many_attached has_rich_text broadcasts generates_token_for
  serialize store delegations delegate_missing_to
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(name:, data:) ⇒ SingleModelFormatter

Create a formatter for a single model using the provided introspection data. enums, scopes, callbacks, concerns, instance_methods, etc.). The values are stored on the instance without validation.

Parameters:

  • name (String)

    model class name

  • data (Hash)
  • name (String)
    • The model's class name.
  • data (Hash)
    • Introspection hash containing model details (table, associations, validations,


24
25
26
27
# File 'lib/rails_ai_bridge/tools/model_details/single_model_formatter.rb', line 24

def initialize(name:, data:)
  @name = name
  @data = data
end

Instance Method Details

#callString

Format model introspection data into a Markdown document.

The output includes the model name header and, when present in the input data, sections for table name, semantic tier (and tier reason), associations, validations, enums, scopes, callbacks, concerns, and up to the first 15 key instance methods.

Builds a Markdown document describing the given ActiveRecord model's introspected details. The output may include table name, semantic tier and reason, associations, validations, enums, scopes, callbacks, concerns, source macros, and up to 15 key instance methods depending on the data provided. Associations from ActiveRecord reflection are tagged [VERIFIED]; regex-extracted scopes and source macros are tagged [INFERRED].

Returns:

  • (String)

    The Markdown-formatted representation of the model.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rails_ai_bridge/tools/model_details/single_model_formatter.rb', line 43

def call
  lines = ["# #{@name}", '']
  lines << "**Table:** `#{@data[:table_name]}`" if @data[:table_name]
  if @data[:semantic_tier].present?
    lines << "**Semantic tier:** `#{@data[:semantic_tier]}`"
    lines << "**Tier reason:** #{@data[:semantic_tier_reason]}" if @data[:semantic_tier_reason].present?
  end

  if @data[:associations]&.any?
    lines << '' << '## Associations'
    @data[:associations].each do |a|
      line = "- `#{a[:type]}` **#{a[:name]}**"
      line += " (class: #{a[:class_name]})" if a[:class_name] && a[:class_name] != a[:name].to_s.classify
      line += " through: #{a[:through]}" if a[:through]
      line += ' [polymorphic]' if a[:polymorphic]
      line += " dependent: #{a[:dependent]}" if a[:dependent]
      lines << ConfidenceTag.tagged(line, a[:source] || :reflection)
    end
  end

  if @data[:validations]&.any?
    lines << '' << '## Validations'
    @data[:validations].each do |v|
      attrs = v[:attributes].join(', ')
      opts  = v[:options]&.any? ? " (#{v[:options].map { |k, val| "#{k}: #{val}" }.join(', ')})" : ''
      lines << "- `#{v[:kind]}` on #{attrs}#{opts}"
    end
  end

  if @data[:enums]&.any?
    lines << '' << '## Enums'
    @data[:enums].each do |attr, values|
      lines << "- `#{attr}`: #{values.join(', ')}"
    end
  end

  if @data[:scopes]&.any?
    lines << '' << '## Scopes'
    lines << @data[:scopes].map { |s| ConfidenceTag.tagged("- `#{s}`", :regex) }.join("\n")
  end

  if @data[:callbacks]&.any?
    lines << '' << '## Callbacks'
    @data[:callbacks].each do |type, methods|
      lines << "- `#{type}`: #{methods.join(', ')}"
    end
  end

  if @data[:concerns]&.any?
    lines << '' << '## Concerns'
    lines << @data[:concerns].map { |c| "- #{c}" }.join("\n")
  end

  if @data[:instance_methods]&.any?
    lines << '' << '## Key instance methods'
    lines << @data[:instance_methods].first(15).map { |m| "- `#{m}`" }.join("\n")
  end

  append_source_macros(lines)

  lines.join("\n")
end