Skip to content

Commit

Permalink
Merge pull request #19 from aki77/0.8.0
Browse files Browse the repository at this point in the history
0.8.0
  • Loading branch information
aki77 authored Apr 12, 2024
2 parents acd298d + 22456bd commit 16d5f92
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(' ') }
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion lib/csb/configuration.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/csb/handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions lib/csb/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/csb/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Csb
VERSION = '0.7.0'
VERSION = '0.8.0'
end
10 changes: 3 additions & 7 deletions spec/lib/csb/template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,19 +29,15 @@
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,[email protected],\ntester2,[email protected],\n" }
end

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,[email protected],\r\ntester2,[email protected],\r\n" }
end
Expand Down

0 comments on commit 16d5f92

Please sign in to comment.