class PS2Format::BatchTransfer

Attributes

header[RW]
operations[RW]

Public Class Methods

new(opts = {}) click to toggle source
# File lib/PS2Format/ps2.rb, line 10
def initialize(opts = {})
  @header = Header.new(opts)
  @operations = []
  process_options(opts)
end
read(file_name) click to toggle source
# File lib/PS2Format/ps2.rb, line 131
def read(file_name)
  stream = File.new(file_name,'r')
  file = unmarshall stream
  stream.close
  file
end
unmarshall(stream) click to toggle source
# File lib/PS2Format/ps2.rb, line 138
def unmarshall(stream)
  lines = stream.readlines

  header = Header.new(lines[0])

  operations = lines[1..-2].reduce([]) { |array, line| array << Operation.new(line) }

  ps2 = PS2Format::BatchTransfer.new
  ps2.header = header
  ps2.operations = operations
  #NOTICE: Footer is built on the fly! This should not happen for unmarshalling

  ps2
end

Public Instance Methods

add_operation(options = {}) click to toggle source
# File lib/PS2Format/ps2.rb, line 41
def add_operation(options = {})
  oper = Operation.new(options)
  @operations << oper
  oper.validate
  oper.errors
end
add_operation!(options = {}) click to toggle source
# File lib/PS2Format/ps2.rb, line 36
def add_operation!(options = {})
  Operation.validate_options(options)
  @operations << Operation.new(options)
end
data() click to toggle source
# File lib/PS2Format/ps2.rb, line 87
def data
  to_a.map(&:to_s).join("\n")
end
errors() click to toggle source
# File lib/PS2Format/ps2.rb, line 91
def errors
  @header.errors + operation_errors + footer.errors
end
expense_amount() click to toggle source
# File lib/PS2Format/ps2.rb, line 58
def expense_amount
  @operations.map { |op|
    if op.operation_type.to_i < 50 then op.amount.to_i else 0 end
  }.reduce(0, &:+)
end
income_amount() click to toggle source
# File lib/PS2Format/ps2.rb, line 52
def income_amount
  @operations.map { |op|
    if op.operation_type.to_i >= 50 then op.amount.to_i else 0 end
  }.reduce(0, &:+)
end
invalid?() click to toggle source
# File lib/PS2Format/ps2.rb, line 73
def invalid?
  not(valid?)
end
marshall(stream) click to toggle source
# File lib/PS2Format/ps2.rb, line 122
def marshall(stream)
  stream << to_s
end
operation_errors() click to toggle source
# File lib/PS2Format/ps2.rb, line 99
def operation_errors
  @operations.map(&:errors).reject(&:empty?)
end
ordering_nib=(nib) click to toggle source
# File lib/PS2Format/ps2.rb, line 16
def ordering_nib=(nib)
  @header.ordering_nib = Record.pre_process_nib(nib)
end
processing_date() click to toggle source
# File lib/PS2Format/ps2.rb, line 28
def processing_date
  Record.str_to_date(@header.processing_date)
end
processing_date=(date) click to toggle source
# File lib/PS2Format/ps2.rb, line 32
def processing_date=(date)
  @header.processing_date = Record.date_to_str(date)
end
reference() click to toggle source
# File lib/PS2Format/ps2.rb, line 24
def reference
  @header.reference.lstrip
end
reference=(ref) click to toggle source
# File lib/PS2Format/ps2.rb, line 20
def reference=(ref)
  @header.reference = String(ref).rjust(Header::REFERENCE_FIELD_SIZE, ' ')
end
save(file_name) click to toggle source
# File lib/PS2Format/ps2.rb, line 111
def save(file_name)
  stream = File.new(file_name, "w")
  validate
  if errors.empty?
    marshall stream
    stream.close
  else
    errors
  end
end
structured_errors() click to toggle source
# File lib/PS2Format/ps2.rb, line 95
def structured_errors
  { header: @header.errors, operations: structured_operation_errors, footer: footer.errors }
end
structured_operation_errors() click to toggle source
# File lib/PS2Format/ps2.rb, line 103
def structured_operation_errors
  error_hash = {}
  @operations.each do |op|
    error_hash[op] = op.errors unless op.errors.empty?
  end
  error_hash
end
to_s() click to toggle source
# File lib/PS2Format/ps2.rb, line 126
def to_s
  data
end
total_amount() click to toggle source
# File lib/PS2Format/ps2.rb, line 48
def total_amount
  @operations.map(&:amount).reduce(0, &:+)
end
valid?() click to toggle source
# File lib/PS2Format/ps2.rb, line 68
def valid?
  validate
  errors.empty?
end
validate() click to toggle source
# File lib/PS2Format/ps2.rb, line 77
def validate
  oper_errors = @operations.each do |o|
    o.validate
  end

  @header.validate

  footer.validate
end

Private Instance Methods

process_options(opts) click to toggle source
# File lib/PS2Format/ps2.rb, line 155
def process_options(opts)
  nib = opts.delete(:ordering_nib)
  date = opts.delete(:processing_date)
  ref = opts.delete(:reference)

  self.ordering_nib = nib if nib
  self.processing_date = date if date
  self.reference = ref if ref
end
to_a() click to toggle source
# File lib/PS2Format/ps2.rb, line 165
def to_a
  [@header.data, @operations.map(&:data), footer.data].flatten
end