Skip to content

Commit

Permalink
Increment batch numbers if not set manually (fixes #42)
Browse files Browse the repository at this point in the history
  • Loading branch information
jm81 committed Dec 18, 2016
1 parent cd07776 commit eb1865c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/ach/ach_file.rb
Expand Up @@ -25,7 +25,11 @@ def initialize data=nil
def to_s
records = []
records << @header
@batches.each { |b| records += b.to_ach }

@batches.each_with_index do |batch, index|
batch.header.batch_number ||= index + 1
records += batch.to_ach
end
records << @control

nines_needed = 10 - (records.length % 10)
Expand Down
2 changes: 1 addition & 1 deletion lib/ach/records/batch_header.rb
Expand Up @@ -33,6 +33,6 @@ class BatchHeader < Record
field :originating_dfi_identification, String,
nil, nil, /\A\d{8}\z/

field :batch_number, Integer, lambda { |f| sprintf('%07d', f)}, 1
field :batch_number, Integer, lambda { |f| sprintf('%07d', f)}, nil
end
end
20 changes: 20 additions & 0 deletions spec/ach/ach_file_spec.rb
Expand Up @@ -47,6 +47,26 @@ def add_detail batch
end

describe '#to_s' do
describe 'incrementing batch numbers' do
before(:each) do
add_batch ach_file, 1
add_batch ach_file, 1
add_batch ach_file, 1
end

context 'batch numbers not set' do
it 'increments batch numbers' do
lines = ach_file.to_s.split("\r\n")
expect(lines[1][-1]).to eq('1')
expect(lines[3][-1]).to eq('1')
expect(lines[4][-1]).to eq('2')
expect(lines[6][-1]).to eq('2')
expect(lines[7][-1]).to eq('3')
expect(lines[9][-1]).to eq('3')
end
end
end

describe 'padding with 9s' do
let(:nines) { '9' * 94 }

Expand Down

0 comments on commit eb1865c

Please sign in to comment.