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



51
52
53
54
55
56
57
58
59
# File 'lib/finance/transaction.rb', line 51

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

#dateDate

Returns the date of the transaction.

Returns:

  • (Date)

    the date of the transaction



16
17
18
# File 'lib/finance/transaction.rb', line 16

def date
  @date
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



37
38
39
# File 'lib/finance/transaction.rb', line 37

def difference
  @amount - @original
end

#inspectObject



73
74
75
# File 'lib/finance/transaction.rb', line 73

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



68
69
70
# File 'lib/finance/transaction.rb', line 68

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



85
86
87
# File 'lib/finance/transaction.rb', line 85

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



91
92
93
# File 'lib/finance/transaction.rb', line 91

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



102
103
104
# File 'lib/finance/transaction.rb', line 102

def payment?
  self.instance_of? Payment
end