From 9ba678ee0b0a35ac72473140f5de850d0aee7f1b Mon Sep 17 00:00:00 2001 From: Aleksey Strizhak Date: Wed, 17 Oct 2018 16:41:35 +0500 Subject: [PATCH 1/5] fixes #389 #to_csv: changed predefined arguments and add documentation --- README.md | 13 +++++++++++++ lib/roo/formatters/csv.rb | 2 +- spec/lib/roo/base_spec.rb | 2 +- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ceb54f63..c22581c6 100644 --- a/README.md +++ b/README.md @@ -156,6 +156,19 @@ sheet.to_xml sheet.to_yaml ``` +You can specified file as default argument to `#to_csv`: + +```ruby +# puts csv format content into a specified file +sheet.to_csv(File.new("/dev/null")) +``` + +also you can specify the custom separator for the content: + +```ruby +sheet.to_csv(separator: ":") # "," is used by default +``` + ### Excel (xlsx and xlsm) Support Stream rows from an Excelx spreadsheet. diff --git a/lib/roo/formatters/csv.rb b/lib/roo/formatters/csv.rb index 3181f091..595a6095 100644 --- a/lib/roo/formatters/csv.rb +++ b/lib/roo/formatters/csv.rb @@ -1,7 +1,7 @@ module Roo module Formatters module CSV - def to_csv(filename = nil, separator = ",", sheet = default_sheet) + def to_csv(filename = nil, separator: ",", sheet: default_sheet) if filename File.open(filename, "w") do |file| write_csv_content(file, sheet, separator) diff --git a/spec/lib/roo/base_spec.rb b/spec/lib/roo/base_spec.rb index 76cefcc4..4cdbc01e 100644 --- a/spec/lib/roo/base_spec.rb +++ b/spec/lib/roo/base_spec.rb @@ -269,7 +269,7 @@ def sheets end it 'should convert the spreadsheet to csv using the separator when is passed on the parameter' do - expect(spreadsheet.to_csv(nil, ';')).to eq(expected_csv_with_semicolons) + expect(spreadsheet.to_csv(separator: ';')).to eq(expected_csv_with_semicolons) end end end From 3ffaead99b0c69dee4e735c122ac249e417d08e2 Mon Sep 17 00:00:00 2001 From: Aleksey Strizhak Date: Tue, 13 Nov 2018 16:35:13 +0500 Subject: [PATCH 2/5] revert bound of default_sheet argument --- lib/roo/formatters/csv.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/roo/formatters/csv.rb b/lib/roo/formatters/csv.rb index 595a6095..1c30c07a 100644 --- a/lib/roo/formatters/csv.rb +++ b/lib/roo/formatters/csv.rb @@ -1,7 +1,7 @@ module Roo module Formatters module CSV - def to_csv(filename = nil, separator: ",", sheet: default_sheet) + def to_csv(filename = nil, sheet = default_sheet, separator: ",") if filename File.open(filename, "w") do |file| write_csv_content(file, sheet, separator) From bae8a82925f047d188119490fa8da6e556129b36 Mon Sep 17 00:00:00 2001 From: Aleksey Strizhak Date: Wed, 14 Nov 2018 13:52:52 +0500 Subject: [PATCH 3/5] add 'deprecation' messages for #to_csv arguments --- lib/roo/formatters/csv.rb | 10 +++++++++- spec/lib/roo/base_spec.rb | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/roo/formatters/csv.rb b/lib/roo/formatters/csv.rb index 1c30c07a..67f63faf 100644 --- a/lib/roo/formatters/csv.rb +++ b/lib/roo/formatters/csv.rb @@ -1,7 +1,15 @@ module Roo module Formatters module CSV - def to_csv(filename = nil, sheet = default_sheet, separator: ",") + def to_csv(filename = nil, old_separator = nil, old_sheet = nil, separator: ",", sheet: default_sheet) + if old_separator + warn("[DEPRECATION] optional argument for separator is deprecated. Please use keyword argument :separator instead") + separator = old_separator + end + if old_sheet + warn("[DEPRECATION] optional argument for sheet is deprecated. Please use keyword argument :sheet instead") + sheet = old_sheet + end if filename File.open(filename, "w") do |file| write_csv_content(file, sheet, separator) diff --git a/spec/lib/roo/base_spec.rb b/spec/lib/roo/base_spec.rb index 4cdbc01e..b813eb54 100644 --- a/spec/lib/roo/base_spec.rb +++ b/spec/lib/roo/base_spec.rb @@ -271,5 +271,19 @@ def sheets it 'should convert the spreadsheet to csv using the separator when is passed on the parameter' do expect(spreadsheet.to_csv(separator: ';')).to eq(expected_csv_with_semicolons) end + + context 'should contains the deprecation warning message' do + it 'convert the spreadsheet to csv using the separator' do + converting =-> { spreadsheet.to_csv(nil, ';') } + expect(converting.call).to eq(expected_csv_with_semicolons) + expect(&converting).to output(/DEPRECATION.*:separator\b/).to_stderr + end + + it 'be able to arguments: filename, separator, sheet' do + converting =-> { spreadsheet.to_csv(nil, ';', spreadsheet.default_sheet) } + expect(converting.call).to eq(expected_csv_with_semicolons) + expect(&converting).to output(/DEPRECATION.*:sheet\b/).to_stderr + end + end end end From 0d1b91f0b7b361c505c69781e2ee418015329ebb Mon Sep 17 00:00:00 2001 From: Aleksey Strizhak Date: Wed, 14 Nov 2018 13:54:12 +0500 Subject: [PATCH 4/5] supress console output during rspec tests --- spec/spec_helper.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a0930085..f35cf55d 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -6,4 +6,15 @@ c.include Helpers c.color = true c.formatter = :documentation if ENV["USE_REPORTERS"] + original_stderr = $stderr + original_stdout = $stdout + c.before(:all) do + # Redirect stderr and stdout + $stderr = File.open(File::NULL, "w") + $stdout = File.open(File::NULL, "w") + end + c.after(:all) do + $stderr = original_stderr + $stdout = original_stdout + end end From 9a40dad50314d687612236eaaf3f70e86fd1cfb7 Mon Sep 17 00:00:00 2001 From: Aleksey Strizhak Date: Sun, 7 Apr 2019 10:27:44 +0500 Subject: [PATCH 5/5] #389 fix grammar --- README.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c22581c6..2df9f42a 100644 --- a/README.md +++ b/README.md @@ -156,17 +156,16 @@ sheet.to_xml sheet.to_yaml ``` -You can specified file as default argument to `#to_csv`: +Specify the file as default argument for `#to_csv`: ```ruby -# puts csv format content into a specified file sheet.to_csv(File.new("/dev/null")) ``` -also you can specify the custom separator for the content: +specify the custom separator: ```ruby -sheet.to_csv(separator: ":") # "," is used by default +sheet.to_csv(separator: ":") # "," using by default ``` ### Excel (xlsx and xlsm) Support