Class: Reporter::Field::CalculationField

Inherits:
Base
  • Object
show all
Defined in:
lib/reporter/field/calculation_field.rb

Direct Known Subclasses

AverageField, CountField, SumField

Instance Method Summary collapse

Constructor Details

#initialize(structure, alias_name, data_source, calculation, column, options = {}, &block) ⇒ CalculationField



3
4
5
6
7
8
9
10
# File 'lib/reporter/field/calculation_field.rb', line 3

def initialize structure, alias_name, data_source, calculation, column, options = {}, &block
  super structure, alias_name
  @source = data_source
  @column = column
  @options = options
  @calculation = calculation
  @calculation_block = block if block_given?
end

Instance Method Details

#calculate_value(data_source, calculation_options) ⇒ Object



12
13
14
15
16
# File 'lib/reporter/field/calculation_field.rb', line 12

def calculate_value data_source, calculation_options
  source = data_source.get(@source)
  value = source.calculate @calculation, @column, options, &calculation_block
  Reporter::Value.new(name, options[:name], value, nil, options[:description], options[:source_link])
end

#preload_for_period(data_source, calculation_options, period, filter, scope) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/reporter/field/calculation_field.rb', line 18

def preload_for_period data_source, calculation_options, period, filter, scope
  source = data_source.get(@source)
  values = source.calculate_for_period @calculation, period, filter, scope, @column, options, &calculation_block
  results = {}
  values.each do |r|
    val = r.delete :value
    results[r] = Reporter::Value.new(name, options[:name], val, nil, options[:description], options[:source_link])
  end
  results
end