Class: Qif::Reader

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/qif/reader.rb

Overview

The Qif::Reader class reads a qif file and provides access to the transactions as Qif::Transaction objects.

Usage:

reader = Qif::Reader.new(open('/path/to/qif'), 'dd/mm/yyyy')
reader.each do |transaction|
puts transaction.date.strftime('%d/%m/%Y')
puts transaction.amount.to_s
end

Instance Method Summary collapse

Constructor Details

#initialize(data, format = 'dd/mm/yyyy') ⇒ Reader

Create a new Qif::Reader object. The data argument must be either an IO object or a String containing the Qif file data.

The format argument specifies the date format in the file. This defaults to 'dd/mm/yyyy', but also accepts 'mm/dd/yyyy'.



24
25
26
27
28
29
# File 'lib/qif/reader.rb', line 24

def initialize(data, format = 'dd/mm/yyyy')
  @format = DateFormat.new(format)
  @data = data.respond_to?(:read) ? data : StringIO.new(data.to_s)
  read_header
  reset
end

Instance Method Details

#each(&block) ⇒ Object

Call a block with each Qif::Transaction from the Qif file. This method yields each transaction as it reads the file so it is better to use this than #transactions for large qif files.

reader.each do |transaction|
puts transaction.amount
end


46
47
48
49
50
51
52
# File 'lib/qif/reader.rb', line 46

def each(&block)    
  reset

  while transaction = next_transaction
    yield transaction
  end
end

#sizeObject Also known as: length

Return the number of transactions in the qif file.



55
56
57
58
# File 'lib/qif/reader.rb', line 55

def size
  read_all_transactions
  transaction_cache.size
end

#transactionsObject

Return an array of Qif::Transaction objects from the Qif file. This method reads the whole file before returning, so it may not be suitable for very large qif files.



34
35
36
37
# File 'lib/qif/reader.rb', line 34

def transactions
  read_all_transactions
  transaction_cache
end