Class: Rails::Hyperdrive::Tools::LocateSource
- Inherits:
-
Base
- Object
- MCP::Tool
- Base
- Rails::Hyperdrive::Tools::LocateSource
show all
- Defined in:
- lib/rails/hyperdrive/tools/locate_source.rb
Class Method Summary
collapse
Methods inherited from Base
respond_error, respond_json, respond_text, with_dev_guard
Class Method Details
.call(reference:, server_context: nil) ⇒ Object
17
18
19
20
21
|
# File 'lib/rails/hyperdrive/tools/locate_source.rb', line 17
def self.call(reference:, server_context: nil)
with_dev_guard do
resolve(reference.to_s.strip)
end
end
|
.constantize(name) ⇒ Object
76
77
78
79
80
81
82
|
# File 'lib/rails/hyperdrive/tools/locate_source.rb', line 76
def self.constantize(name)
if defined?(::ActiveSupport) && String.method_defined?(:constantize)
name.constantize
else
name.split("::").inject(Object) { |m, c| m.const_get(c) }
end
end
|
.instance_method_defined?(klass, method) ⇒ Boolean
64
65
66
67
68
|
# File 'lib/rails/hyperdrive/tools/locate_source.rb', line 64
def self.instance_method_defined?(klass, method)
sym = method.to_sym
klass.instance_methods(true).include?(sym) ||
klass.private_instance_methods(true).include?(sym)
end
|
.resolve(reference) ⇒ Object
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
|
# File 'lib/rails/hyperdrive/tools/locate_source.rb', line 23
def self.resolve(reference)
if reference.start_with?("dep:")
return resolve_gem(reference.sub(/\Adep:/, ""), reference)
end
if reference.include?("#")
klass_name, method = reference.split("#", 2)
klass = constantize(klass_name)
unless instance_method_defined?(klass, method)
return respond_error("could not resolve: #{reference}")
end
file, line = klass.instance_method(method.to_sym).source_location
file ? respond_text("#{file}:#{line}") : respond_error("could not resolve: #{reference}")
elsif reference.include?(".")
klass_name, method = reference.split(".", 2)
klass = constantize(klass_name)
unless klass.respond_to?(method, true)
return respond_error("could not resolve: #{reference}")
end
file, line = klass.method(method.to_sym).source_location
file ? respond_text("#{file}:#{line}") : respond_error("could not resolve: #{reference}")
else
owner_name, const_name = split_const(reference)
owner = constantize(owner_name)
return respond_error("could not resolve: #{reference}") unless owner.respond_to?(:const_source_location)
location = owner.const_source_location(const_name)
location && location.first ? respond_text("#{location[0]}:#{location[1]}") : respond_error("could not resolve: #{reference}")
end
rescue NameError
respond_error("could not resolve: #{reference}")
end
|
.resolve_gem(name, reference) ⇒ Object
70
71
72
73
74
|
# File 'lib/rails/hyperdrive/tools/locate_source.rb', line 70
def self.resolve_gem(name, reference)
spec = Gem::Specification.find_all_by_name(name).first
return respond_error("could not resolve: #{reference}") unless spec
respond_text(spec.full_gem_path)
end
|
.split_const(reference) ⇒ Object
55
56
57
58
59
60
61
62
|
# File 'lib/rails/hyperdrive/tools/locate_source.rb', line 55
def self.split_const(reference)
if reference.include?("::")
parts = reference.split("::")
[parts[0..-2].join("::"), parts.last]
else
["Object", reference]
end
end
|