module ACH::File::Builder

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.

Public Instance Methods

batch_count() click to toggle source

Return amount of batches in file.

@return [Fixnum]

# File lib/ach/file/builder.rb, line 8
def batch_count
  batches.length
end
block_count() click to toggle source

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.

@return [Fixnum]

# File lib/ach/file/builder.rb, line 17
def block_count
  (record_count.to_f / Constants::BLOCKING_FACTOR).ceil
end
entry_hash() click to toggle source

Return sum of entry_hash values of all batches within self.

@return [Fixnum]

# File lib/ach/file/builder.rb, line 31
def entry_hash
  batch_sum_of(:entry_hash)
end
file_entry_addenda_count() click to toggle source

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

@return [Fixnum]

# File lib/ach/file/builder.rb, line 24
def file_entry_addenda_count
  batches.map{ |batch| batch.entry_addenda_count }.inject(&:+) || 0
end
record_count() click to toggle source

Return total amount of records hosted by a file.

@return [Fixnum]

# File lib/ach/file/builder.rb, line 61
def record_count
  2 + batch_count * 2 + file_entry_addenda_count
end
tail() click to toggle source

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

@return [Array<ACH::Record::Tail>]

# File lib/ach/file/builder.rb, line 98
def tail
  [Record::Tail.new] * tails_count
end
tails_count() click to toggle source

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

@return [Fixnum]

# File lib/ach/file/builder.rb, line 106
def tails_count
  block_count * Constants::BLOCKING_FACTOR - record_count
end
to_ach() click to toggle source

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

@return [Array<ACH::Record::Base>]

# 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!() click to toggle source

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.

@return [String]

# File lib/ach/file/builder.rb, line 54
def to_s!
  to_ach.map(&:to_s!).join(Constants::ROWS_DELIMITER)
end
total_credit_amount() click to toggle source

Return sum of total_credit_amount values of all batches within self.

@return [Fixnum]

# File lib/ach/file/builder.rb, line 45
def total_credit_amount
  batch_sum_of(:total_credit_amount)
end
total_debit_amount() click to toggle source

Return sum of total_debit_amount values of all batches within self.

@return [Fixnum]

# File lib/ach/file/builder.rb, line 38
def total_debit_amount
  batch_sum_of(:total_debit_amount)
end
write(filename) click to toggle source

Write string representation of self to passed filename.

@param [String] filename @return [::File]

# 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

Private Instance Methods

batch_sum_of(meth) click to toggle source

Helper method for calculating different properties of batches within file.

@param [String] meth @return [Fixnum]

# File lib/ach/file/builder.rb, line 80
def batch_sum_of(meth)
  batches.map(&meth).compact.inject(&:+)
end