Class: Ledgerous

Inherits:
Object
  • Object
show all
Defined in:
lib/ledgerous.rb,
lib/ledgerous/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLedgerous

Returns a new instance of Ledgerous.



10
11
12
13
# File 'lib/ledgerous.rb', line 10

def initialize
  @accounts = Hash.new(0)
  @threshold = 0
end

Instance Attribute Details

#accountsObject (readonly)

Returns the value of attribute accounts.



6
7
8
# File 'lib/ledgerous.rb', line 6

def accounts
  @accounts
end

#paymentsObject (readonly)

Returns the value of attribute payments.



6
7
8
# File 'lib/ledgerous.rb', line 6

def payments
  @payments
end

#thresholdObject

Returns the value of attribute threshold.



8
9
10
# File 'lib/ledgerous.rb', line 8

def threshold
  @threshold
end

Instance Method Details

#creditorsObject

Returns all accounts wiht positive balances



29
30
31
# File 'lib/ledgerous.rb', line 29

def creditors
  @accounts.select {|_, amt| amt > 0}
end

#debtorsObject

Returns all accounts with negative balances



24
25
26
# File 'lib/ledgerous.rb', line 24

def debtors
  @accounts.select {|_, amt| amt < 0}
end

#empty?Boolean

Returns true if there are no open accounts above or below the threshold

Returns:

  • (Boolean)


16
17
18
19
20
21
# File 'lib/ledgerous.rb', line 16

def empty?
  @accounts.each do |k, v| 
    return false if v.abs > @threshold
  end
  return true
end

#reconcile(t) ⇒ Object

Reconciles the ledgerous with the transaction



34
35
36
37
# File 'lib/ledgerous.rb', line 34

def reconcile(t)
  (t.creditor, t.amount)
  (t.debtor, -t.amount)
end

#settleObject

Returns list of transactions that will settle open accounts



40
41
42
43
# File 'lib/ledgerous.rb', line 40

def settle
  s = Marshal.load(Marshal.dump(self))
  s.settle!
end

#settle!Object

Returns a list of transactions that will settle open accounts, and applies them to clear accounts



47
48
49
50
51
52
53
54
# File 'lib/ledgerous.rb', line 47

def settle!
  payments = []
  next_payment do |n|
    reconcile(n)
    payments << n.reverse
  end
  payments
end