Class: RockBooks::AcctAmount
- Inherits:
-
Struct
- Object
- Struct
- RockBooks::AcctAmount
- Defined in:
- lib/rock_books/types/acct_amount.rb
Overview
This class represents an account code and an amount. Journal entries will have multiple instances of these.
Instance Attribute Summary collapse
-
#amount ⇒ Object
Returns the value of attribute amount.
-
#code ⇒ Object
Returns the value of attribute code.
-
#date ⇒ Object
Returns the value of attribute date.
-
#journal_entry_context ⇒ Object
Returns the value of attribute journal_entry_context.
Class Method Summary collapse
-
.aggregate_amounts_by_account(acct_amounts) ⇒ Object
Returns a hash whose keys are account codes and values are the totals for those codes.
-
.containing_code(acct_amounts, account_code) ⇒ Object
Returns the subset of the passed array of acct_amount’s that contain the specified account code.
-
.create_with_chart_validation(date, code, amount, journal_entry_context) ⇒ Object
Same as constructor except it raises an error if the account code is not in the chart of accounts.
- .filter(acct_amounts, filter) ⇒ Object
- .total_amount(acct_amounts) ⇒ Object
-
.total_amount_for_code(acct_amounts, account_code) ⇒ Object
For the passed array of AcctAmount’s, calculate the total for a single account.
Instance Attribute Details
#amount ⇒ Object
Returns the value of attribute amount
5 6 7 |
# File 'lib/rock_books/types/acct_amount.rb', line 5 def amount @amount end |
#code ⇒ Object
Returns the value of attribute code
5 6 7 |
# File 'lib/rock_books/types/acct_amount.rb', line 5 def code @code end |
#date ⇒ Object
Returns the value of attribute date
5 6 7 |
# File 'lib/rock_books/types/acct_amount.rb', line 5 def date @date end |
#journal_entry_context ⇒ Object
Returns the value of attribute journal_entry_context
5 6 7 |
# File 'lib/rock_books/types/acct_amount.rb', line 5 def journal_entry_context @journal_entry_context end |
Class Method Details
.aggregate_amounts_by_account(acct_amounts) ⇒ Object
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.
24 25 26 27 28 29 30 31 |
# 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) ⇒ Object
Returns the subset of the passed array of acct_amount’s that contain the specified account code
35 36 37 |
# 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) ⇒ Object
Same as constructor except it raises an error if the account code is not in the chart of accounts.
9 10 11 12 13 14 |
# 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) ⇒ Object
48 49 50 |
# 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) ⇒ Object
17 18 19 |
# 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) ⇒ Object
For the passed array of AcctAmount’s, calculate the total for a single account.
41 42 43 44 45 |
# 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 |