Skip to content

Commit

Permalink
Merge pull request #469 from Mifrill/issue_389
Browse files Browse the repository at this point in the history
fixes #389 #to_csv: changed predefined arguments and add documentation
  • Loading branch information
simonoff committed Mar 14, 2024
2 parents 9e73e5c + 9a40dad commit 3d24c11
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,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.
Expand Down
10 changes: 9 additions & 1 deletion lib/roo/formatters/csv.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
16 changes: 15 additions & 1 deletion spec/lib/roo/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,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
11 changes: 11 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 3d24c11

Please sign in to comment.