Class: ShowCode::SourceLocation

Inherits:
Object
  • Object
show all
Defined in:
lib/show_code/source_location.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ SourceLocation

Returns a new instance of SourceLocation.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/show_code/source_location.rb', line 7

def initialize(target)
  if target.is_a?(String)
    arr    = target.gsub('.new.', '.allocate.').split('.')
    klass  = arr[0..-2].join('.')
    method = arr[-1]

    # refer:
    # Object.instance_method(:method).bind(User).call(:current).source_location
    # Object.instance_method(:method).bind(User.allocate).call(:name).source_location
    # Object.instance_method(:method).bind(User.second.school.leader).call(:name).source_location

    @method = Object.instance_method(:method).bind(eval(klass)).call(method)
  elsif target.is_a?(Method) || target.is_a?(UnboundMethod)
    @method = target
  else
    raise ArgumentError, 'bad argument (expected Method object or Chaining string)'
  end

  if @method.source_location.nil?
    raise SourceLocationError, 'cannot find the method source location'
  else
    @file, @line = @method.source_location
  end

end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



5
6
7
# File 'lib/show_code/source_location.rb', line 5

def file
  @file
end

#lineObject (readonly)

Returns the value of attribute line.



5
6
7
# File 'lib/show_code/source_location.rb', line 5

def line
  @line
end

#methodObject (readonly)

Returns the value of attribute method.



5
6
7
# File 'lib/show_code/source_location.rb', line 5

def method
  @method
end