From f9401adeaa9efa2e1a2c3f64d6cdf61cd73e1cac Mon Sep 17 00:00:00 2001 From: "Andrew S. Brown" Date: Thu, 9 Oct 2014 22:22:50 -0700 Subject: [PATCH] fix addition for hashes with 2d keys --- lib/chartnado/series/wrap.rb | 10 +++++++--- spec/series_spec.rb | 5 +++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/chartnado/series/wrap.rb b/lib/chartnado/series/wrap.rb index b9c0b10..2e91342 100644 --- a/lib/chartnado/series/wrap.rb +++ b/lib/chartnado/series/wrap.rb @@ -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| @@ -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). @@ -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 diff --git a/spec/series_spec.rb b/spec/series_spec.rb index 06b98f0..33ac499 100644 --- a/spec/series_spec.rb +++ b/spec/series_spec.rb @@ -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})