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
# 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
  end
rescue Exception
  #catch errors for files ruby_parser fails on
  @locations
end

Instance Method Details

#in_method?(line_number) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
# File 'lib/base/line_numbers.rb', line 23

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



29
30
31
32
33
34
35
# File 'lib/base/line_numbers.rb', line 29

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
  return nil unless found_method_and_range
  found_method_and_range.first
end

#start_line_for_method(method) ⇒ Object



37
38
39
40
# File 'lib/base/line_numbers.rb', line 37

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