Class: MetricFu::LineNumbers

Inherits:
Object
  • Object
show all
Defined in:
lib/base/line_numbers.rb

Instance Method Summary collapse

Constructor Details

#initialize(contents) ⇒ LineNumbers

Returns a new instance of LineNumbers.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/base/line_numbers.rb', line 5

def initialize(contents)
  rp = RubyParser.new
  @locations = {}
  file_sexp = rp.parse(contents)
  case file_sexp[0]
  when :class
    process_class(file_sexp)
  when :module
    process_module(file_sexp)
  when :block
    file_sexp.each_of_type(:class) { |sexp| process_class(sexp) }
  else
    puts "Unexpected sexp_type #{file_sexp[0].inspect}"
  end
rescue Exception => e
  #catch errors for files ruby_parser fails on
  puts "RUBY PARSE FAILURE: #{e.class}\t#{e.message}\t#{file_sexp.inspect}\t#{contents.inspect}\t#{e.backtrace}"
  @locations
end

Instance Method Details

#in_method?(line_number) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
# File 'lib/base/line_numbers.rb', line 25

def in_method? line_number
  !!@locations.detect do |method_name, line_number_range|
    line_number_range.include?(line_number)
  end
end

#method_at_line(line_number) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/base/line_numbers.rb', line 31

def method_at_line line_number
  found_method_and_range = @locations.detect do |method_name, line_number_range|
    line_number_range.include?(line_number)
  end
  if found_method_and_range
    found_method_and_range.first
  else
    nil
  end
end

#start_line_for_method(method) ⇒ Object



42
43
44
45
# File 'lib/base/line_numbers.rb', line 42

def start_line_for_method(method)
  return nil unless @locations.has_key?(method)
  @locations[method].first
end