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_countFixnum

Return amount of batches in file.

Returns:

  • (Fixnum)


8
9
10
# File 'lib/ach/file/builder.rb', line 8

def batch_count
  batches.length
end

#block_countFixnum

Return 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.

Returns:

  • (Fixnum)


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

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

#entry_hashFixnum

Return sum of entry_hash values of all batches within self.

Returns:

  • (Fixnum)


31
32
33
# File 'lib/ach/file/builder.rb', line 31

def entry_hash
  batch_sum_of(:entry_hash)
end

#file_entry_addenda_countFixnum

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

Returns:

  • (Fixnum)


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

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

#record_countFixnum

Return total amount of records hosted by a file.

Returns:

  • (Fixnum)


61
62
63
# File 'lib/ach/file/builder.rb', line 61

def record_count
  2 + batch_count * 2 + file_entry_addenda_count
end

#tailArray<ACH::Record::Tail>

Return array of ACH::Record::Tail records, based on tails_count.

Returns:



98
99
100
# File 'lib/ach/file/builder.rb', line 98

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

#tails_countFixnum

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

Returns:

  • (Fixnum)


106
107
108
# File 'lib/ach/file/builder.rb', line 106

def tails_count
  block_count * Constants::BLOCKING_FACTOR - record_count
end

#to_achArray<ACH::Record::Base>

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

Returns:



89
90
91
92
93
# File 'lib/ach/file/builder.rb', line 89

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

#to_s!String

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

Returns:

  • (String)


54
55
56
# File 'lib/ach/file/builder.rb', line 54

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

#total_credit_amountFixnum

Return sum of total_credit_amount values of all batches within self.

Returns:

  • (Fixnum)


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

def total_credit_amount
  batch_sum_of(:total_credit_amount)
end

#total_debit_amountFixnum

Return sum of total_debit_amount values of all batches within self.

Returns:

  • (Fixnum)


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

def total_debit_amount
  batch_sum_of(:total_debit_amount)
end

#write(filename) ⇒ ::File

Write string representation of self to passed filename.

Parameters:

  • filename (String)

Returns:

  • (::File)


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

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