class OFX::TransactionCollection
An Enumerable collection of Transaction objects parsed from an OFX statement. Provides convenience filters for credits and debits.
Public Class Methods
Source
# File lib/ofx_kit/transaction_collection.rb, line 17 def initialize(transactions) @transactions = transactions end
Creates a new collection from transactions (Array of Transaction).
Public Instance Methods
Source
# File lib/ofx_kit/transaction_collection.rb, line 110 def ==(other) @transactions == other.to_a end
Returns true if both collections contain the same transactions as other.
Source
# File lib/ofx_kit/transaction_collection.rb, line 42 def credits sub = self.class.new(select { |t| t.amount.positive? }) # Propagate statement wiring so inferred_currency resolves to the correct # account currency even when this sub-collection has no transactions. if (stmt = statement) sub.define_singleton_method(:statement) { stmt } end sub end
Returns a new TransactionCollection containing only positive-amount transactions.
Example
ofx.transactions.credits.length #=> 5 ofx.transactions.credits.first.amount #=> #<Money fractional:10000 currency:USD>
Source
# File lib/ofx_kit/transaction_collection.rb, line 60 def debits sub = self.class.new(select { |t| t.amount.negative? }) # Same as credits — statement is the authoritative source of currency. if (stmt = statement) sub.define_singleton_method(:statement) { stmt } end sub end
Returns a new TransactionCollection containing only negative-amount transactions.
Example
ofx.transactions.debits.length #=> 37 ofx.transactions.debits.first.name #=> "AMAZON.COM" ofx.transactions.debits.first.amount_cents #=> -5099
Source
# File lib/ofx_kit/transaction_collection.rb, line 25 def each(&) @transactions.each(&) end
Iterates over each transaction.
Source
# File lib/ofx_kit/transaction_collection.rb, line 31 def length @transactions.length end
Returns the number of transactions in the collection (Integer).
Source
# File lib/ofx_kit/transaction_collection.rb, line 98 def net total_credits + total_debits end
Returns the net amount (credits + debits) as a Money object. This is NOT the account balance — it reflects only the transactions present in the OFX file and ignores any accumulated prior balance (e.g. opening cash balance or unpaid invoice from a previous period). Use Balance for the actual account balance.
Example
ofx.transactions.net.format #=> "$500.00"
Source
# File lib/ofx_kit/transaction_collection.rb, line 13 def statement = nil
The statement (BankStatement, CreditCardStatement, or nil) this collection belongs to. Overridden per-instance by Base::Builder at build time.
Source
# File lib/ofx_kit/transaction_collection.rb, line 104 def to_a @transactions.dup end
Returns a duplicate of the internal transactions array.
Source
# File lib/ofx_kit/transaction_collection.rb, line 75 def total_credits sum_amounts(credits) end
Returns the sum of all positive transaction amounts as a Money object.
Example
ofx.transactions.total_credits.format #=> "$350.00"
Source
# File lib/ofx_kit/transaction_collection.rb, line 85 def total_debits sum_amounts(debits) end
Returns the sum of all negative transaction amounts as a Money object.
Example
ofx.transactions.total_debits.format #=> "-$1,234.56"