Skip to content

Commit

Permalink
Add optional eol argument to AchFile#to_s
Browse files Browse the repository at this point in the history
Closes #49
  • Loading branch information
jm81 committed Nov 12, 2017
1 parent 34042e8 commit 1d2ea0d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/ach/ach_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ def initialize data=nil
end
end

def to_s
# @param eol [String] Line ending, default to CRLF
def to_s eol = "\r\n"
records = []
records << @header

Expand Down Expand Up @@ -51,7 +52,7 @@ def to_s
@control.entry_hash += batch.control.entry_hash
end

records.collect { |r| r.to_ach }.join("\r\n") + "\r\n"
records.collect { |r| r.to_ach }.join(eol) + eol
end

def report
Expand Down
21 changes: 21 additions & 0 deletions spec/ach/ach_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,26 @@ def add_detail batch
end
end
end

describe 'eol param' do
context 'default' do
subject(:output) { ach_file.to_s }

it 'uses CRLF' do
expect(output.split("\r\n").length).to eq(10)
expect(output[-2..-1]).to eq("\r\n")
end
end

context 'param given' do
subject(:output) { ach_file.to_s("\n") }
it 'uses the param' do
expect(output.split("\r\n").length).to eq(1)
expect(output.split("\n").length).to eq(10)
expect(output[-2..-1]).to_not eq("\r\n")
expect(output[-1]).to eq("\n")
end
end
end
end
end

0 comments on commit 1d2ea0d

Please sign in to comment.