Skip to content

Commit

Permalink
Merge 81dbbeb into 8f65109
Browse files Browse the repository at this point in the history
  • Loading branch information
Mifrill committed Nov 14, 2018
2 parents 8f65109 + 81dbbeb commit 0f61eba
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
13 changes: 13 additions & 0 deletions README.md
Expand Up @@ -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.
Expand Down
10 changes: 9 additions & 1 deletion 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)
Expand Down
16 changes: 15 additions & 1 deletion spec/lib/roo/base_spec.rb
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
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 0f61eba

Please sign in to comment.