Class: Cubicle::Ratio

Inherits:
CalculatedMeasure show all
Defined in:
lib/cubicle/ratio.rb

Instance Attribute Summary collapse

Attributes inherited from Measure

#aggregation_method

Attributes inherited from Member

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

Instance Method Summary collapse

Methods inherited from CalculatedMeasure

#to_js_keys

Methods inherited from Measure

#default_aggregation_method, #distinct_count?, #expression

Methods inherited from Member

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

Constructor Details

#initialize(member_name, numerator, denominator, opts = {}) ⇒ Ratio

Returns a new instance of Ratio.



5
6
7
8
9
# File 'lib/cubicle/ratio.rb', line 5

def initialize(member_name,numerator,denominator,opts={})
  @numerator, @denominator = numerator, denominator
  #opts[:expression]="(value.#{denominator} > 0 && value.#{numerator} ? value.#{numerator}/value.#{denominator} : 0)"
  super(member_name,opts)
end

Instance Attribute Details

#denominatorObject (readonly)

Returns the value of attribute denominator.



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

def denominator
  @denominator
end

#numeratorObject (readonly)

Returns the value of attribute numerator.



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

def numerator
  @numerator
end

Instance Method Details

#aggregate(values) ⇒ Object



11
12
13
# File 'lib/cubicle/ratio.rb', line 11

def aggregate(values)
  0
end

#depends_onObject



27
28
29
# File 'lib/cubicle/ratio.rb', line 27

def depends_on
  [numerator,denominator]
end

#finalize_aggregation(aggregation) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cubicle/ratio.rb', line 15

def finalize_aggregation(aggregation)
  n = aggregation[numerator].to_f
  d = aggregation[denominator].to_f

  #If the numerator is not zero, when we'll do the division
  #even if d is zero. This will result in a NaN, which indicates something
  #wrong with the data, which is fine. However, if the numerator is zero,
  #then maybe there just isn't any data, in which case NaN is pretty pessimistic -
  #we'll return 0 instead in this case.
  aggregation[name] = n != 0 ? n/d : 0
end