@@ -19,7 +19,9 @@ class Statistics
1919 # @parameter coverage [Covered::Coverage] The coverage object to summarize.
2020 # @returns [Covered::Statistics] Statistics containing the given coverage.
2121 def self . for ( coverage )
22- self . new ( Aggregate . new ( [ coverage ] ) )
22+ self . new . tap do |statistics |
23+ statistics << coverage
24+ end
2325 end
2426
2527 # Immutable aggregate coverage statistics.
@@ -42,7 +44,6 @@ def initialize(coverages = [])
4244 end
4345
4446 paths . each_value ( &:freeze )
45- @paths = paths . freeze
4647
4748 @count = paths . size
4849 @executable_count = paths . sum { |_path , coverage | coverage . executable_count }
@@ -63,23 +64,6 @@ def initialize(coverages = [])
6364 # @returns [Integer] The executed line count.
6465 attr :executed_count
6566
66- # @attribute [Hash(String, Covered::Coverage)] Coverage statistics indexed by path.
67- attr :paths
68-
69- # Add coverage to a new aggregate statistics object.
70- # @parameter coverage [Covered::Coverage] The coverage object to add.
71- # @returns [Covered::Statistics::Aggregate] The new aggregate statistics.
72- def with ( coverage )
73- self . class . new ( @paths . values + [ coverage ] )
74- end
75-
76- # Get coverage for the given path.
77- # @parameter path [String] The source path.
78- # @returns [Covered::Coverage | Nil] The merged coverage for the path.
79- def []( path )
80- @paths [ path ]
81- end
82-
8367 # A JSON-compatible representation of these aggregate statistics.
8468 # @returns [Hash] The aggregate count, line counts and percentage.
8569 def as_json
@@ -99,31 +83,25 @@ def to_json(options)
9983 end
10084 end
10185
102- # Initialize coverage statistics.
103- # @parameter aggregate [Covered::Statistics::Aggregate] The aggregate coverage statistics.
104- def initialize ( aggregate = Aggregate . new )
105- @aggregate = aggregate
86+ # Initialize empty coverage statistics.
87+ def initialize
88+ @total = nil
89+ @paths = Hash . new
10690 end
10791
108- # @attribute [Covered::Statistics::Aggregate] The aggregate coverage statistics.
109- attr :aggregate
110-
11192 # The total aggregate statistics.
11293 # @returns [Covered::Statistics::Aggregate] The total aggregate statistics.
11394 def total
114- @aggregate
95+ @total ||= Aggregate . new ( @paths . values )
11596 end
11697
117- # Coverage statistics indexed by path.
118- # @returns [Hash(String, Covered::Coverage)] The coverage statistics indexed by path.
119- def paths
120- @aggregate . paths
121- end
98+ # @attribute [Hash(String, Covered::Coverage)] Coverage statistics indexed by path.
99+ attr :paths
122100
123101 # The number of unique paths with coverage data.
124102 # @returns [Integer] The number of unique paths.
125103 def count
126- @aggregate . count
104+ @paths . size
127105 end
128106
129107 # The total number of executable lines.
@@ -141,7 +119,15 @@ def executed_count
141119 # Add coverage to these statistics.
142120 # @parameter coverage [Covered::Coverage] The coverage object to add.
143121 def << coverage
144- @aggregate = @aggregate . with ( coverage )
122+ current = @paths [ coverage . path ]
123+
124+ unless current
125+ current = @paths [ coverage . path ] = coverage . empty
126+ end
127+
128+ current . merge! ( coverage )
129+
130+ @total = nil
145131
146132 return self
147133 end
@@ -150,7 +136,7 @@ def << coverage
150136 # @parameter path [String] The source path.
151137 # @returns [Covered::Coverage | Nil] The merged coverage for the path.
152138 def []( path )
153- @aggregate [ path ]
139+ @paths [ path ]
154140 end
155141
156142 # A JSON-compatible representation of these statistics.
0 commit comments