Class: Cubicle::Measure
Direct Known Subclasses
Instance Attribute Summary collapse
-
#aggregation_method ⇒ Object
can be :sum, :average, :count.
Attributes inherited from Member
#alias_list, #condition, #expression_type, #field_name, #name, #options
Instance Method Summary collapse
- #aggregate(values) ⇒ Object
- #default_aggregation_method ⇒ Object
- #distinct_count? ⇒ Boolean
-
#expression ⇒ Object
def to_js_value return super unless aggregation_method == :count “((#super) ? 1 : 0)” end.
- #finalize_aggregation(aggregation) ⇒ Object
-
#initialize(*args) ⇒ Measure
constructor
A new instance of Measure.
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..delete(:aggregation_method) || default_aggregation_method end |
Instance Attribute Details
#aggregation_method ⇒ Object
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_method ⇒ Object
21 22 23 |
# File 'lib/cubicle/measure.rb', line 21 def default_aggregation_method :count end |
#distinct_count? ⇒ Boolean
25 26 27 |
# File 'lib/cubicle/measure.rb', line 25 def distinct_count? aggregation_method==:count && [:distinct] end |
#expression ⇒ Object
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 && [: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 |