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
-
#batch_count ⇒ Fixnum
Return amount of
batches
in file. -
#block_count ⇒ Fixnum
Return amount of blocks, used in count.
-
#entry_hash ⇒ Fixnum
Return sum of
entry_hash
values of all batches within self. -
#file_entry_addenda_count ⇒ Fixnum
Return total amount of
entry
andaddenda
records of all batches within file. -
#record_count ⇒ Fixnum
Return total amount of records hosted by a file.
-
#tail ⇒ Array<ACH::Record::Tail>
Return array of ACH::Record::Tail records, based on
tails_count
. -
#tails_count ⇒ Fixnum
Return amount of ACH::Record::Tail records, required to append to string representation of a file to match proper amount of blocks.
-
#to_ach ⇒ Array<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.
-
#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
. -
#total_credit_amount ⇒ Fixnum
Return sum of
total_credit_amount
values of all batches withinself
. -
#total_debit_amount ⇒ Fixnum
Return sum of
total_debit_amount
values of all batches withinself
. -
#write(filename) ⇒ ::File
Write string representation of self to passed
filename
.
Instance Method Details
#batch_count ⇒ Fixnum
Return amount of batches
in file.
8 9 10 |
# File 'lib/ach/file/builder.rb', line 8 def batch_count batches.length end |
#block_count ⇒ Fixnum
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.
17 18 19 |
# File 'lib/ach/file/builder.rb', line 17 def block_count (record_count.to_f / Constants::BLOCKING_FACTOR).ceil end |
#entry_hash ⇒ Fixnum
Return sum of entry_hash
values of all batches within self.
31 32 33 |
# File 'lib/ach/file/builder.rb', line 31 def entry_hash batch_sum_of(:entry_hash) end |
#file_entry_addenda_count ⇒ Fixnum
Return total amount of entry
and addenda
records of all batches within file.
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_count ⇒ Fixnum
Return total amount of records hosted by a file.
61 62 63 |
# File 'lib/ach/file/builder.rb', line 61 def record_count 2 + batch_count * 2 + file_entry_addenda_count end |
#tail ⇒ Array<ACH::Record::Tail>
Return array of ACH::Record::Tail records, based on tails_count
.
98 99 100 |
# File 'lib/ach/file/builder.rb', line 98 def tail [Record::Tail.new] * tails_count end |
#tails_count ⇒ Fixnum
Return amount of ACH::Record::Tail records, required to append to string representation of a file to match proper amount of blocks.
106 107 108 |
# File 'lib/ach/file/builder.rb', line 106 def tails_count block_count * Constants::BLOCKING_FACTOR - record_count end |
#to_ach ⇒ Array<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.
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
.
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_amount ⇒ Fixnum
Return sum of total_credit_amount
values of all batches within self
.
45 46 47 |
# File 'lib/ach/file/builder.rb', line 45 def total_credit_amount batch_sum_of(:total_credit_amount) end |
#total_debit_amount ⇒ Fixnum
Return sum of total_debit_amount
values of all batches within self
.
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
.
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 |