diff options
| -rw-r--r-- | axlsx.gemspec | 1 | ||||
| -rw-r--r-- | lib/axlsx.rb | 13 | ||||
| -rw-r--r-- | lib/axlsx/workbook/workbook.rb | 16 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/cell.rb | 5 | ||||
| -rw-r--r-- | test/tc_axlsx | 0 | ||||
| -rw-r--r-- | test/tc_axlsx.rb | 8 |
6 files changed, 36 insertions, 7 deletions
diff --git a/axlsx.gemspec b/axlsx.gemspec index e9b1f0a9..5da4d73f 100644 --- a/axlsx.gemspec +++ b/axlsx.gemspec @@ -18,7 +18,6 @@ Gem::Specification.new do |s| s.add_runtime_dependency 'rubyzip', '>= 1.3.0', '< 3' s.add_runtime_dependency "htmlentities", "~> 4.3", '>= 4.3.4' s.add_runtime_dependency "marcel", '~> 1.0' - s.add_runtime_dependency "activesupport" ### TODO: replace with local? s.add_development_dependency 'yard', "~> 0.9.8" s.add_development_dependency 'kramdown', '~> 2.3' diff --git a/lib/axlsx.rb b/lib/axlsx.rb index f2403893..c30fcf26 100644 --- a/lib/axlsx.rb +++ b/lib/axlsx.rb @@ -166,6 +166,19 @@ module Axlsx end end + # utility method for performing a deep merge on a Hash + # @param [Hash] Hash to merge into + # @param [Hash] Hash to be added + def self.hash_deep_merge(first_hash, second_hash) + first_hash.merge(second_hash) do |key, this_val, other_val| + if this_val.is_a?(Hash) && other_val.is_a?(Hash) + Axlsx.hash_deep_merge(this_val, other_val) + else + other_val + end + end + end + # Instructs the serializer to not try to escape cell value input. # This will give you a huge speed bonus, but if you content has <, > or other xml character data # the workbook will be invalid and excel will complain. diff --git a/lib/axlsx/workbook/workbook.rb b/lib/axlsx/workbook/workbook.rb index f9e857fc..417e15a3 100644 --- a/lib/axlsx/workbook/workbook.rb +++ b/lib/axlsx/workbook/workbook.rb @@ -196,7 +196,6 @@ require 'axlsx/workbook/worksheet/selection.rb' # A helper to apply styles that were added using `worksheet.add_style` # @return [Boolean] - require 'active_support/core_ext/hash/deep_merge' ### TODO: keep or replace with local solution def apply_styles return false if !styled_cells @@ -204,7 +203,7 @@ require 'axlsx/workbook/worksheet/selection.rb' current_style = styles.style_index[cell.style] if current_style - new_style = current_style.deep_merge(cell.raw_style) + new_style = Axlsx.hash_deep_merge(current_style, cell.raw_style) else new_style = cell.raw_style end @@ -419,5 +418,18 @@ require 'axlsx/workbook/worksheet/selection.rb' str << '</workbook>' end + private + + # Utility method for performing a deep merge on a Hash + def hash_deep_merge(first_hash, second_hash) + first_hash.merge(second_hash) do |key, this_val, other_val| + if this_val.is_a?(Hash) && other_val.is_a?(Hash) + this_val.deep_merge(other_val, &block) + else + other_val + end + end + end + end end diff --git a/lib/axlsx/workbook/worksheet/cell.rb b/lib/axlsx/workbook/worksheet/cell.rb index 626cf60f..9ab93e61 100644 --- a/lib/axlsx/workbook/worksheet/cell.rb +++ b/lib/axlsx/workbook/worksheet/cell.rb @@ -84,7 +84,6 @@ module Axlsx attr_accessor :raw_style - require 'active_support/core_ext/hash/deep_merge' ### TODO: can/should we remove this dependency # The index of the cellXfs item to be applied to this cell. # @param [Hash] styles # @see Axlsx::Styles @@ -92,9 +91,7 @@ module Axlsx def add_style(style) self.raw_style ||= {} - # using deep_merge from active_support: - # with regular Hash#merge adding borders fails miserably - new_style = raw_style.deep_merge(style) + new_style = Axlsx.hash_deep_merge(raw_style, style) all_edges = [:top, :right, :bottom, :left] diff --git a/test/tc_axlsx b/test/tc_axlsx new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/test/tc_axlsx diff --git a/test/tc_axlsx.rb b/test/tc_axlsx.rb index e58ebafe..3903fc42 100644 --- a/test/tc_axlsx.rb +++ b/test/tc_axlsx.rb @@ -132,4 +132,12 @@ class TestAxlsx < Test::Unit::TestCase nil_subject = InstanceValuesSubject.new(nil_obj: nil) assert_equal({"nil_obj" => nil}, Axlsx.instance_values_for(nil_subject), 'should return nil ivars') end + + def test_hash_deep_merge + h1 = {foo: {bar: true}} + h2 = {foo: {baz: true}} + assert_equal({foo: {baz: true}}, h1.merge(h2)) + assert_equal({foo: {bar: true, baz: true}}, Axlsx.hash_deep_merge(h1, h2)) + end + end |
