diff --git a/README.md b/README.md index ceb54f63..2df9f42a 100644 --- a/README.md +++ b/README.md @@ -156,6 +156,18 @@ sheet.to_xml sheet.to_yaml ``` +Specify the file as default argument for `#to_csv`: + +```ruby +sheet.to_csv(File.new("/dev/null")) +``` + +specify the custom separator: + +```ruby +sheet.to_csv(separator: ":") # "," using 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..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, separator = ",", sheet = default_sheet) + 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 76cefcc4..b813eb54 100644 --- a/spec/lib/roo/base_spec.rb +++ b/spec/lib/roo/base_spec.rb @@ -269,7 +269,21 @@ 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 + + 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 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