class FinancialCalculator::Transaction
the Transaction
class provides a general interface for working with individual cash flows. @api public
Attributes
@return [DecNum] the cash value of the transaction @api public
@return [Date] the date of the transaction @api public
@return [Integer] the period number of the transaction @note this attribute is mainly used in the case of mortgage amortization with no dates @api public
Public Class Methods
create a new Transaction
@return [Transaction] @param [Numeric] amount the cash value of the transaction @param [optional, Hash] opts sets optional attributes @option opts [String] :period the period number of the transaction @example a simple transaction
t = Transaction.new(400)
@example a transaction with a period number
t = Transaction.new(400, :period => 3)
@api public
# File lib/financial_calculator/transaction.rb, line 49 def initialize(amount, opts={}) @amount = amount @original = amount # Set optional attributes.. opts.each do |key, value| send("#{key}=", value) end end
Public Instance Methods
Set the cash value of the transaction @return None @param [Numeric] value the cash value @example
t = Transaction.new(500) t.amount = 750 t.amount #=> 750
@api public
# File lib/financial_calculator/transaction.rb, line 24 def amount=(value) @amount = Flt::DecNum.new(value.to_s) end
@return [DecNum] the difference between the original transaction
amount and the current amount
@example
t = Transaction.new(500) t.amount = 750 t.difference #=> DecNum('250')
@api public
# File lib/financial_calculator/transaction.rb, line 35 def difference @amount - @original end
@api public
# File lib/financial_calculator/transaction.rb, line 71 def inspect "Transaction(#{@amount})" end
@return [Boolean] whether or not the Transaction
is an Interest
transaction @example
pmt = Payment.new(500) int = Interest.new(500) pmt.interest? #=> False int.interest? #=> True
@api public
# File lib/financial_calculator/transaction.rb, line 66 def interest? self.instance_of? Interest end
Modify a Transaction's amount by passing a block @return none @note self is passed as the argument to the block. This makes any public attribute available. @example add $100 to a monthly payment
pmt = Payment.new(-500) pmt.modify { |t| t.amount-100 } pmt.amount #=> -600
@api public
# File lib/financial_calculator/transaction.rb, line 83 def modify @amount = yield(self) end
(see amount
) @deprecated Provided for backwards compatibility
# File lib/financial_calculator/transaction.rb, line 89 def payment @amount end
@return [Boolean] whether or not the Transaction
is a Payment
transaction @example
pmt = Payment.new(500) int = Interest.new(500) pmt.payment? #=> True int.payment? #=> False
@api public
# File lib/financial_calculator/transaction.rb, line 100 def payment? self.instance_of? Payment end