Module: CalculateAll::Helpers

Defined in:
lib/calculate-all.rb

Class Method Summary collapse

Class Method Details

.decode_function_aliases(aliases) ⇒ Object


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/calculate-all.rb', line 52

def decode_function_aliases(aliases)
  aliases.map do |key|
    function =
      case key
      when String
        key
      when :count
        "COUNT(*)"
      when /^(.*)_distinct_count$/, /^count_distinct_(.*)$/
        "COUNT(DISTINCT #{$1})"
      when /^(.*)_(count|sum|max|min|avg)$/
        "#{$2.upcase}(#{$1})"
      when /^(count|sum|max|min|avg)_(.*)$$/
        "#{$1.upcase}(#{$2})"
      when /^(.*)_average$/, /^average_(.*)$/
        "AVG(#{$1})"
      when /^(.*)_maximum$/, /^maximum_(.*)$/
        "MAX(#{$1})"
      when /^(.*)_minimum$/, /^minimum_(.*)$/
        "MIN(#{$1})"
      else
        raise ArgumentError, "Can't recognize function alias #{key}"
      end
    [key, function]
  end.to_h
end