Class: Cubicle::Measure

Inherits:
Member
  • Object
show all
Defined in:
lib/cubicle/measure.rb

Direct Known Subclasses

CalculatedMeasure, Duration

Instance Attribute Summary collapse

Attributes inherited from Member

#alias_list, #condition, #expression_type, #field_name, #name, #options

Instance Method Summary collapse

Methods inherited from Member

#included_in?, #matches, #to_js_keys, #to_js_value

Constructor Details

#initialize(*args) ⇒ Measure

Returns a new instance of Measure.



4
5
6
7
# File 'lib/cubicle/measure.rb', line 4

def initialize(*args)
  super
  @aggregation_method = self.options.delete(:aggregation_method) || default_aggregation_method
end

Instance Attribute Details

#aggregation_methodObject

can be :sum, :average, :count



9
10
11
# File 'lib/cubicle/measure.rb', line 9

def aggregation_method
  @aggregation_method
end

Instance Method Details

#aggregate(values) ⇒ Object



29
30
31
32
33
# File 'lib/cubicle/measure.rb', line 29

def aggregate(values)
  return nil if values.blank?
  sum = values.inject(0){|total,val|total+val}
  aggregation_method == :average ? sum/values.length : sum
end

#default_aggregation_methodObject



21
22
23
# File 'lib/cubicle/measure.rb', line 21

def default_aggregation_method
  :count
end

#distinct_count?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/cubicle/measure.rb', line 25

def distinct_count?
  aggregation_method==:count && options[:distinct]
end

#expressionObject

def to_js_value

  return super unless aggregation_method == :count
  "((#{super}) ? 1 : 0)"
end


16
17
18
19
# File 'lib/cubicle/measure.rb', line 16

def expression
  return "((#{super}) ? 1 : 0)" if aggregation_method == :count && options[:distinct] != true
  super
end

#finalize_aggregation(aggregation) ⇒ Object



35
36
37
38
39
40
# File 'lib/cubicle/measure.rb', line 35

def finalize_aggregation(aggregation)
  aggregation[name] = aggregation[name].length if distinct_count? &&
          aggregation[name] &&
          aggregation[name].respond_to?(:length)
  aggregation
end