class Guevara::Batch

Attributes

attributes[R]
transactions[R]

Public Class Methods

new(attributes) click to toggle source
# File lib/guevara/batch.rb, line 12
def initialize(attributes)
  @transactions = attributes.delete(:transactions)
  @attributes   = attributes
end

Public Instance Methods

addenda(transaction, index) click to toggle source
# File lib/guevara/batch.rb, line 64
def addenda transaction, index
  Addenda.new transaction.merge(entry_number: index + 1)
end
batch_control() click to toggle source
# File lib/guevara/batch.rb, line 33
def batch_control
  control_attributes = {
    entry_count:    transactions.size * 2,
    entry_hash:     entry_hash,
    total_debit:    total('debit'),
    total_credit:   total('credit')
  }.merge(attributes)
  BatchControl.new control_attributes
end
batch_header() click to toggle source
# File lib/guevara/batch.rb, line 29
def batch_header
  BatchHeader.new attributes
end
entry(transaction, index) click to toggle source
# File lib/guevara/batch.rb, line 58
def entry transaction, index
  entry_attributes = transaction.merge(number: index + 1,
                                       origin_id: attributes[:origin_id])
  Entry.new entry_attributes
end
entry_hash() click to toggle source
# File lib/guevara/batch.rb, line 43
def entry_hash
  transactions.
    map{ |t| t[:routing_number].to_i / 10 }. # ignore the check digit
    reduce(:+).                              # sum
    modulo(10_000_000_000)                   # cap to 10 digits
end
to_s() click to toggle source
# File lib/guevara/batch.rb, line 17
def to_s
  batch = []

  batch << batch_header.to_s
  transactions.each_with_index do |transaction, index|
    batch << entry(transaction, index).to_s
    batch << addenda(transaction, index).to_s
  end
  batch << batch_control
  batch.join
end
total(type) click to toggle source
# File lib/guevara/batch.rb, line 50
def total type
  transactions.
    select{ |t| t[:type] == type }.
    map{ |t| t[:amount] }.
    reduce(0, :+).
    modulo(1_000_000_000_000)
end