Class: DoubleEntry::Aggregate

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(function, account, code, options) ⇒ Aggregate

Returns a new instance of Aggregate.



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

def initialize(function, , code, options)
  @function = function.to_s
  raise "Function not supported" unless %w[sum count average].include?(@function)

  @account = .to_s
  @code = code ? code.to_s : nil
  @options = options
  @scope = options[:scope]
  @range = options[:range]
  @filter = options[:filter]
end

Instance Attribute Details

#accountObject (readonly)

Returns the value of attribute account.



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

def 
  @account
end

#codeObject (readonly)

Returns the value of attribute code.



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

def code
  @code
end

#filterObject (readonly)

Returns the value of attribute filter.



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

def filter
  @filter
end

#functionObject (readonly)

Returns the value of attribute function.



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

def function
  @function
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#rangeObject (readonly)

Returns the value of attribute range.



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

def range
  @range
end

#scopeObject (readonly)

Returns the value of attribute scope.



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

def scope
  @scope
end

Class Method Details

.formatted_amount(function, amount) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/double_entry/aggregate.rb', line 31

def self.formatted_amount(function, amount)
  safe_amount = amount || 0

  case function.to_s
  when 'count'
    safe_amount
  else
    Money.new(safe_amount)
  end
end

Instance Method Details

#amount(force_recalculation = false) ⇒ Object



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

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

#formatted_amountObject



27
28
29
# File 'lib/double_entry/aggregate.rb', line 27

def formatted_amount
  Aggregate.formatted_amount(function, amount)
end