Class: Tidewave::Tools::GetModels

Inherits:
Tidewave::Tool show all
Defined in:
lib/tidewave/tools/get_models.rb

Constant Summary collapse

DESCRIPTION =
<<~DESCRIPTION.freeze
  Returns a list of all database-backed models in the application.
DESCRIPTION

Instance Method Summary collapse

Methods inherited from Tidewave::Tool

descendants, inherited, #validate_and_call

Constructor Details

#initialize(options = {}) ⇒ GetModels

Returns a new instance of GetModels.



10
11
12
13
14
# File 'lib/tidewave/tools/get_models.rb', line 10

def initialize(options = {})
  @root = options[:root] ? Pathname.new(options[:root].to_s) : Pathname.pwd
  @database_adapter = Tidewave::DatabaseAdapter.for(options[:orm_adapter]) if options[:orm_adapter]
  @before_reload = options[:before_reload]
end

Instance Method Details

#call(_arguments) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/tidewave/tools/get_models.rb', line 29

def call(_arguments)
  @before_reload&.call

  models = @database_adapter.get_models

  models.map do |model|
    display_name = model.name || model.to_s

    if location = get_relative_source_location(model.name)
      "* #{display_name} at #{location}"
    else
      "* #{display_name}"
    end
  end.join("\n")
end

#definitionObject



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/tidewave/tools/get_models.rb', line 16

def definition
  return nil unless @database_adapter

  {
    "name" => "get_models",
    "description" => DESCRIPTION,
    "inputSchema" => {
      "type" => "object",
      "properties" => {}
    }
  }
end