Class: Finrb::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/finrb/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



30
31
32
33
34
35
36
37
38
# File 'lib/finrb/transaction.rb', line 30

def initialize(amount, opts = {})
  @amount = amount
  @original = amount

  # Set optional attributes..
  opts.each do |key, value|
    __send__("#{key}=", value)
  end
end

Instance Attribute Details

#amountFlt::DecNum

Returns the cash value of the transaction.

Returns:

  • (Flt::DecNum)

    the cash value of the transaction



11
12
13
# File 'lib/finrb/transaction.rb', line 11

def amount
  @amount
end

#dateDate

Returns the date of the transaction.

Returns:

  • (Date)

    the date of the transaction



18
19
20
# File 'lib/finrb/transaction.rb', line 18

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



15
16
17
# File 'lib/finrb/transaction.rb', line 15

def period
  @period
end

Instance Method Details

#differenceFlt::DecNum

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

Examples:

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

Returns:

  • (Flt::DecNum)

    the difference between the original transaction amount and the current amount



59
60
61
# File 'lib/finrb/transaction.rb', line 59

def difference
  @amount - @original
end

#inspectObject



75
76
77
# File 'lib/finrb/transaction.rb', line 75

def inspect
  "Transaction(#{@amount.round(2)}, date: #{@date})"
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



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

def interest?
  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



87
88
89
# File 'lib/finrb/transaction.rb', line 87

def modify
  @amount = yield(self)
end

#paymentFlt::DecNum

Deprecated.

Provided for backwards compatibility

Returns the cash value of the transaction.

Returns:

  • (Flt::DecNum)

    the cash value of the transaction



93
94
95
# File 'lib/finrb/transaction.rb', line 93

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



104
105
106
# File 'lib/finrb/transaction.rb', line 104

def payment?
  instance_of?(Payment)
end