Class: Financier::Ledger

Inherits:
Object
  • Object
show all
Defined in:
lib/financier/ledger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*transactions) ⇒ Ledger

Returns a new instance of Ledger.



5
6
7
# File 'lib/financier/ledger.rb', line 5

def initialize(*transactions)
  send(:transactions=, *transactions)
end

Instance Attribute Details

#transactionsObject

Returns the value of attribute transactions.



3
4
5
# File 'lib/financier/ledger.rb', line 3

def transactions
  @transactions
end

Instance Method Details

#<<(transaction) ⇒ Object Also known as: push



17
18
19
20
21
22
23
24
25
# 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

#balance(before: nil, after: nil, between: nil, within: nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# 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

#creditsObject



28
29
30
# File 'lib/financier/ledger.rb', line 28

def credits
  transactions.select(&:credit?)
end

#debitsObject



32
33
34
# File 'lib/financier/ledger.rb', line 32

def debits
  transactions.select(&:debit?)
end