Skip to content

Commit 3d24c11

Browse files
authored
Merge pull request #469 from Mifrill/issue_389
fixes #389 #to_csv: changed predefined arguments and add documentation
2 parents 9e73e5c + 9a40dad commit 3d24c11

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,18 @@ sheet.to_xml
171171
sheet.to_yaml
172172
```
173173

174+
Specify the file as default argument for `#to_csv`:
175+
176+
```ruby
177+
sheet.to_csv(File.new("/dev/null"))
178+
```
179+
180+
specify the custom separator:
181+
182+
```ruby
183+
sheet.to_csv(separator: ":") # "," using by default
184+
```
185+
174186
### Excel (xlsx and xlsm) Support
175187

176188
Stream rows from an Excelx spreadsheet.

lib/roo/formatters/csv.rb

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
module Roo
22
module Formatters
33
module CSV
4-
def to_csv(filename = nil, separator = ",", sheet = default_sheet)
4+
def to_csv(filename = nil, old_separator = nil, old_sheet = nil, separator: ",", sheet: default_sheet)
5+
if old_separator
6+
warn("[DEPRECATION] optional argument for separator is deprecated. Please use keyword argument :separator instead")
7+
separator = old_separator
8+
end
9+
if old_sheet
10+
warn("[DEPRECATION] optional argument for sheet is deprecated. Please use keyword argument :sheet instead")
11+
sheet = old_sheet
12+
end
513
if filename
614
File.open(filename, "w") do |file|
715
write_csv_content(file, sheet, separator)

spec/lib/roo/base_spec.rb

+15-1
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,21 @@ def sheets
289289
end
290290

291291
it 'should convert the spreadsheet to csv using the separator when is passed on the parameter' do
292-
expect(spreadsheet.to_csv(nil, ';')).to eq(expected_csv_with_semicolons)
292+
expect(spreadsheet.to_csv(separator: ';')).to eq(expected_csv_with_semicolons)
293+
end
294+
295+
context 'should contains the deprecation warning message' do
296+
it 'convert the spreadsheet to csv using the separator' do
297+
converting =-> { spreadsheet.to_csv(nil, ';') }
298+
expect(converting.call).to eq(expected_csv_with_semicolons)
299+
expect(&converting).to output(/DEPRECATION.*:separator\b/).to_stderr
300+
end
301+
302+
it 'be able to arguments: filename, separator, sheet' do
303+
converting =-> { spreadsheet.to_csv(nil, ';', spreadsheet.default_sheet) }
304+
expect(converting.call).to eq(expected_csv_with_semicolons)
305+
expect(&converting).to output(/DEPRECATION.*:sheet\b/).to_stderr
306+
end
293307
end
294308
end
295309
end

spec/spec_helper.rb

+11
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,15 @@
66
c.include Helpers
77
c.color = true
88
c.formatter = :documentation if ENV["USE_REPORTERS"]
9+
original_stderr = $stderr
10+
original_stdout = $stdout
11+
c.before(:all) do
12+
# Redirect stderr and stdout
13+
$stderr = File.open(File::NULL, "w")
14+
$stdout = File.open(File::NULL, "w")
15+
end
16+
c.after(:all) do
17+
$stderr = original_stderr
18+
$stdout = original_stdout
19+
end
920
end

0 commit comments

Comments
 (0)