Skip to content

Commit

Permalink
fix addition for hashes with 2d keys
Browse files Browse the repository at this point in the history
  • Loading branch information
ashanbrown committed Oct 10, 2014
1 parent 41a539b commit f9401ad
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/chartnado/series/wrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def add(*series, scalar_sum: 0.0)
(series, scalars) = [__getobj__, *series].partition { |s| s.respond_to?(:map) }
scalar_sum += scalars.reduce(:+) || 0.0

if wrap(series.first).dimensions == 3
if has_separate_named_series?
result = series.map(&:to_a).flatten(1).group_by(&:first).map do |name, values|
data = values.map(&:second).reduce(Hash.new(scalar_sum)) do |hash, values|
values.each do |key, value|
Expand Down Expand Up @@ -77,8 +77,8 @@ def over(bottom, multiplier: 1.0, precision: 2)

if dimensions > bottom.dimensions
top_series_by_name = data_by_name
if hash_of_named_series? || array_of_named_series?
top_series_by_name.map do |name, top_values|
if has_separate_named_series?
data_by_name.map do |name, top_values|
[
name,
wrap(top_values).
Expand Down Expand Up @@ -185,6 +185,10 @@ def with_precision(precision, value)
def wrap(val)
self.class[val]
end

def has_separate_named_series?
hash_of_named_series? || array_of_named_series?
end
end
end
end
5 changes: 5 additions & 0 deletions spec/series_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ class << self
expect(series_sum(2,{'a' => {0 => 3}})).to eq ([['a', {0 => 5}]])
end
end
describe "adding a scalar to a hash with 2 dimensional keys" do
it "returns each item of the array with a scalar added" do
expect(series_sum({['a', 0] => 3}, 1)).to eq ({['a', 0] => 4})
end
end
describe "adding two hashes" do
it "returns each item of the array with a scalar added" do
expect(series_sum({0 => 1},{0 => 2})).to eq ({0 => 3})
Expand Down

0 comments on commit f9401ad

Please sign in to comment.