Class: OFX::TransactionCollection

Inherits:
Object
  • Object
show all
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

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

#creditsObject

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

#debitsObject

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

#eachObject

Iterates over each transaction.

:yields: transaction



25
26
27
# File 'lib/ofx_kit/transaction_collection.rb', line 25

def each(&)
  @transactions.each(&)
end

#lengthObject

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

#netObject

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

#statementObject

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_aObject

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_creditsObject

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_debitsObject

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