Class: ACH::File

Inherits:
Component show all
Defined in:
lib/ach/file.rb

Defined Under Namespace

Classes: Control, Header

Constant Summary

Constants included from Constants

Constants::BLOCKING_FACTOR, Constants::FORMAT_CODE, Constants::RECORD_SIZE

Instance Attribute Summary

Attributes inherited from Component

#attributes

Instance Method Summary collapse

Methods inherited from Component

#control, #fields_for, has_many, #header, #initialize, #method_missing

Methods included from Validations

#errors, #valid?

Constructor Details

This class inherits a constructor from ACH::Component

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ACH::Component

Instance Method Details

#batch_countObject



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

def batch_count
  batches.length
end

#block_countObject



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

def block_count
  (file_entry_count.to_f / BLOCKING_FACTOR).ceil
end

#entry_hashObject



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

def entry_hash
  batches.map(&:entry_hash).compact.inject(&:+)
end

#file_entry_countObject



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

def file_entry_count
  batches.map{ |b| b.entries.length }.inject(&:+) || 0
end

#record_countObject



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

def record_count
  2 + batches.length * 2 + file_entry_count
end

#to_achObject



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

def to_ach
  extra = block_count * BLOCKING_FACTOR - file_entry_count
  tail = ([Tail.new] * extra).unshift(control)
  [header] + batches.map(&:to_ach).flatten + tail
end

#to_s!Object



35
36
37
# File 'lib/ach/file.rb', line 35

def to_s!
  to_ach.map(&:to_s!).join("\r\n") + "\r\n"
end

#total_credit_amountObject



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

def total_credit_amount
  batches.map(&:total_credit_amount).compact.inject(&:+)
end

#total_debit_amountObject



21
22
23
# File 'lib/ach/file.rb', line 21

def total_debit_amount
  batches.map(&:total_debit_amount).compact.inject(&:+)
end

#write(filename) ⇒ Object



43
44
45
46
47
48
# File 'lib/ach/file.rb', line 43

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