class Financier::Ledger

Attributes

transactions[R]

Public Class Methods

new(*transactions) click to toggle source
# File lib/financier/ledger.rb, line 5
def initialize(*transactions)
  send(:transactions=, *transactions)
end

Public Instance Methods

<<(transaction) click to toggle source
# File lib/financier/ledger.rb, line 17
def <<(transaction)
  transaction = Transaction.new(transaction.to_s) unless
    transaction.is_a?(Transaction)

  @transactions << transaction
  @transactions.sort {|a, b| a.date <=> b.date }

  transaction
end
Also aliased as: push
balance(before: nil, after: nil, between: nil, within: nil) click to toggle source
# File lib/financier/ledger.rb, line 36
def balance(before: nil, after: nil, between: nil, within: nil)
  transactions_to_sum = transactions.dup

  between = (within.first - 1.day)..(within.last + 1.day) if within
  after, before = [between.first, between.last] if between
  transactions_to_sum.select! { |t| t.before?(before) } if before
  transactions_to_sum.select! { |t| t.after?(after) } if after

  transactions_to_sum
    .collect(&:amount)
    .sum
    .round(2)
end
credits() click to toggle source
# File lib/financier/ledger.rb, line 28
def credits
  transactions.select(&:credit?)
end
debits() click to toggle source
# File lib/financier/ledger.rb, line 32
def debits
  transactions.select(&:debit?)
end
push(transaction)
Alias for: <<
transactions=(*transactions) click to toggle source
# File lib/financier/ledger.rb, line 9
def transactions=(*transactions)
  @transactions = []
  transactions.flatten.each do |transaction|
    send(:<<, transaction)
  end
  @transactions
end