Class: FeatureMap::Private::LinesOfCodeCalculator

Inherits:
Object
  • Object
show all
Defined in:
lib/feature_map/private/lines_of_code_calculator.rb

Constant Summary collapse

SINGLE_LINE_COMMENT_PATTERN =

NOTE: regex ‘x’ arg ignores whitespace within the construction of the regex.

regex 'm' arg allows the regex to _execute_ on multiline strings.
/
  \s* # Any amount of whitespace
  (#{Constants::SINGLE_LINE_COMMENT_PATTERNS.join('|')}) # Any comment start
  .* # And the rest of the line
/x.freeze
MULTI_LINE_COMMENT_PATTERN =
/
  (#{Constants::MULTILINE_COMMENT_START_PATTERNS.join('|')}) # Multiline comment start
  .*? # Everything in between, but lazily so we stop when we hit...
  (#{Constants::MULTILINE_COMMENT_END_PATTERNS.join('|')}) # ...Multiline comment end
/xm.freeze

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ LinesOfCodeCalculator

Returns a new instance of LinesOfCodeCalculator.



22
23
24
# File 'lib/feature_map/private/lines_of_code_calculator.rb', line 22

def initialize(file_path)
  @file_path = file_path
end

Instance Method Details

#calculateObject



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/feature_map/private/lines_of_code_calculator.rb', line 26

def calculate
  # Ignore lines that are entirely whitespace or that are entirely a comment.
  File
    .readlines(@file_path)
    .join("\n")
    .gsub(SINGLE_LINE_COMMENT_PATTERN, '')
    .gsub(MULTI_LINE_COMMENT_PATTERN, '')
    .split("\n")
    .reject { |l| l.strip == '' }
    .size
end