Class: BeanSprout::Ledger
- Inherits:
-
Object
- Object
- BeanSprout::Ledger
- Defined in:
- lib/bean_sprout/ledger.rb
Instance Attribute Summary collapse
-
#base_currency ⇒ Object
readonly
Returns the value of attribute base_currency.
Instance Method Summary collapse
-
#account(id) ⇒ Object
TODO: clients can’t access ID.
-
#accounts ⇒ Object
TODO: test.
- #create_account(currency, other_data: nil) ⇒ Object
- #create_entry(account, amount, other_data: nil) ⇒ Object
- #create_transaction(entries, other_data: nil) ⇒ Object
- #expense_account(currency = nil) ⇒ Object
- #forex_account(currency = nil) ⇒ Object
- #forex_transfer(from_acc, to_acc, from_amount, to_amount, other_data: nil) ⇒ Object
- #income_account(currency = nil) ⇒ Object
-
#initialize(base_currency) ⇒ Ledger
constructor
A new instance of Ledger.
-
#transaction(id) ⇒ Object
TODO: clients can’t access ID.
-
#transactions ⇒ Object
TODO: test.
- #transfer(from_acc, to_acc, amount, other_data: nil) ⇒ Object
Constructor Details
#initialize(base_currency) ⇒ Ledger
Returns a new instance of Ledger.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/bean_sprout/ledger.rb', line 10 def initialize base_currency @base_currency = base_currency @beans = SparseArray.new @sprout_bunches = SparseArray.new @sprouts = SparseArray.new @forex_accounts = {} @income_accounts = {} @expense_accounts = {} end |
Instance Attribute Details
#base_currency ⇒ Object (readonly)
Returns the value of attribute base_currency.
8 9 10 |
# File 'lib/bean_sprout/ledger.rb', line 8 def base_currency @base_currency end |
Instance Method Details
#account(id) ⇒ Object
TODO: clients can’t access ID.
66 67 68 |
# File 'lib/bean_sprout/ledger.rb', line 66 def account id @beans.fetch(id).to_account end |
#accounts ⇒ Object
TODO: test
76 77 78 79 80 81 82 83 84 |
# File 'lib/bean_sprout/ledger.rb', line 76 def accounts @beans.values.map do |bean| if block_given? yield bean.to_account else bean.to_account end end end |
#create_account(currency, other_data: nil) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/bean_sprout/ledger.rb', line 21 def create_account currency, other_data: nil currency ||= @base_currency bean = @beans.store do |next_id| Bean.new(next_id, currency) end Account.new(bean, other_data) end |
#create_entry(account, amount, other_data: nil) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/bean_sprout/ledger.rb', line 30 def create_entry account, amount, other_data: nil bean = get_target account if not @beans.has_key? bean.id raise "Unkown account #{bean.to_account} refered." end sprout = @sprouts.store do |next_id| Sprout.new(next_id, bean, amount) end Entry.new(sprout, other_data) end |
#create_transaction(entries, other_data: nil) ⇒ Object
43 44 45 |
# File 'lib/bean_sprout/ledger.rb', line 43 def create_transaction entries, other_data: nil commit_entries entries, other_data end |
#expense_account(currency = nil) ⇒ Object
108 109 110 111 |
# File 'lib/bean_sprout/ledger.rb', line 108 def expense_account currency = nil currency ||= @base_currency @expense_accounts[currency] ||= create_account currency, other_data: "This is an expense account for #{currency}." end |
#forex_account(currency = nil) ⇒ Object
97 98 99 100 101 |
# File 'lib/bean_sprout/ledger.rb', line 97 def forex_account currency = nil currency ||= @base_currency acc = create_account currency, other_data: "This is a forex account for #{currency}." @forex_accounts[currency] ||= acc end |
#forex_transfer(from_acc, to_acc, from_amount, to_amount, other_data: nil) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/bean_sprout/ledger.rb', line 57 def forex_transfer from_acc, to_acc, from_amount, to_amount, other_data: nil entry0 = create_entry from_acc, -from_amount entry1 = create_entry (forex_account from_acc.currency), from_amount entry2 = create_entry to_acc, to_amount entry3 = create_entry (forex_account to_acc.currency), -to_amount commit_entries [entry0, entry1, entry2, entry3], other_data end |
#income_account(currency = nil) ⇒ Object
103 104 105 106 |
# File 'lib/bean_sprout/ledger.rb', line 103 def income_account currency = nil currency ||= @base_currency @income_accounts[currency] ||= create_account currency, other_data: "This is an income account for #{currency}." end |
#transaction(id) ⇒ Object
TODO: clients can’t access ID.
71 72 73 |
# File 'lib/bean_sprout/ledger.rb', line 71 def transaction id @sprout_bunches.fetch(id).to_transaction end |
#transactions ⇒ Object
TODO: test
87 88 89 90 91 92 93 94 95 |
# File 'lib/bean_sprout/ledger.rb', line 87 def transactions @sprout_bunches.values.map do |sprout_bunch| if block_given? yield sprout_bunch.to_transaction else sprout_bunch.to_transaction end end end |
#transfer(from_acc, to_acc, amount, other_data: nil) ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/bean_sprout/ledger.rb', line 47 def transfer from_acc, to_acc, amount, other_data: nil if from_acc.currency != to_acc.currency raise "Cannot transfer between two forex accounts." end entry0 = create_entry from_acc, -amount entry1 = create_entry to_acc, amount commit_entries [entry0, entry1], other_data end |