Class: RockBooks::AcctAmount

Inherits:
Struct
  • Object
show all
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

Class Method Summary collapse

Instance Attribute Details

#amountObject

Returns the value of attribute amount

Returns:

  • (Object)

    the current value of amount



5
6
7
# File 'lib/rock_books/types/acct_amount.rb', line 5

def amount
  @amount
end

#codeObject

Returns the value of attribute code

Returns:

  • (Object)

    the current value of code



5
6
7
# File 'lib/rock_books/types/acct_amount.rb', line 5

def code
  @code
end

#dateObject

Returns the value of attribute date

Returns:

  • (Object)

    the current value of date



5
6
7
# File 'lib/rock_books/types/acct_amount.rb', line 5

def date
  @date
end

#journal_entry_contextObject

Returns the value of attribute journal_entry_context

Returns:

  • (Object)

    the current value of 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.(acct_amounts)
  totals = acct_amounts.each_with_object(Hash.new(0)) do |acct_amount, |
    [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, )
  acct_amounts.select { |acct_amount| acct_amount.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, )
  containing_code(acct_amounts, ) \
      .map(&:amount) \
      .sum
end