Class: Qif::Reader
Overview
Defined Under Namespace
Classes: UnknownAccountType, UnrecognizedData
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 Attribute Summary collapse
-
#index ⇒ Object
readonly
Returns the value of attribute index.
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 guessed reading the transactions in the file, this defaults to ‘dd/mm/yyyy’ if guessing method fails.
40 41 42 43 44 45 46 47 |
# File 'lib/qif/reader.rb', line 40 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(UnrecognizedData, "Provided data doesn't seems to represent a QIF file") unless @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 Attribute Details
#index ⇒ Object (readonly)
Returns the value of attribute index.
20 21 22 |
# File 'lib/qif/reader.rb', line 20 def index @index 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
64 65 66 67 68 69 70 |
# File 'lib/qif/reader.rb', line 64 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 (?!).
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/qif/reader.rb', line 80 def guess_date_format begin line = @data.gets break if line.nil? date = line[1..-1] guessed_format = Qif::DateFormat::SUPPORTED_DATEFORMAT.find { |format_string, format| test_date_with_format?(date, format_string, format) } end until guessed_format @data.rewind guessed_format ? guessed_format.first : @fallback_format end |
#size ⇒ Object Also known as: length
Return the number of transactions in the qif file.
73 74 75 76 |
# File 'lib/qif/reader.rb', line 73 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.
52 53 54 55 |
# File 'lib/qif/reader.rb', line 52 def transactions read_all_transactions transaction_cache end |