Module: Utils::Xt::SourceLocationExtension

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

Constant Summary collapse

CLASS_METHOD_REGEXP =
/\A([A-Z][\w:]+)([#.])([\w!?]+)/
FILE_LINENUMBER_REGEXP =
/\A\s*([^:]+):(\d+)-?(\d+)?/

Instance Method Summary collapse

Instance Method Details

#source_locationObject



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

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 =
        deep_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