Class: Finance::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/finance/transaction.rb

Overview

the Transaction class provides a general interface for working with individual cash flows.

Direct Known Subclasses

Interest, Payment

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(amount, opts = {}) ⇒ Transaction

create a new Transaction

Examples:

a simple transaction

t = Transaction.new(400)

a transaction with a period number

t = Transaction.new(400, :period => 3)

Parameters:

  • amount (Numeric)

    the cash value of the transaction

  • opts (optional, Hash) (defaults to: {})

    sets optional attributes

Options Hash (opts):

  • :period (String)

    the period number of the transaction



48
49
50
51
52
53
54
55
56
# File 'lib/finance/transaction.rb', line 48

def initialize(amount, opts={})
  @amount = amount
  @original = amount
  
  # Set optional attributes..
  opts.each do |key, value|
    send("#{key}=", value)
  end
end

Instance Attribute Details

#amountDecNum

Returns the cash value of the transaction.

Returns:

  • (DecNum)

    the cash value of the transaction



9
10
11
# File 'lib/finance/transaction.rb', line 9

def amount
  @amount
end

#periodInteger

Note:

this attribute is mainly used in the case of mortgage amortization with no dates

Returns the period number of the transaction.

Returns:

  • (Integer)

    the period number of the transaction



13
14
15
# File 'lib/finance/transaction.rb', line 13

def period
  @period
end

Instance Method Details

#differenceDecNum

Returns the difference between the original transaction amount and the current amount.

Examples:

t = Transaction.new(500)
t.amount = 750
t.difference #=> DecNum('250')

Returns:

  • (DecNum)

    the difference between the original transaction amount and the current amount



34
35
36
# File 'lib/finance/transaction.rb', line 34

def difference
  @amount - @original
end

#inspectObject



70
71
72
# File 'lib/finance/transaction.rb', line 70

def inspect
  "Transaction(#{@amount})"
end

#interest?Boolean

Returns whether or not the Transaction is an Interest transaction.

Examples:

pmt = Payment.new(500)
int = Interest.new(500)
pmt.interest? #=> False
int.interest? #=> True

Returns:

  • (Boolean)

    whether or not the Transaction is an Interest transaction



65
66
67
# File 'lib/finance/transaction.rb', line 65

def interest?
  self.instance_of? Interest
end

#modifyObject

Note:

self is passed as the argument to the block. This makes any public attribute available.

Modify a Transaction’s amount by passing a block

Examples:

add $100 to a monthly payment

pmt = Payment.new(-500)
pmt.modify { |t| t.amount-100 }
pmt.amount #=> -600

Returns:

  • none



82
83
84
# File 'lib/finance/transaction.rb', line 82

def modify
  @amount = yield(self)
end

#paymentDecNum

Deprecated.

Provided for backwards compatibility

Returns the cash value of the transaction.

Returns:

  • (DecNum)

    the cash value of the transaction



88
89
90
# File 'lib/finance/transaction.rb', line 88

def payment
  @amount
end

#payment?Boolean

Returns whether or not the Transaction is a Payment transaction.

Examples:

pmt = Payment.new(500)
int = Interest.new(500)
pmt.payment? #=> True
int.payment? #=> False

Returns:

  • (Boolean)

    whether or not the Transaction is a Payment transaction



99
100
101
# File 'lib/finance/transaction.rb', line 99

def payment?
  self.instance_of? Payment
end