Class: RailsStats::CodeStatisticsCalculator

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_stats/code_statistics_calculator.rb

Overview

:nodoc:

Constant Summary collapse

PATTERNS =
{
  rb: {
    line_comment: /^\s*#/,
    begin_block_comment: /^=begin/,
    end_block_comment: /^=end/,
    class: /^\s*class\s+[_A-Z]/,
    method: /^\s*def\s+[_a-z]/,
  },
  js: {
    line_comment: %r{^\s*//},
    begin_block_comment: %r{^\s*/\*},
    end_block_comment: %r{\*/},
    method: /function(\s+[_a-zA-Z][\da-zA-Z]*)?\s*\(/,
  },
  coffee: {
    line_comment: /^\s*#/,
    begin_block_comment: /^\s*###/,
    end_block_comment: /^\s*###/,
    class: /^\s*class\s+[_A-Z]/,
    method: /[-=]>/,
  },
  feature: {
    class: /^\s*Feature:/,
    method: /^\s*Scenario:/,
  }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(test = false) ⇒ CodeStatisticsCalculator

Returns a new instance of CodeStatisticsCalculator.



34
35
36
37
38
39
40
41
# File 'lib/rails_stats/code_statistics_calculator.rb', line 34

def initialize(test=false)
  @test = test
  @files_total = 0
  @lines = 0
  @code_lines = 0
  @classes = 0
  @methods = 0
end

Instance Attribute Details

#classesObject (readonly)

Returns the value of attribute classes.



5
6
7
# File 'lib/rails_stats/code_statistics_calculator.rb', line 5

def classes
  @classes
end

#code_linesObject (readonly)

Returns the value of attribute code_lines.



5
6
7
# File 'lib/rails_stats/code_statistics_calculator.rb', line 5

def code_lines
  @code_lines
end

#files_totalObject (readonly)

Returns the value of attribute files_total.



5
6
7
# File 'lib/rails_stats/code_statistics_calculator.rb', line 5

def files_total
  @files_total
end

#linesObject (readonly)

Returns the value of attribute lines.



5
6
7
# File 'lib/rails_stats/code_statistics_calculator.rb', line 5

def lines
  @lines
end

#methodsObject (readonly)

Returns the value of attribute methods.



5
6
7
# File 'lib/rails_stats/code_statistics_calculator.rb', line 5

def methods
  @methods
end

#testObject (readonly)

Returns the value of attribute test.



5
6
7
# File 'lib/rails_stats/code_statistics_calculator.rb', line 5

def test
  @test
end

Instance Method Details

#add(code_statistics_calculator) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/rails_stats/code_statistics_calculator.rb', line 43

def add(code_statistics_calculator)
  @files_total += code_statistics_calculator.files_total
  @lines += code_statistics_calculator.lines
  @code_lines += code_statistics_calculator.code_lines
  @classes += code_statistics_calculator.classes
  @methods += code_statistics_calculator.methods
end

#add_by_file_path(file_path) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/rails_stats/code_statistics_calculator.rb', line 51

def add_by_file_path(file_path)
  @files_total += 1

  File.open(file_path) do |f|
    self.add_by_io(f, file_type(file_path))
  end
end

#add_by_io(io, file_type) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rails_stats/code_statistics_calculator.rb', line 59

def add_by_io(io, file_type)
  patterns = PATTERNS[file_type] || {}

  comment_started = false

  while line = io.gets
    @lines += 1

    if comment_started
      if patterns[:end_block_comment] && line =~ patterns[:end_block_comment]
        comment_started = false
      end
      next
    else
      if patterns[:begin_block_comment] && line =~ patterns[:begin_block_comment]
        comment_started = true
        next
      end
    end

    @classes   += 1 if patterns[:class] && line =~ patterns[:class]
    @methods   += 1 if patterns[:method] && line =~ patterns[:method]
    if line !~ /^\s*$/ && (patterns[:line_comment].nil? || line !~ patterns[:line_comment])
      @code_lines += 1
    end
  end
end