Class: Tidewave::Tools::GetSourceLocation
- Defined in:
- lib/tidewave/tools/get_source_location.rb
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.get_source_location(reference) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/tidewave/tools/get_source_location.rb', line 58 def self.get_source_location(reference) constant_path, selector, method_name = reference.rpartition(/\.|#/) # There are no selectors, so the method_name is a constant path return Object.const_source_location(method_name) if selector.empty? begin mod = Object.const_get(constant_path) rescue NameError => e raise e rescue raise "wrong or invalid reference #{reference}" end raise "reference #{constant_path} does not point a class/module" unless mod.is_a?(Module) if selector == "#" mod.instance_method(method_name).source_location else mod.method(method_name).source_location end end |
Instance Method Details
#call(reference:) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/tidewave/tools/get_source_location.rb', line 24 def call(reference:) # Check if this is a package location request if reference.start_with?("dep:") package_name = reference.gsub("dep:", "") return get_package_location(package_name) end file_path, line_number = self.class.get_source_location(reference) if file_path begin relative_path = Pathname.new(file_path).relative_path_from(Rails.root) "#{relative_path}:#{line_number}" rescue ArgumentError # If the path cannot be made relative, return the absolute path "#{file_path}:#{line_number}" end else raise NameError, "could not find source location for #{reference}" end end |
#get_package_location(package) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/tidewave/tools/get_source_location.rb', line 46 def get_package_location(package) raise "dep: prefix only works with projects using Bundler" unless defined?(Bundler) specs = Bundler.load.specs spec = specs.find { |s| s.name == package } if spec spec.full_gem_path else raise "Package #{package} not found. Check your Gemfile for available packages." end end |