Class: Sloc::Analyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/sloc/analyzer.rb

Constant Summary collapse

REPORT_KEYS =
[:total, :empty_lines, :single_comment]

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Analyzer

Returns a new instance of Analyzer.



3
4
5
# File 'lib/sloc/analyzer.rb', line 3

def initialize(options = {})
  @options = options
end

Instance Method Details

#analyze(code, extension) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/sloc/analyzer.rb', line 9

def analyze(code, extension)
  result = {}

  code.scrub!.gsub!(/\r\n|\r/, "\n")

  result[:total]          = code.scan("\n").size
  result[:empty_lines]    = code.scan(/^\s*$/).size

  regexp = single_comment_expression(extension)
  result[:single_comment] = regexp ? code.scan(regexp).size : 0

  result
end

#block_comment_expression(extension) ⇒ Object



40
41
42
43
44
45
# File 'lib/sloc/analyzer.rb', line 40

def block_comment_expression(extension)
  case extension
  when '.rb' then [/=begin/, /=end/]
  else [nil, nil]
  end
end

#comment_expressions(extension) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/sloc/analyzer.rb', line 23

def comment_expressions(extension)
  start, stop = block_comment_expression(extension)

  {
    single: single_comment_expression(extension),
    start: start,
    stop: stop,
  }
end

#single_comment_expression(extension) ⇒ Object



33
34
35
36
37
38
# File 'lib/sloc/analyzer.rb', line 33

def single_comment_expression(extension)
  case extension
  when '.rb' then /#/
  when '.vim' then /"/
  end
end