Module: ACH::File::Builder

Included in:
ACH::File
Defined in:
lib/ach/file/builder.rb

Overview

This module hosts all the methods required for building string representation of an ACH file, and writing it to an actual file in the filesystem. Included by the ACH::File.

Instance Method Summary collapse

Instance Method Details

#batch_countObject

Returns amount of batches in file



6
7
8
# File 'lib/ach/file/builder.rb', line 6

def batch_count
  batches.length
end

#block_countObject

Returns amount of blocks, used in count. This amount is based on blocking factor, which is usually equals to 10, and on overall amount of records in a file. Return value represents the least amount of blocks taken by records in file.



13
14
15
# File 'lib/ach/file/builder.rb', line 13

def block_count
  (record_count.to_f / Constants::BLOCKING_FACTOR).ceil
end

#entry_hashObject

Returns sum of entry_hash values of all batches within self



23
24
25
# File 'lib/ach/file/builder.rb', line 23

def entry_hash
  batch_sum_of(:entry_hash)
end

#file_entry_addenda_countObject

Returns total amount of entry and addenda records of all batches within file.



18
19
20
# File 'lib/ach/file/builder.rb', line 18

def file_entry_addenda_count
  batches.map{ |batch| batch.entry_addenda_count }.inject(&:+) || 0
end

#record_countObject

Returns total amount of records hosted by a file.



44
45
46
# File 'lib/ach/file/builder.rb', line 44

def record_count
  2 + batch_count * 2 + file_entry_addenda_count
end

#tailObject

Returns array of ACH::Record::Tail records, based on tails_count



71
72
73
# File 'lib/ach/file/builder.rb', line 71

def tail
  [ Record::Tail.new ] * tails_count
end

#tails_countObject

Returns amount of ACH::Record::Tail records, required to append to string representation of a file to match proper amount of blocks.



77
78
79
# File 'lib/ach/file/builder.rb', line 77

def tails_count
  block_count * Constants::BLOCKING_FACTOR - record_count
end

#to_achObject

Returns well-fetched array of all ACH records in the file, appending proper amount, based on number of blocks, of tail records to it.



64
65
66
67
68
# File 'lib/ach/file/builder.rb', line 64

def to_ach
  head = [ header ]
  head.unshift(transmission_header) if have_transmission_header?
  head + batches.map(&:to_ach).flatten + [control] + tail
end

#to_s!Object

Returns complete string representation of a ACH file by converting each interval record to a string and joining the result by Constants::ROWS_DELIMITER



39
40
41
# File 'lib/ach/file/builder.rb', line 39

def to_s!
  to_ach.map(&:to_s!).join(Constants::ROWS_DELIMITER)
end

#total_credit_amountObject

Returns sum of total_credit_amount values of all batches within self



33
34
35
# File 'lib/ach/file/builder.rb', line 33

def total_credit_amount
  batch_sum_of(:total_credit_amount)
end

#total_debit_amountObject

Returns sum of total_debit_amount values of all batches within self



28
29
30
# File 'lib/ach/file/builder.rb', line 28

def total_debit_amount
  batch_sum_of(:total_debit_amount)
end

#write(filename) ⇒ Object

Writes string representation of self to passed filename



49
50
51
52
53
54
# File 'lib/ach/file/builder.rb', line 49

def write(filename)
  return false unless valid?
  ::File.open(filename, 'w') do |fh|
    fh.write(to_s!)
  end
end