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
|
# File 'lib/en14960/source_code.rb', line 11
def self.get_method_source(method_name, module_name, additional_methods = [])
base_dir = File.expand_path("..", __FILE__)
ruby_files = Dir.glob(File.join(base_dir, "**", "*.rb"))
file_path, line_number = find_method_in_files(ruby_files, method_name)
unless file_path
raise StandardError, "Source code not available for method: #{method_name}"
end
unless File.exist?(file_path)
raise StandardError, "Source file not found: #{file_path}"
end
lines = File.readlines(file_path, encoding: "UTF-8")
constants_code = ""
module_constants = get_module_constants(module_name, method_name)
if module_constants.any?
module_constants.each do |constant_name|
constant_def = (lines, constant_name)
constants_code += strip_consistent_indentation(constant_def) + "\n"
end
end
methods_code = ""
additional_methods.each do |additional_method|
if module_name.respond_to?(additional_method)
method_line_idx = lines.index { |line| line.strip =~ /^def\s+(self\.)?#{Regexp.escape(additional_method.to_s)}(\(|$|\s)/ }
if method_line_idx
additional_lines = (lines, method_line_idx, additional_method)
methods_code += strip_consistent_indentation(additional_lines.join("")) + "\n\n"
end
end
end
method_lines = (lines, line_number - 1, method_name)
methods_code += strip_consistent_indentation(method_lines.join(""))
output = ""
if constants_code.strip.length > 0
output += "# Related Constants:\n"
output += constants_code
output += "\n# Method Implementation:\n"
end
output += methods_code
output
end
|