module Aloe::Ledger

Ledger is the façade interface to the account. When manipulating accounts (eg. creating transactions between accounts) you should use the Ledger and not the underlying models.

Attributes

account_scope[W]

Public Instance Methods

create_entry(amount, options) click to toggle source

Creates entry in the ledger.

Creates entries in both credit and debit account and linking transaction between those two entries. Credit and debit accounts and given amount have to be in the same currency otherwise an exception is raised.

@param [Money] amount The amount of money @param [Hash] options Options @option options [Aloe::Account] :from Account which to debit @option options [Aloe::Account] :to Account which to credit @option options [Fixnum] :type Type of transaction @return [Aloe::Transaction]

# File lib/aloe/ledger.rb, line 50
def create_entry(amount, options)
  Aloe::LedgerEntry.new(amount, options).create!
end
default_currency() click to toggle source

Return the default currency that gets used when no currency is specified.

@return [String]

# File lib/aloe/ledger.rb, line 57
def default_currency
  Money.default_currency.to_s
end
find_account(owner, currency = default_currency) click to toggle source

Returns an account for given owner.

@param [Object, Symbol] owner Account owner or account name @param [String, Symbol] currency Currency symbol @return [Aloe::Account, nil] Account which belongs to given object

or nil
# File lib/aloe/ledger.rb, line 22
def find_account(owner, currency = default_currency)
  scope_for_owner(owner).currency(currency.to_s).first
end
find_accounts(owner, currency = nil) click to toggle source

Returns accounts for a given owner.

@param [Object, Symbol] owner Account owner or account name @param [String, Symbol] currency Currency symbol @return [Aloe::Account, nil] Array of accounts which belongs to

given object
# File lib/aloe/ledger.rb, line 32
def find_accounts(owner, currency = nil)
  scope = scope_for_owner(owner)
  currency ? scope.currency(currency) : scope
end

Protected Instance Methods

account_scope() click to toggle source

Return the scope for Accounts repository.

@return [ActiveRecord::Relation]

# File lib/aloe/ledger.rb, line 78
def account_scope
  @account_scope ||= Aloe::Account.unscoped
end
scope_for_owner(owner) click to toggle source

Return the scope for account with given owner.

@param [Object] @return [ActiveRecord::Relation]

# File lib/aloe/ledger.rb, line 67
def scope_for_owner(owner)
  if owner.class.respond_to?(:model_name)
    account_scope.owner(owner)
  else
    account_scope.where(name: owner.to_s.titleize)
  end
end