class Aloe::LedgerEntry

LedgerEntry is use-case class that encompasses the process of moving funds between accounts.

Public Class Methods

new(*args) click to toggle source
Calls superclass method
# File lib/aloe/ledger_entry.rb, line 11
def initialize(*args)
  super
  raise Aloe::InvalidAmountError if amount.zero?
  unless same_currencies?
    raise Aloe::InvalidCurrencyError.new(debit_account, credit_account,
      amount.currency)
  end
  unless debit_account.open?
    raise Aloe::InoperableAccountError.new(debit_account)
  end
  unless credit_account.open?
    raise Aloe::InoperableAccountError.new(credit_account)
  end
end

Public Instance Methods

create!() click to toggle source
# File lib/aloe/ledger_entry.rb, line 26
def create!
  ActiveRecord::Base.transaction do
    debit_entry = debit_account.create_entry(-amount.cents)
    credit_entry = credit_account.create_entry(amount.cents)
    attributes = { credit_entry: credit_entry,
                   debit_entry: debit_entry,
                   category: category }.merge options
    Aloe::Transaction::create! attributes
  end
end

Protected Instance Methods

category() click to toggle source
# File lib/aloe/ledger_entry.rb, line 39
def category
  @category ||= options.delete(:type)
end
credit_account() click to toggle source

@return [Aloe::Account]

# File lib/aloe/ledger_entry.rb, line 44
def credit_account
  @credit_account ||= may_find_account(options.delete(:to), currency)
end
currency() click to toggle source

@return [String]

# File lib/aloe/ledger_entry.rb, line 49
def currency
  @currency ||= amount.currency
end
debit_account() click to toggle source

@return [Aloe::Account]

# File lib/aloe/ledger_entry.rb, line 54
def debit_account
  @debit_account ||= may_find_account(options.delete(:from), currency)
end
ledger() click to toggle source

@return [Class]

# File lib/aloe/ledger_entry.rb, line 59
def ledger
  Aloe::Ledger
end
may_find_account(account_or_owner, currency) click to toggle source

@param [Object, Aloe::Aloe] account_or_owner @param [String, Symbol] currency @return [Aloe::Account]

# File lib/aloe/ledger_entry.rb, line 66
def may_find_account(account_or_owner, currency)
  if account_or_owner.respond_to?(:create_entry)
    account_or_owner
  elsif account_or_owner
    ledger.find_account account_or_owner, currency
  end
end
same_currencies?() click to toggle source

Are the two accounts of the matching currency as the given amount?

@return [true, false]

# File lib/aloe/ledger_entry.rb, line 77
def same_currencies?
  debit_account.currency?(amount.currency) &&
    credit_account.currency?(amount.currency)
end