Class: Qif::Reader
Overview
Defined Under Namespace
Classes: UnknownAccountType
Constant Summary collapse
- SUPPORTED_ACCOUNTS =
{ "!Type:Bank" => "Bank account transactions", "!Type:Cash" => "Cash account transactions", "!Type:CCard" => "Credit card account transactions", "!Type:Oth A" => "Asset account transactions", "!Type:Oth L" => "Liability account transactions" }
Instance Method Summary collapse
-
#each(&block) ⇒ Object
Call a block with each Qif::Transaction from the Qif file.
-
#guess_date_format ⇒ Object
Guess the file format of dates, reading the beginning of file, or return nil if no dates are found (?!).
-
#initialize(data, format = nil) ⇒ Reader
constructor
Create a new Qif::Reader object.
-
#size ⇒ Object
(also: #length)
Return the number of transactions in the qif file.
-
#transactions ⇒ Object
Return an array of Qif::Transaction objects from the Qif file.
Constructor Details
#initialize(data, format = nil) ⇒ 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 optional format argument specifies the date format in the file. Giving a format will force it, otherwise the format will guissed reading the transactions in the file, this defaults to ‘dd/mm/yyyy’ if guessing method fails.
34 35 36 37 38 39 40 |
# File 'lib/qif/reader.rb', line 34 def initialize(data, format = nil) @data = data.respond_to?(:read) ? data : StringIO.new(data.to_s) @format = DateFormat.new(format || guess_date_format || 'dd/mm/yyyy') read_header raise(UnknownAccountType, "Unknown account type. Should be one of followings :\n#{SUPPORTED_ACCOUNTS.keys.inspect}") unless SUPPORTED_ACCOUNTS.keys.collect(&:downcase).include? @header.downcase 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
57 58 59 60 61 62 63 |
# File 'lib/qif/reader.rb', line 57 def each(&block) reset while transaction = next_transaction yield transaction end end |
#guess_date_format ⇒ Object
Guess the file format of dates, reading the beginning of file, or return nil if no dates are found (?!).
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/qif/reader.rb', line 73 def guess_date_format begin line = @data.gets break if line.nil? date = line.strip.scan(/^D(\d{1,2}).(\d{1,2}).(\d{2,4})/).flatten if date.count == 3 guessed_format = date[0].to_i.between?(1, 12) ? (date[1].to_i.between?(1, 12) ? nil : 'mm/dd') : 'dd/mm' guessed_format += '/' + 'y'*date[2].length if guessed_format end end until guessed_format @data.rewind guessed_format end |
#size ⇒ Object Also known as: length
Return the number of transactions in the qif file.
66 67 68 69 |
# File 'lib/qif/reader.rb', line 66 def size read_all_transactions transaction_cache.size end |
#transactions ⇒ Object
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.
45 46 47 48 |
# File 'lib/qif/reader.rb', line 45 def transactions read_all_transactions transaction_cache end |