Class: Cubicle::Ratio
- Inherits:
-
CalculatedMeasure
- Object
- Member
- Measure
- CalculatedMeasure
- Cubicle::Ratio
- Defined in:
- lib/cubicle/ratio.rb
Instance Attribute Summary collapse
-
#denominator ⇒ Object
readonly
Returns the value of attribute denominator.
-
#numerator ⇒ Object
readonly
Returns the value of attribute numerator.
Attributes inherited from Measure
Attributes inherited from Member
#alias_list, #condition, #expression, #expression_type, #field_name, #name, #options
Instance Method Summary collapse
- #aggregate(values) ⇒ Object
- #depends_on ⇒ Object
- #finalize_aggregation(aggregation) ⇒ Object
-
#initialize(member_name, numerator, denominator, opts = {}) ⇒ Ratio
constructor
A new instance of Ratio.
Methods inherited from CalculatedMeasure
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
#denominator ⇒ Object (readonly)
Returns the value of attribute denominator.
4 5 6 |
# File 'lib/cubicle/ratio.rb', line 4 def denominator @denominator end |
#numerator ⇒ Object (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_on ⇒ Object
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 |