Class: DoubleEntry::Reporting::Aggregate

Inherits:
Object
  • Object
show all
Defined in:
lib/double_entry/reporting/aggregate.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(function:, account:, code:, range:, partner_account: nil, filter: nil) ⇒ Aggregate

Returns a new instance of Aggregate.



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/double_entry/reporting/aggregate.rb', line 18

def initialize(function:, account:, code:, range:, partner_account: nil, filter: nil)
  @function = function.to_s
  fail AggregateFunctionNotSupported unless %w(sum count average).include?(@function)

  @account         = 
  @code            = code.try(:to_s)
  @range           = range
  @partner_account = 
  @filter          = filter
  @currency        = DoubleEntry::Account.currency()
end

Instance Attribute Details

#accountObject (readonly)

Returns the value of attribute account.



5
6
7
# File 'lib/double_entry/reporting/aggregate.rb', line 5

def 
  @account
end

#codeObject (readonly)

Returns the value of attribute code.



5
6
7
# File 'lib/double_entry/reporting/aggregate.rb', line 5

def code
  @code
end

#currencyObject (readonly)

Returns the value of attribute currency.



5
6
7
# File 'lib/double_entry/reporting/aggregate.rb', line 5

def currency
  @currency
end

#filterObject (readonly)

Returns the value of attribute filter.



5
6
7
# File 'lib/double_entry/reporting/aggregate.rb', line 5

def filter
  @filter
end

#functionObject (readonly)

Returns the value of attribute function.



5
6
7
# File 'lib/double_entry/reporting/aggregate.rb', line 5

def function
  @function
end

#partner_accountObject (readonly)

Returns the value of attribute partner_account.



5
6
7
# File 'lib/double_entry/reporting/aggregate.rb', line 5

def 
  @partner_account
end

#rangeObject (readonly)

Returns the value of attribute range.



5
6
7
# File 'lib/double_entry/reporting/aggregate.rb', line 5

def range
  @range
end

Class Method Details

.formatted_amount(function:, account:, code:, range:, partner_account: nil, filter: nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/double_entry/reporting/aggregate.rb', line 7

def self.formatted_amount(function:, account:, code:, range:, partner_account: nil, filter: nil)
  new(
    function: function,
    account: ,
    code: code,
    range: range,
    partner_account: ,
    filter: filter,
  ).formatted_amount
end

Instance Method Details

#amount(force_recalculation = false) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/double_entry/reporting/aggregate.rb', line 30

def amount(force_recalculation = false)
  if force_recalculation
    clear_old_aggregates
    calculate
  else
    retrieve || calculate
  end
end

#formatted_amount(value = amount) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/double_entry/reporting/aggregate.rb', line 39

def formatted_amount(value = amount)
  value ||= 0
  if function == 'count'
    value
  else
    Money.new(value, currency)
  end
end