Skip to content

Commit

Permalink
Merge pull request #22 from aki77/0.11.0
Browse files Browse the repository at this point in the history
0.11.0
  • Loading branch information
aki77 authored May 20, 2024
2 parents 9a5472b + be298b4 commit 435fa94
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.11.0

- Consider `Csb.configuration` as initial value for `Csb::Builder`

## 0.10.0

- Drop support for rails 6.1
Expand Down
6 changes: 3 additions & 3 deletions lib/csb/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ class Builder
attr_reader :output, :utf8_bom, :items, :cols, :csv_options
attr_accessor :items

def initialize(output = '', items: [], utf8_bom: false, csv_options: {})
def initialize(output = '', items: [], **kwargs)
@output = output
@utf8_bom = utf8_bom
@cols = Cols.new
@items = items
@csv_options = csv_options
@utf8_bom = kwargs.fetch(:utf8_bom) { Csb.configuration.utf8_bom }
@csv_options = kwargs.fetch(:csv_options) { Csb.configuration.csv_options }
end

def build
Expand Down
2 changes: 0 additions & 2 deletions lib/csb/handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ def self.call(template, source = nil)

<<~RUBY
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
16 changes: 11 additions & 5 deletions lib/csb/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ module Csb
class Template
attr_accessor :utf8_bom, :filename, :streaming, :items, :cols, :csv_options

def initialize(utf8_bom:, streaming:, csv_options:)
@utf8_bom = utf8_bom
def initialize(streaming:)
@streaming = streaming
@csv_options = csv_options
@cols = Cols.new
@items = []
end
Expand All @@ -20,16 +18,24 @@ def streaming?

private

def builder_options
{
items: items,
utf8_bom: utf8_bom,
csv_options: csv_options,
}.compact
end

def build_string
builder = Builder.new(utf8_bom: utf8_bom, items: items, csv_options: csv_options)
builder = Builder.new(**builder_options)
builder.cols.copy!(cols)
builder.build
end

def build_enumerator
Enumerator.new do |y|
begin
builder = Builder.new(y, utf8_bom: utf8_bom, items: items, csv_options: csv_options)
builder = Builder.new(y, **builder_options)
builder.cols.copy!(cols)
builder.build
rescue => error
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.10.0'
VERSION = '0.11.0'
end
10 changes: 7 additions & 3 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, csv_options: {}) }
let(:template) { Csb::Template.new(streaming: true) }

it 'Is a Enumerator' do
expect(enum).to be_a Enumerator
Expand All @@ -29,15 +29,19 @@
context 'Not streaming' do
subject { template.build }

let(:template) { Csb::Template.new(streaming: false, utf8_bom: false, csv_options: {}) }
let(:template) { Csb::Template.new(streaming: false) }

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, csv_options: { row_sep: "\r\n" }) }
let(:template) do
template = Csb::Template.new(streaming: false)
template.csv_options = { row_sep: "\r\n" }
template
end

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 435fa94

Please sign in to comment.