Class: Ledgerous
- Inherits:
-
Object
- Object
- Ledgerous
- Defined in:
- lib/ledgerous.rb,
lib/ledgerous/version.rb
Constant Summary collapse
- VERSION =
"0.0.1"
Instance Attribute Summary collapse
-
#accounts ⇒ Object
readonly
Returns the value of attribute accounts.
-
#payments ⇒ Object
readonly
Returns the value of attribute payments.
-
#threshold ⇒ Object
Returns the value of attribute threshold.
Instance Method Summary collapse
-
#creditors ⇒ Object
Returns all accounts wiht positive balances.
-
#debtors ⇒ Object
Returns all accounts with negative balances.
-
#empty? ⇒ Boolean
Returns true if there are no open accounts above or below the threshold.
-
#initialize ⇒ Ledgerous
constructor
A new instance of Ledgerous.
-
#reconcile(t) ⇒ Object
Reconciles the ledgerous with the transaction.
-
#settle ⇒ Object
Returns list of transactions that will settle open accounts.
-
#settle! ⇒ Object
Returns a list of transactions that will settle open accounts, and applies them to clear accounts.
Constructor Details
#initialize ⇒ Ledgerous
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
#accounts ⇒ Object (readonly)
Returns the value of attribute accounts.
6 7 8 |
# File 'lib/ledgerous.rb', line 6 def accounts @accounts end |
#payments ⇒ Object (readonly)
Returns the value of attribute payments.
6 7 8 |
# File 'lib/ledgerous.rb', line 6 def payments @payments end |
#threshold ⇒ Object
Returns the value of attribute threshold.
8 9 10 |
# File 'lib/ledgerous.rb', line 8 def threshold @threshold end |
Instance Method Details
#creditors ⇒ Object
Returns all accounts wiht positive balances
29 30 31 |
# File 'lib/ledgerous.rb', line 29 def creditors @accounts.select {|_, amt| amt > 0} end |
#debtors ⇒ Object
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
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) update_or_create_account(t.creditor, t.amount) update_or_create_account(t.debtor, -t.amount) end |
#settle ⇒ Object
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 |