lib/metrics_collector.rb - C0 code coverage information

### C0 code coverage information

Generated on Fri Aug 10 13:52:34 -0300 2007 with rcov 0.7.0

    Code reported as executed by Ruby looks like this...
    and this: this line is also marked as covered.
    Lines considered as run by rcov, but not reported by Ruby, look like this,
    and this: these lines were inferred by rcov (using simple heuristics).
    Finally, here's a line marked as not executed.

Name | Total lines | Lines of code | Total coverage | Code coverage
lib/metrics_collector.rb | `73` | `60` |
`100.0%` |
`100.0%` |

     1 require 'set'
     2
     3 class MetricsCollector
     4   def initialize
     5     @ids = Set.new
     6     @values = {}
     7   end
     8
     9   def new_metric(id, value)
    10     if !is_test? id
    11       process_metric(id, value)
    12     end
    13   end
    14
    15   def add_metric(id, value)
    16     if @ids.include?(id)
    17       process_metric(id, value, true)
    18     else
    19       new_metric(id, value)
    20     end
    21   end
    22
    23   def metrics
    24     @ids.collect do |id|
    25       {:id => id, :value => @values[id]}
    26     end
    27   end
    28
    29   def average
    30     total = metrics.inject(0) do |sum, metric|
    31       sum + metric[:value]
    32     end
    33     total/metrics.size
    34   end
    35
    36   def std_dev
    37     mean = average
    38     total = metrics.inject(0) do |sum, metric|
    39       sum + (metric[:value] - mean)**2
    40     end
    41     Math.sqrt(total/(metrics.size - 1))
    42   end
    43
    44   def min
    45     minMetric = metrics.min do |a, b|
    46       a[:value] <=> b[:value]
    47     end
    48     minMetric[:value]
    49   end
    50
    51   def max
    52     maxMetric = metrics.max do |a, b|
    53       a[:value] <=> b[:value]
    54     end
    55     maxMetric[:value]
    56   end
    57
    58   def result
    59     "# Average\tStdDev\tMin\tMax\tClasses: #{metrics.size}\n#{average}\t#{std_dev}\t#{min}\t#{max}"
    60   end
    61
    62   private
    63
    64   def process_metric(id, value, add=false)
    65     @ids.add(id)
    66     @values[id] = add ? @values[id] + value : value
    67   end
    68
    69   def is_test?(id)
    70     id =~ /Test/i
    71   end
    72
    73 end

Generated using the rcov code coverage analysis tool for Ruby version 0.7.0.