Class: OFX::TransactionCollection
- Inherits:
-
Object
- Object
- OFX::TransactionCollection
- Includes:
- Enumerable
- Defined in:
- lib/ofx_kit/domain/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) ⇒ Boolean
-
#credits ⇒ TransactionCollection
A new collection containing only positive-amount transactions.
-
#debits ⇒ TransactionCollection
A new collection containing only negative-amount transactions.
-
#each {|transaction| ... } ⇒ Object
Iterates over each transaction.
-
#initialize(transactions) ⇒ TransactionCollection
constructor
A new instance of TransactionCollection.
-
#length ⇒ Integer
Number of transactions in the collection.
-
#net ⇒ Money
Net amount (credits + debits).
-
#statement ⇒ Object
Placeholder overridden per-instance by Base::Builder#wire at build time.
-
#to_a ⇒ Array<Transaction>
A duplicate of the internal transactions array.
-
#total_credits ⇒ Money
Sum of all positive transaction amounts.
-
#total_debits ⇒ Money
Sum of all negative transaction amounts.
Constructor Details
#initialize(transactions) ⇒ TransactionCollection
Returns a new instance of TransactionCollection.
13 14 15 |
# File 'lib/ofx_kit/domain/transaction_collection.rb', line 13 def initialize(transactions) @transactions = transactions end |
Instance Method Details
#==(other) ⇒ Boolean
74 75 76 |
# File 'lib/ofx_kit/domain/transaction_collection.rb', line 74 def ==(other) @transactions == other.to_a end |
#credits ⇒ TransactionCollection
Returns a new collection containing only positive-amount transactions.
29 30 31 32 33 34 35 36 37 |
# File 'lib/ofx_kit/domain/transaction_collection.rb', line 29 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 ⇒ TransactionCollection
Returns a new collection containing only negative-amount transactions.
40 41 42 43 44 45 46 47 |
# File 'lib/ofx_kit/domain/transaction_collection.rb', line 40 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 {|transaction| ... } ⇒ Object
Iterates over each transaction.
19 20 21 |
# File 'lib/ofx_kit/domain/transaction_collection.rb', line 19 def each(&) @transactions.each(&) end |
#length ⇒ Integer
Returns number of transactions in the collection.
24 25 26 |
# File 'lib/ofx_kit/domain/transaction_collection.rb', line 24 def length @transactions.length end |
#net ⇒ Money
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.
Returns net amount (credits + debits).
63 64 65 |
# File 'lib/ofx_kit/domain/transaction_collection.rb', line 63 def net total_credits + total_debits end |
#statement ⇒ Object
Placeholder overridden per-instance by Base::Builder#wire at build time.
10 |
# File 'lib/ofx_kit/domain/transaction_collection.rb', line 10 def statement = nil |
#to_a ⇒ Array<Transaction>
Returns a duplicate of the internal transactions array.
68 69 70 |
# File 'lib/ofx_kit/domain/transaction_collection.rb', line 68 def to_a @transactions.dup end |
#total_credits ⇒ Money
Returns sum of all positive transaction amounts.
50 51 52 |
# File 'lib/ofx_kit/domain/transaction_collection.rb', line 50 def total_credits sum_amounts(credits) end |
#total_debits ⇒ Money
Returns sum of all negative transaction amounts.
55 56 57 |
# File 'lib/ofx_kit/domain/transaction_collection.rb', line 55 def total_debits sum_amounts(debits) end |