class BeanSprout::Ledger

Attributes

base_currency[R]

Public Class Methods

new(base_currency) click to toggle source
# 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

Public Instance Methods

account(id) click to toggle source

TODO: clients can't access ID.

# File lib/bean_sprout/ledger.rb, line 66
def account id
  @beans.fetch(id).to_account
end
accounts() { |to_account| ... } click to toggle source

TODO: test

# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

TODO: clients can't access ID.

# File lib/bean_sprout/ledger.rb, line 71
def transaction id
  @sprout_bunches.fetch(id).to_transaction
end
transactions() { |to_transaction| ... } click to toggle source

TODO: test

# 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) click to toggle source
# 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

Private Instance Methods

commit_entries(entries, other_data = nil) click to toggle source
# File lib/bean_sprout/ledger.rb, line 118
def commit_entries entries, other_data = nil
  sprouts = entries.map do |entry| get_target entry end
  sprout_bunch = @sprout_bunches.store do |next_id|
    SproutBunch.new(next_id, sprouts)
  end

  trans = Transaction.new(sprout_bunch, other_data)
  trans.commit
  trans
end
get_target(obj) click to toggle source
# File lib/bean_sprout/ledger.rb, line 114
def get_target obj
  obj.instance_variable_get :@target
end