Class: Rails::CodeStatisticsCalculator
- Inherits:
-
Object
- Object
- Rails::CodeStatisticsCalculator
- Defined in:
- lib/rails/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]/, }, erb: { line_comment: %r{((^\s*<%#.*%>)|(<!--.*-->))}, }, css: { line_comment: %r{^\s*/\*.*\*/}, }, scss: { line_comment: %r{((^\s*/\*.*\*/)|(^\s*//))}, }, 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: /[-=]>/, } }
Instance Attribute Summary collapse
-
#classes ⇒ Object
readonly
Returns the value of attribute classes.
-
#code_lines ⇒ Object
readonly
Returns the value of attribute code_lines.
-
#lines ⇒ Object
readonly
Returns the value of attribute lines.
-
#methods ⇒ Object
readonly
Returns the value of attribute methods.
Instance Method Summary collapse
- #add(code_statistics_calculator) ⇒ Object
- #add_by_file_path(file_path) ⇒ Object
- #add_by_io(io, file_type) ⇒ Object
-
#initialize(lines = 0, code_lines = 0, classes = 0, methods = 0) ⇒ CodeStatisticsCalculator
constructor
A new instance of CodeStatisticsCalculator.
Constructor Details
#initialize(lines = 0, code_lines = 0, classes = 0, methods = 0) ⇒ CodeStatisticsCalculator
Returns a new instance of CodeStatisticsCalculator.
42 43 44 45 46 47 |
# File 'lib/rails/code_statistics_calculator.rb', line 42 def initialize(lines = 0, code_lines = 0, classes = 0, methods = 0) @lines = lines @code_lines = code_lines @classes = classes @methods = methods end |
Instance Attribute Details
#classes ⇒ Object (readonly)
Returns the value of attribute classes.
5 6 7 |
# File 'lib/rails/code_statistics_calculator.rb', line 5 def classes @classes end |
#code_lines ⇒ Object (readonly)
Returns the value of attribute code_lines.
5 6 7 |
# File 'lib/rails/code_statistics_calculator.rb', line 5 def code_lines @code_lines end |
#lines ⇒ Object (readonly)
Returns the value of attribute lines.
5 6 7 |
# File 'lib/rails/code_statistics_calculator.rb', line 5 def lines @lines end |
#methods ⇒ Object (readonly)
Returns the value of attribute methods.
5 6 7 |
# File 'lib/rails/code_statistics_calculator.rb', line 5 def methods @methods end |
Instance Method Details
#add(code_statistics_calculator) ⇒ Object
49 50 51 52 53 54 |
# File 'lib/rails/code_statistics_calculator.rb', line 49 def add(code_statistics_calculator) @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
56 57 58 59 60 |
# File 'lib/rails/code_statistics_calculator.rb', line 56 def add_by_file_path(file_path) File.open(file_path) do |f| add_by_io(f, file_type(file_path)) end end |
#add_by_io(io, file_type) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/rails/code_statistics_calculator.rb', line 62 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] && patterns[:end_block_comment].match?(line) comment_started = false end next else if patterns[:begin_block_comment] && patterns[:begin_block_comment].match?(line) comment_started = true next end end @classes += 1 if patterns[:class] && patterns[:class].match?(line) @methods += 1 if patterns[:method] && patterns[:method].match?(line) if !line.match?(/^\s*$/) && (patterns[:line_comment].nil? || !line.match?(patterns[:line_comment])) @code_lines += 1 end end end |