Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #389 #to_csv: changed predefined arguments and add documentation #469

Merged
merged 5 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Mifrill marked this conversation as resolved.
Show resolved Hide resolved
Mifrill marked this conversation as resolved.
Show resolved Hide resolved
### 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 @@ -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
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