class Aloe::Account

Public Instance Methods

balance() click to toggle source

Return the balance of account

@return [Money] The balance

# File lib/aloe/account.rb, line 62
def balance
  Money.new read_attribute(:balance), currency
end
balance_at(time) click to toggle source

Computes the balance of the account at the requested date and time. Returns nil if the account did not exist at that time.

@param [Time] the date and time @return [Money] the past balance

# File lib/aloe/account.rb, line 71
def balance_at(time)
  return nil unless created_at <= time
  amount = entries.where('created_at >= ?', time).sum(&:amount)
  offset = amount == 0 ? Money.new(0, currency) : amount
  balance - offset
end
balance_of?(amount, option = nil) click to toggle source

Does account have minimum given balance?

@param [Money, Fixnum] amount Amount in question in cents @param [NilClass, Symbol] option Option @return [true, false]

# File lib/aloe/account.rb, line 83
def balance_of?(amount, option = nil)
  reload if option == :reload
  cents_amount = amount.respond_to?(:cents) ? amount.cents : amount
  read_attribute(:balance) >= cents_amount
end
closeable?() click to toggle source

Can the account be closed?

An account can be closed only if the balance is 0.

@return [true, false]

# File lib/aloe/account.rb, line 94
def closeable?
  balance.zero?
end
create_entry(cents_amount) click to toggle source

Creates entry in the account.

Creates new entry and modified the balance.

@param [Fixnum] cents_amount Amount in cents @return [Aloe::Entry] Created entry

# File lib/aloe/account.rb, line 104
def create_entry(cents_amount)
  with_lock(true) do
    if cents_amount < 0 && !debit_possible?(-cents_amount)
      raise Aloe::InsufficientBalanceError.new(self, -cents_amount)
    end
    entry = entries.create! amount: cents_amount
    increment! :balance, cents_amount
    entry
  end
end
currency?(currency_in_question) click to toggle source

Is account in given currency?

@param [Currency, Symbol, String] currency_in_question @return [true, false]

# File lib/aloe/account.rb, line 119
def currency?(currency_in_question)
  currency.to_s == currency_in_question.to_s
end
debit_possible?(amount) click to toggle source

Is the debit of given amount possible?

@param [Money, Fixnum] amount Amount in question in cents @return [true, false]

# File lib/aloe/account.rb, line 127
def debit_possible?(amount)
  allow_negative_balance ? true : balance_of?(amount, :reload)
end
owner_type_and_id() click to toggle source

Return account owner type and it’s ID.

@return [String]

# File lib/aloe/account.rb, line 156
def owner_type_and_id
  "#{owner_type} #{owner_id}" if owner.present?
end
rollback_all() click to toggle source

Rolls back all transactions on this account.

# File lib/aloe/account.rb, line 132
def rollback_all
  transactions = entries.map &:transaction
  transactions.map &:rollback
end
to_s() click to toggle source

Return string representation of account.

@return [String]

# File lib/aloe/account.rb, line 140
def to_s
  name? ? name : owner_type_and_id
end
turnover(period) click to toggle source

Return account turnover over given period of time.

@param [Range] period @return [Money]

# File lib/aloe/account.rb, line 148
def turnover(period)
  turnover = entries.where(created_at: period).sum &:amount
  turnover == 0 ? Money.new(0, currency) : turnover
end

Protected Instance Methods

has_conflicting_account_for_owner?() click to toggle source
# File lib/aloe/account.rb, line 169
def has_conflicting_account_for_owner?
  new_record? && owner.present? &&
    self.class.owner(owner).currency(currency).where(name: name).exists?
end
uniqueness_of_owner() click to toggle source
# File lib/aloe/account.rb, line 162
def uniqueness_of_owner
  if has_conflicting_account_for_owner?
    message = I18n.t('aloe.accounts.errors.owner_already_has_account')
    errors.add(:owner, message)
  end
end