From 216996bd076f4f3549155774dbccab4b3c634cd0 Mon Sep 17 00:00:00 2001 From: Ryan Winograd Date: Sat, 29 Aug 2020 09:42:47 -0500 Subject: Update #serialize to accept 3 arguments --- lib/axlsx/package.rb | 13 +++++++++---- test/tc_package.rb | 12 ++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/axlsx/package.rb b/lib/axlsx/package.rb index 6b55091b..d9865a48 100644 --- a/lib/axlsx/package.rb +++ b/lib/axlsx/package.rb @@ -100,8 +100,8 @@ module Axlsx # # Serialize to a stream # s = p.to_stream() # File.open('example_streamed.xlsx', 'w') { |f| f.write(s.read) } - def serialize(output, options = {}) - confirm_valid, zip_command = parse_serialize_options(options) + def serialize(output, options = {}, secondary_options = nil) + confirm_valid, zip_command = parse_serialize_options(options, secondary_options) return false unless !confirm_valid || self.validate.empty? zip_provider = if zip_command ZipCommand.new(zip_command) @@ -366,8 +366,13 @@ module Axlsx # @return [Boolean, (String or nil)] Returns an array where the first value is # `confirm_valid` and the second is the `zip_command`. # @private - def parse_serialize_options(options) + def parse_serialize_options(options, secondary_options) + if secondary_options + warn "[DEPRECATION] Axlsx::Package#serialize with 3 arguments is deprecated. " + + "Use keyword args instead e.g., package.serialize(output, confirm_valid: false, zip_command: 'zip')" + end if options.is_a?(Hash) + options.merge!(secondary_options || {}) invalid_keys = options.keys - [:confirm_valid, :zip_command] if invalid_keys.any? raise ArgumentError.new("Invalid keyword arguments: #{invalid_keys}") @@ -376,7 +381,7 @@ module Axlsx else warn "[DEPRECATION] Axlsx::Package#serialize with confirm_valid as a boolean is deprecated. " + "Use keyword args instead e.g., package.serialize(output, confirm_valid: false)" - [options, nil] + parse_serialize_options((secondary_options || {}).merge(confirm_valid: options), nil) end end end diff --git a/test/tc_package.rb b/test/tc_package.rb index fc48a9eb..b151d06f 100644 --- a/test/tc_package.rb +++ b/test/tc_package.rb @@ -180,6 +180,18 @@ class TestPackage < Test::Unit::TestCase end assert_equal 1, warnings.size assert_includes warnings.first, "confirm_valid as a boolean is deprecated" + File.delete(@fname) + end + + def test_serialization_with_deprecated_three_arguments + warnings = capture_warnings do + @package.serialize(@fname, true, zip_command: "zip") + end + assert_zip_file_matches_package(@fname, @package) + assert_created_with_zip_command(@fname, @package) + assert_equal 2, warnings.size + assert_includes warnings.first, "with 3 arguments is deprecated" + File.delete(@fname) end def capture_warnings(&block) -- cgit v1.2.3