Module: Utils::Xt::SourceLocationExtension

Included in:
Object
Defined in:
lib/utils/xt/source_location_extension.rb

Constant Summary collapse

CLASS_METHOD_REGEXP =

Regular expression to match Ruby class method signatures Matches patterns like “ClassName#method” or “ClassName.method”

/\A([A-Z][\w:]+)([#.])([\w!?]+)/
FILE_LINENUMBER_REGEXP =

Regular expression to parse file path and line number information Matches patterns like “file.rb:123” or “file.rb:123-125”

/\A\s*([^:]+):(\d+)-?(\d+)?/

Instance Method Summary collapse

Instance Method Details

#source_locationArray<String, Integer>

The source_location method determines the file path and line number information for an object.

This method analyzes the object to extract source location details, handling different cases including string representations that contain file paths with line numbers, class method references, or simple file names. It returns an array containing the filename and line number, along with additional methods attached to the array for convenient access to location information.

Returns:

  • (Array<String, Integer>)

    an array containing the filename and line number, with additional methods attached for accessing filename, linenumber, and range properties



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
# File 'lib/utils/xt/source_location_extension.rb', line 24

def source_location
  filename   = nil
  linenumber = nil
  rangeend   = nil
  if respond_to?(:to_str)
    string = to_str
    case
    when string =~ FILE_LINENUMBER_REGEXP && File.exist?($1)
      filename   = $1
      linenumber = $2.to_i
      rangeend   = $3&.to_i
    when string =~ CLASS_METHOD_REGEXP && !File.exist?(string)
      klassname   = $1
      method_kind = $2 == '#' ? :instance_method : :method
      methodname  = $3
      filename, linenumber = ::Object.const_get(klassname).
        __send__(method_kind, methodname).source_location
    else
      filename = string
    end
  else
    filename = to_s
  end
  array = linenumber ? [ filename, linenumber ] : [ filename, 1 ]
  array_singleton_class = class << array; self; end
  array_singleton_class.instance_eval do
    define_method(:filename) { filename }
    define_method(:linenumber) { linenumber }
    define_method(:rangeend) { rangeend }
    define_method(:to_s) { [ filename, linenumber ].compact * ':' }
    define_method(:range) { rangeend ? "#{to_s}-#{rangeend}" : "#{to_s}" }
  end
  array
end