Class: MetricFu::LineNumbers

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(contents, file_path = "") ⇒ LineNumbers

Parses ruby code to collect line numbers for class, module, and method definitions. Used by metrics that don’t provide line numbers for class, module, or methods problems

Parameters:

  • contents (String)

    a string of ruby code

  • file_path (String) (defaults to: "")

    the file path for the contents, defaults to empty string



10
11
12
13
14
15
16
17
18
# File 'lib/metric_fu/data_structures/line_numbers.rb', line 10

def initialize(contents, file_path = "")
  @file_path = file_path
  @locations = {}
  if contents.to_s.size.zero?
    mf_log "NON PARSEABLE INPUT: File is empty at path #{file_path.inspect}\n\t#{caller.join("\n\t")}"
  else
    parse_code(contents)
  end
end

Instance Attribute Details

#file_pathObject (readonly)

Returns the value of attribute file_path.



4
5
6
# File 'lib/metric_fu/data_structures/line_numbers.rb', line 4

def file_path
  @file_path
end

Instance Method Details

#in_method?(line_number) ⇒ Boolean

Returns if the given line number is in a method.

Parameters:

  • line_number (Integer)

Returns:

  • (Boolean)

    if the given line number is in a method



22
23
24
# File 'lib/metric_fu/data_structures/line_numbers.rb', line 22

def in_method?(line_number)
  not method_at_line(line_number) == :no_method_at_line
end

#method_at_line(line_number) ⇒ String?

For all collected locations, find the first location

where the line_number_range includes the line_number.
If a location is found, return the method name (first element)
Else return :no_method_at_line

Parameters:

  • line_number (Integer)

Returns:

  • (String, nil)

    the method which includes that line number, if any



32
33
34
35
36
37
# File 'lib/metric_fu/data_structures/line_numbers.rb', line 32

def method_at_line(line_number)
  default_proc = -> { [:no_method_at_line] }
  @locations.detect(default_proc) do |_method_name, line_number_range|
    line_number_range.include?(line_number)
  end.first
end

#start_line_for_method(method) ⇒ Integer?

Returns the line number at which the given method is defined.

Parameters:

  • method (String)

    the method name being queried

Returns:

  • (Integer, nil)

    the line number at which the given method is defined



41
42
43
44
# File 'lib/metric_fu/data_structures/line_numbers.rb', line 41

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