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.

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,


17
18
19
20
# File 'lib/rails_ai_bridge/tools/model_details/single_model_formatter.rb', line 17

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, and up to 15 key instance methods depending on the data provided.

Returns:

  • (String)

    The Markdown-formatted representation of the model.



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
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
# File 'lib/rails_ai_bridge/tools/model_details/single_model_formatter.rb', line 34

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 << line
    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| "- `#{s}`" }.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

  lines.join("\n")
end