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.



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

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.



4
5
6
# File 'lib/payment_dta/dta_file.rb', line 4

def records
  @records
end

Class Method Details

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

Yields:

  • (dta_file)


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

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

Instance Method Details

#<<(record) ⇒ Object



25
26
27
28
29
# File 'lib/payment_dta/dta_file.rb', line 25

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

#totalObject



19
20
21
22
23
# File 'lib/payment_dta/dta_file.rb', line 19

def total
  @records.inject(0) do |sum, record|
    sum + record.amount.to_f
  end
end

#write_fileObject



12
13
14
15
16
17
# File 'lib/payment_dta/dta_file.rb', line 12

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