diff --git a/CHANGELOG.md b/CHANGELOG.md index 10789e2..7f9842a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.8.0 + +- Add `csv_options` as an option to pass to `CSV.generate_line` (#18) + ## 0.7.0 - Drop support for ruby 2.7 diff --git a/README.md b/README.md index 0220db7..45bf54d 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ csv.items = @reports # csv.filename = "reports_#{Time.current.to_i}.csv" # csv.streaming = false +# csv.csv_options = { col_sep: "\t" } csv.cols.add('Update date') { |r| l(r.updated_at.to_date) } csv.cols.add('Categories') { |r| r.categories.pluck(:name).join(' ') } @@ -130,6 +131,7 @@ In `config/initializers/csb.rb`, you can configure the following values. Csb.configure do |config| config.utf8_bom = true # default: false config.streaming = false # default: true + config.csv_options = { col_sep: "\t" } # default: {} config.after_streaming_error = ->(error) do # default: nil Rails.logger.error(error) Bugsnag.notify(error) diff --git a/lib/csb/configuration.rb b/lib/csb/configuration.rb index 08057d1..90e1a48 100644 --- a/lib/csb/configuration.rb +++ b/lib/csb/configuration.rb @@ -1,10 +1,11 @@ module Csb class Configuration - attr_accessor :utf8_bom, :streaming, :after_streaming_error, :ignore_class_names + attr_accessor :utf8_bom, :streaming, :after_streaming_error, :ignore_class_names, :csv_options def initialize @utf8_bom = false @streaming = true + @csv_options = {} @ignore_class_names = %w[Puma::ConnectionError] end end diff --git a/lib/csb/handler.rb b/lib/csb/handler.rb index f9f38b3..5a11c4a 100644 --- a/lib/csb/handler.rb +++ b/lib/csb/handler.rb @@ -14,6 +14,7 @@ def self.call(template, source = nil) csv = ::Csb::Template.new( utf8_bom: ::Csb.configuration.utf8_bom, streaming: ::Csb.configuration.streaming, + csv_options: ::Csb.configuration.csv_options, ) #{source} controller.send(:send_file_headers!, type: 'text/csv', filename: csv.filename) diff --git a/lib/csb/template.rb b/lib/csb/template.rb index 916082e..5f09b23 100644 --- a/lib/csb/template.rb +++ b/lib/csb/template.rb @@ -2,12 +2,12 @@ module Csb class Template attr_accessor :utf8_bom, :filename, :streaming, :items, :cols, :csv_options - def initialize(utf8_bom:, streaming:) + def initialize(utf8_bom:, streaming:, csv_options:) @utf8_bom = utf8_bom @streaming = streaming + @csv_options = csv_options @cols = Cols.new @items = [] - @csv_options = {} end def build diff --git a/lib/csb/version.rb b/lib/csb/version.rb index c7d7adf..7d67997 100644 --- a/lib/csb/version.rb +++ b/lib/csb/version.rb @@ -1,3 +1,3 @@ module Csb - VERSION = '0.7.0' + VERSION = '0.8.0' end diff --git a/spec/lib/csb/template_spec.rb b/spec/lib/csb/template_spec.rb index 9660889..3e6c198 100644 --- a/spec/lib/csb/template_spec.rb +++ b/spec/lib/csb/template_spec.rb @@ -16,7 +16,7 @@ context 'Streaming' do subject(:enum) { template.build } - let(:template) { Csb::Template.new(streaming: true, utf8_bom: false) } + let(:template) { Csb::Template.new(streaming: true, utf8_bom: false, csv_options: {}) } it 'Is a Enumerator' do expect(enum).to be_a Enumerator @@ -29,7 +29,7 @@ context 'Not streaming' do subject { template.build } - let(:template) { Csb::Template.new(streaming: false, utf8_bom: false) } + let(:template) { Csb::Template.new(streaming: false, utf8_bom: false, csv_options: {}) } it { is_expected.to eq "Name,Email,Dummy\ntester1,dummy1@dummy.test,\ntester2,dummy2@dummy.test,\n" } end @@ -37,11 +37,7 @@ context 'with csv_options' do subject { template.build } - let(:template) { Csb::Template.new(streaming: false, utf8_bom: false) } - - before do - template.csv_options = { row_sep: "\r\n" } - end + let(:template) { Csb::Template.new(streaming: false, utf8_bom: false, csv_options: { row_sep: "\r\n" }) } it { is_expected.to eq "Name,Email,Dummy\r\ntester1,dummy1@dummy.test,\r\ntester2,dummy2@dummy.test,\r\n" } end