Class: CountLOC::LineCounter
- Inherits:
-
Object
- Object
- CountLOC::LineCounter
- Defined in:
- lib/countloc.rb
Overview
Class that gathers the metrics.
This class design & implementation is based heavily on Stefan Lang’s ScriptLines class from the “Ruby Cookbook” - Recipe 19.5 “Gathering Statistics About Your Code”.
Constant Summary collapse
- LINE_FORMAT =
'%8s %8s %8s %8s %12s %s'
Instance Attribute Summary collapse
-
#blank ⇒ Object
Returns the value of attribute blank.
-
#code ⇒ Object
Returns the value of attribute code.
-
#comments ⇒ Object
Returns the value of attribute comments.
-
#lines ⇒ Object
Returns the value of attribute lines.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
-
.columnNames ⇒ Object
Return an array containing the column names for the counters collected.
-
.headline ⇒ Object
Generate a string that contains the column headers for the metrics printed with to_s.
Instance Method Summary collapse
-
#+(other) ⇒ Object
Get a new LineCounter instance whose counters hold the sum of self and other.
- #comment_patterns(style) ⇒ Object
-
#initialize(name) ⇒ LineCounter
constructor
A new instance of LineCounter.
-
#read(io, style) ⇒ Object
Iterates over all the lines in io (io might be a file or a string), analyzes them and appropriately increases the counter attributes.
-
#to_a ⇒ Object
Get a formatted string containing all counter numbers and the name of this instance.
-
#to_s ⇒ Object
Get a formatted string containing all counter numbers and the name of this instance.
Constructor Details
#initialize(name) ⇒ LineCounter
Returns a new instance of LineCounter.
129 130 131 132 133 134 135 |
# File 'lib/countloc.rb', line 129 def initialize(name) @name = name @code = 0 @comments = 0 @blank = 0 @lines = 0 end |
Instance Attribute Details
#blank ⇒ Object
Returns the value of attribute blank.
67 68 69 |
# File 'lib/countloc.rb', line 67 def blank @blank end |
#code ⇒ Object
Returns the value of attribute code.
67 68 69 |
# File 'lib/countloc.rb', line 67 def code @code end |
#comments ⇒ Object
Returns the value of attribute comments.
67 68 69 |
# File 'lib/countloc.rb', line 67 def comments @comments end |
#lines ⇒ Object
Returns the value of attribute lines.
67 68 69 |
# File 'lib/countloc.rb', line 67 def lines @lines end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
66 67 68 |
# File 'lib/countloc.rb', line 66 def name @name end |
Class Method Details
.columnNames ⇒ Object
Return an array containing the column names for the counters collected.
125 126 127 |
# File 'lib/countloc.rb', line 125 def self.columnNames self.headline.split(' ', 6) end |
.headline ⇒ Object
Generate a string that contains the column headers for the metrics printed with to_s
118 119 120 |
# File 'lib/countloc.rb', line 118 def self.headline sprintf LINE_FORMAT, "LOC", "COMMENTS", "BLANK", "LINES", "CODE:COMMENT", "FILE" end |
Instance Method Details
#+(other) ⇒ Object
Get a new LineCounter instance whose counters hold the sum of self and other.
210 211 212 213 214 215 216 217 |
# File 'lib/countloc.rb', line 210 def +(other) sum = self.dup sum.code += other.code sum.comments += other.comments sum.blank += other.blank sum.lines += other.lines return sum end |
#comment_patterns(style) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/countloc.rb', line 69 def comment_patterns(style) patterns = { :ruby => { :single_line_full => /^\s*#/, :single_line_mixed => /#/, :multiline_begin => /=begin(\s|$)/, :multiline_end => /=end(\s|$)/, :blank_line => /^\s*$/ }, :vb => { :single_line_full => /^\s*\'/, :single_line_mixed => /\'/, :multiline_begin => /=begin(\s|$)/, # No VB multi-line comment :multiline_end => /=end(\s|$)/, # No VB multi-line comment :blank_line => /^\s*$/ }, :python => { :single_line_full => /^\s*#/, :single_line_mixed => /#/, :multiline_begin => /^\s*"""/, :multiline_end => /"""/, :blank_line => /^\s*$/ }, :cpp => { # C++ :single_line_full => /^\s*\/\//, :single_line_mixed => /\/\//, :multiline_begin => /\/\*/, :multiline_end => /\*\/\s*$/, :multiline_begin_mixed => /^[^\s]+.*\/\*/, :multiline_end_mixed => /\*\/\s*[^\s]+$/, :blank_line => /^\s*$/ } } patterns[:c] = patterns[:cpp] patterns[:csharp] = patterns[:cpp] patterns[:java] = patterns[:cpp] patterns[style] end |
#read(io, style) ⇒ Object
Iterates over all the lines in io (io might be a file or a string), analyzes them and appropriately increases the counter attributes.
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/countloc.rb', line 141 def read(io, style) in_multiline_comment = false io.each do |line| @lines += 1 # Process the line to avoid matching comment characters within quoted # strings or regular expressions. line.gsub!(/\'.*?\'/, "X") # Single quoted string line.gsub!(/[^\"]\"[^\"]+\"/, "X") # Double quoted string if style == :ruby line.gsub!(/\/.*?\//, "X") # Regular expression end patterns = comment_patterns(style) # In the event where the multiline_end pattern is the same as the # multiline_begin pattern, it is necessary to check for the ending # pattern first - otherwise we will never detect the ending pattern. if in_multiline_comment if patterns.include?(:multiline_end_mixed) and line =~ patterns[:multiline_end_mixed] @comments += 1 @code += 1 in_multiline_comment = false next elsif line =~ patterns[:multiline_end] @comments += 1 in_multiline_comment = false next end end case line when patterns[:multiline_begin] in_multiline_comment = true if patterns.include?(:multiline_begin_mixed) and line =~ patterns[:multiline_begin_mixed] @code += 1 end if line.sub(patterns[:multiline_begin], "X") =~ patterns[:multiline_end] in_multiline_comment = false end @comments += 1 when patterns[:blank_line] @blank += 1 when patterns[:single_line_full] @comments += 1 when patterns[:single_line_mixed] @comments += 1 @code += 1 else if in_multiline_comment @comments += 1 else @code += 1 end # if end # case end # read end |
#to_a ⇒ Object
Get a formatted string containing all counter numbers and the name of this instance.
232 233 234 |
# File 'lib/countloc.rb', line 232 def to_a self.to_s.split(' ', 6) end |
#to_s ⇒ Object
Get a formatted string containing all counter numbers and the name of this instance.
223 224 225 226 |
# File 'lib/countloc.rb', line 223 def to_s codeCommentRatio = (sprintf "%0.2f", @code.to_f/@comments if @comments > 0) || '--' sprintf LINE_FORMAT, @code, @comments, @blank, @lines, codeCommentRatio, @name end |