class RockBooks::AcctAmount

This class represents an account code and an amount. Journal entries will have multiple instances of these.

Public Class Methods

aggregate_amounts_by_account(acct_amounts) click to toggle source

Returns a hash whose keys are account codes and values are the totals for those codes. The 'aggregate' in the method name is intended to be a noun, not a verb.

# File lib/rock_books/types/acct_amount.rb, line 24
def self.aggregate_amounts_by_account(acct_amounts)
  totals = acct_amounts.each_with_object(Hash.new(0)) do |acct_amount, by_account|
    by_account[acct_amount.code] += acct_amount.amount
  end
  totals.each do |code, amount |
    totals[code] = amount.round(2)
  end
end
containing_code(acct_amounts, account_code) click to toggle source

Returns the subset of the passed array of acct_amount's that contain the specified account code

# File lib/rock_books/types/acct_amount.rb, line 35
def self.containing_code(acct_amounts, account_code)
  acct_amounts.select { |acct_amount| acct_amount.code == account_code }
end
create_with_chart_validation(date, code, amount, journal_entry_context) click to toggle source

Same as constructor except it raises an error if the account code is not in the chart of accounts.

# File lib/rock_books/types/acct_amount.rb, line 9
def self.create_with_chart_validation(date, code, amount, journal_entry_context)
  unless journal_entry_context.chart_of_accounts.include?(code)
    raise AccountNotFoundError.new(code, journal_entry_context)
  end
  self.new(date, code, amount)
end
filter(acct_amounts, filter) click to toggle source
# File lib/rock_books/types/acct_amount.rb, line 48
def self.filter(acct_amounts, filter)
  acct_amounts.select { |acct_amount| filter.(acct_amount)}
end
total_amount(acct_amounts) click to toggle source
# File lib/rock_books/types/acct_amount.rb, line 17
def self.total_amount(acct_amounts)
  acct_amounts.inject(0) { |sum, acct_amount| sum += acct_amount.amount }
end
total_amount_for_code(acct_amounts, account_code) click to toggle source

For the passed array of AcctAmount's, calculate the total for a single account.

# File lib/rock_books/types/acct_amount.rb, line 41
def self.total_amount_for_code(acct_amounts, account_code)
  containing_code(acct_amounts, account_code) \
      .map(&:amount) \
      .sum
end