Class: OFX::TransactionCollection

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

Constructor Details

#initialize(transactions) ⇒ TransactionCollection

Returns a new instance of TransactionCollection.

Parameters:

  • transactions (Array<Transaction>)

    the list of transactions



13
14
15
# File 'lib/ofx_kit/domain/transaction_collection.rb', line 13

def initialize(transactions)
  @transactions = transactions
end

Instance Method Details

#==(other) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


74
75
76
# File 'lib/ofx_kit/domain/transaction_collection.rb', line 74

def ==(other)
  @transactions == other.to_a
end

#creditsTransactionCollection

Returns a new collection containing only positive-amount transactions.

Returns:



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

#debitsTransactionCollection

Returns a new collection containing only negative-amount transactions.

Returns:



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.

Yield Parameters:



19
20
21
# File 'lib/ofx_kit/domain/transaction_collection.rb', line 19

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

#lengthInteger

Returns number of transactions in the collection.

Returns:

  • (Integer)

    number of transactions in the collection



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

def length
  @transactions.length
end

#netMoney

Note:

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).

Returns:

  • (Money)

    net amount (credits + debits)



63
64
65
# File 'lib/ofx_kit/domain/transaction_collection.rb', line 63

def net
  total_credits + total_debits
end

#statementObject

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_aArray<Transaction>

Returns a duplicate of the internal transactions array.

Returns:

  • (Array<Transaction>)

    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_creditsMoney

Returns sum of all positive transaction amounts.

Returns:

  • (Money)

    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_debitsMoney

Returns sum of all negative transaction amounts.

Returns:

  • (Money)

    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