Class: OFX::TransactionCollection
- Inherits:
-
Object
- Object
- OFX::TransactionCollection
- Includes:
- Enumerable
- Defined in:
- lib/ofx_kit/transaction_collection.rb
Overview
An Enumerable collection of Transaction objects parsed from an OFX statement. Provides convenience filters for credits and debits.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Returns
trueif both collections contain the same transactions asother. -
#credits ⇒ Object
Returns a new TransactionCollection containing only positive-amount transactions.
-
#debits ⇒ Object
Returns a new TransactionCollection containing only negative-amount transactions.
-
#each ⇒ Object
Iterates over each transaction.
-
#initialize(transactions) ⇒ TransactionCollection
constructor
Creates a new collection from
transactions(Array of Transaction). -
#length ⇒ Object
Returns the number of transactions in the collection (Integer).
-
#net ⇒ Object
Returns the net amount (credits + debits) as a Money object.
-
#statement ⇒ Object
The statement (BankStatement, CreditCardStatement, or
nil) this collection belongs to. -
#to_a ⇒ Object
Returns a duplicate of the internal transactions array.
-
#total_credits ⇒ Object
Returns the sum of all positive transaction amounts as a Money object.
-
#total_debits ⇒ Object
Returns the sum of all negative transaction amounts as a Money object.
Constructor Details
#initialize(transactions) ⇒ TransactionCollection
Creates a new collection from transactions (Array of Transaction).
17 18 19 |
# File 'lib/ofx_kit/transaction_collection.rb', line 17 def initialize(transactions) @transactions = transactions end |
Instance Method Details
#==(other) ⇒ Object
Returns true if both collections contain the same transactions as other.
110 111 112 |
# File 'lib/ofx_kit/transaction_collection.rb', line 110 def ==(other) @transactions == other.to_a end |
#credits ⇒ Object
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>
42 43 44 45 46 47 48 49 50 |
# 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 |
#debits ⇒ Object
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
60 61 62 63 64 65 66 67 |
# 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 |
#each ⇒ Object
Iterates over each transaction.
:yields: transaction
25 26 27 |
# File 'lib/ofx_kit/transaction_collection.rb', line 25 def each(&) @transactions.each(&) end |
#length ⇒ Object
Returns the number of transactions in the collection (Integer).
31 32 33 |
# File 'lib/ofx_kit/transaction_collection.rb', line 31 def length @transactions.length end |
#net ⇒ Object
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"
98 99 100 |
# File 'lib/ofx_kit/transaction_collection.rb', line 98 def net total_credits + total_debits end |
#statement ⇒ Object
The statement (BankStatement, CreditCardStatement, or nil) this collection belongs to. Overridden per-instance by Base::Builder at build time.
13 |
# File 'lib/ofx_kit/transaction_collection.rb', line 13 def statement = nil |
#to_a ⇒ Object
Returns a duplicate of the internal transactions array.
104 105 106 |
# File 'lib/ofx_kit/transaction_collection.rb', line 104 def to_a @transactions.dup end |
#total_credits ⇒ Object
Returns the sum of all positive transaction amounts as a Money object.
Example
ofx.transactions.total_credits.format #=> "$350.00"
75 76 77 |
# File 'lib/ofx_kit/transaction_collection.rb', line 75 def total_credits sum_amounts(credits) end |
#total_debits ⇒ Object
Returns the sum of all negative transaction amounts as a Money object.
Example
ofx.transactions.total_debits.format #=> "-$1,234.56"
85 86 87 |
# File 'lib/ofx_kit/transaction_collection.rb', line 85 def total_debits sum_amounts(debits) end |