Sha256: e88dd529b886309fdc28b6c2718ed5738d03228e80c1451907164215082e5f6e

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

module Aggrobot
  module SQLFunctions
    module Common
      delegate :sanitize, to: ActiveRecord::Base
      mattr_accessor :precision

      def desc(attr)
        "#{attr} desc"
      end

      def asc(attr)
        "#{attr} asc"
      end

      def count(attr = '*')
        "COUNT(#{attr})"
      end

      def unique_count(attr = '*')
        "COUNT(DISTINCT #{attr})"
      end

      def max(attr)
        "MAX(#{attr})"
      end

      def min(attr)
        "MIN(#{attr})"
      end

      def sum(attr = count)
        "SUM(#{attr})"
      end

      # returns ROUNDED average of attr, with precision(ROUNDING DIGITS)
      def avg(attr, rounding = self.precision)
        "ROUND(AVG(#{attr}), #{rounding})"
      end

      alias average avg

      # GROUP_CONCAT: A SQL function which returns a concatenated string
      # group_collect returns concatenated string of distinct attr
      def group_collect(attr)
        "GROUP_CONCAT(DISTINCT #{attr})"
      end

      # returns percentage based on ROUND SQL function, with precision(ROUNDING DIGITS)
      def percent(total, attr = count, rounding = self.precision)
        total == 0 ? "0" : "ROUND((#{attr}*100.0)/#{total}, #{rounding})"
      end

      # returns ROUND of multipliers, with precision(self.precision)
      def multiply(attr, multiplier, rounding = self.precision)
        "ROUND(#{attr}*#{multiplier}, #{rounding})"
      end

      # returns ROUND of attr/divider, with precision(self.precision)
      def divide(attr, divider, rounding = self.precision)
        "ROUND(#{attr}/#{divider}, #{rounding})"
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
aggrobot-0.1.0 lib/aggrobot/sql_functions/common.rb