Class: Mohair::Function
- Inherits:
-
Object
- Object
- Mohair::Function
- Defined in:
- lib/mohair/sql/tree.rb
Instance Method Summary collapse
-
#initialize(item) ⇒ Function
constructor
A new instance of Function.
- #is_agg? ⇒ Boolean
- #to_map_js ⇒ Object
- #to_reduce_js ⇒ Object
Constructor Details
#initialize(item) ⇒ Function
Returns a new instance of Function.
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/mohair/sql/tree.rb', line 37 def initialize item @name = item[:function].to_s @argv = [] if item[:arguments] == Array then item[:arguments].each do |i| @argv << i[:item].to_s end else @argv << item[:arguments][:item] end end |
Instance Method Details
#is_agg? ⇒ Boolean
81 82 83 |
# File 'lib/mohair/sql/tree.rb', line 81 def is_agg? true end |
#to_map_js ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/mohair/sql/tree.rb', line 49 def to_map_js s = @argv[0] case @name when 'sum' then "ret.sum_#{s} = obj.#{s};" when 'avg' then "ret.sum_#{s} = obj.#{s};\n ret.count_#{s} = 1;" when 'count' then if s == 'key' then "if(!!(v.#{s})){ ret.count_#{s} = 1; }" else "if(!!(obj.#{s})){ ret.count_#{s} = 1; }" end end end |
#to_reduce_js ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/mohair/sql/tree.rb', line 65 def to_reduce_js s = @argv[0] case @name when 'sum' then ["ret.sum_#{s} = 0;", "if(!!(v.sum_#{s})){ ret.sum_#{s} += v.sum_#{s}; }"] when 'avg' then ["ret.sum_#{s} = 0; ret.count_#{s} = 0;", "if(!!(v.sum_#{s})){ ret.sum_#{s} += v.sum_#{s};\n ret.count_#{s} += v.count_#{s}; }"] when 'count' then ["ret.count_#{s} = 0;", "if(!!(v.count_#{s})){ ret.count_#{s} += v.count_#{s}; }"] end end |