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

Allow stdlib CSV options to be passed through #4

Merged
merged 6 commits into from
Mar 20, 2015
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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,20 @@ bar1,bar1-description,,acb12345
bar2,bar2-description,,xyz98765
```

### stdlib CSV options
If you need to pass any [options](http://docs.ruby-lang.org/en/2.0.0/CSV.html#method-c-new)
to the underlying CSV library:


```
> puts (Foo.all + Bar.all).to_csv csv_options: {col_sep:'|'}
name|description|code|barcode
foo1|foo1-description|111|
foo2|foo2-description|222|
bar1|bar1-description||acb12345
bar2|bar2-description||xyz98765
```

## Contributing

1. Fork it
Expand Down
1 change: 1 addition & 0 deletions as_csv.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Gem::Specification.new do |gem|

gem.add_runtime_dependency "activemodel", '>= 3.0'
gem.add_runtime_dependency "actionpack", '>= 3.0'
gem.add_runtime_dependency "responders"

# Tests
gem.add_development_dependency "rails", ">= 3.0"
Expand Down
5 changes: 3 additions & 2 deletions lib/as_csv/csv_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

module AsCSV
class CSVBuilder
attr_reader :records, :options
attr_reader :records, :options, :csv_options

def initialize(records, options={})
@records = Array(records)
@options = options
@csv_options = options.delete(:csv_options) || {}
validate
end

def to_csv
rows.collect { |row| CSV.generate_line row }.join
rows.collect { |row| CSV.generate_line row, csv_options }.join
end

private
Expand Down
1 change: 1 addition & 0 deletions spec/dummy/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

Bundler.require
require "as_csv"
require "responders"

module Dummy
class Application < Rails::Application
Expand Down
16 changes: 15 additions & 1 deletion spec/models/widget_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@
end
end

describe 'dummy with csv options' do
subject(:dummy_widget) { Widget.new :name => "widget-name", :description => 'widget-description', :code => 1234 }

describe :to_csv do
subject { dummy_widget.to_csv(csv_options: {col_sep:'|'})}
it do
should == <<-CSV.strip_heredoc
id|name|description|code
|widget-name|widget-description|1234
CSV
end
end
end

describe 'collection' do
before do
Widget.create! [
Expand All @@ -44,4 +58,4 @@
end
end

end
end