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



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

def initialize(target)
  if target.is_a?(String)
    arr    = target.gsub('.new.', '.allocate.').split('.')
    klass, method = arr[0..-2].join('.'), arr[-1]
    klass, method = based_on_klass_method(target) if arr.size == 1 # if hope view Class/Module source codes

    # 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, 'Could not find the method source location'
  else
    @file, line = @method.source_location
    @line ||= line
  end

end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



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

def file
  @file
end

#lineObject (readonly)

Returns the value of attribute line.



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

def line
  @line
end

#methodObject (readonly)

Returns the value of attribute method.



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

def method
  @method
end