Class: DTAFile

Inherits:
Object
  • Object
show all
Defined in:
lib/payment_dta/dta_file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, transaction_number = rand(100000000000).to_s) ⇒ DTAFile

Returns a new instance of DTAFile.



8
9
10
11
12
# File 'lib/payment_dta/dta_file.rb', line 8

def initialize(path, transaction_number = rand(100000000000).to_s)
  @transaction_number = transaction_number.to_s
  @path = path
  @records = SortedSet.new
end

Instance Attribute Details

#recordsObject (readonly)

Returns the value of attribute records.



6
7
8
# File 'lib/payment_dta/dta_file.rb', line 6

def records
  @records
end

Class Method Details

.create(path) {|dta_file| ... } ⇒ Object

Yields:

  • (dta_file)


37
38
39
40
41
42
# File 'lib/payment_dta/dta_file.rb', line 37

def self.create(path)
  dta_file = self.new(path)
  yield dta_file
  dta_file.write_file
  dta_file
end

Instance Method Details

#<<(record) ⇒ Object



27
28
29
30
31
# File 'lib/payment_dta/dta_file.rb', line 27

def <<(record)
  record.transaction_number = @transaction_number
  @records << record
  recalculate_entry_sequence_numbers
end

#dta_stringObject



33
34
35
# File 'lib/payment_dta/dta_file.rb', line 33

def dta_string
  (@records.map(&:to_dta) << build_total_record.to_dta) * "\n" << "\n"
end

#totalObject



21
22
23
24
25
# File 'lib/payment_dta/dta_file.rb', line 21

def total
  @records.inject(0) do |sum, record|
    sum + BigDecimal.new(record.amount.to_s, 16)
  end
end

#write_fileObject



14
15
16
17
18
19
# File 'lib/payment_dta/dta_file.rb', line 14

def write_file
  File.open(@path,"w") do |file|
    @records.each{|record| file.puts record.to_dta}
    file.puts build_total_record.to_dta
  end
end