Sha256: 3f27045437303654a3b2c0398335224e019b86ebef765df59ba4a78e957235d9

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

module Prosperity
  class Aggregate::Sql < Aggregate::Base
    def initialize(sql)
      @sql = sql
    end

    def to_sql
      @sql
    end

    def apply(scope, group_by_sql: nil)
      # This is pretty hacky.. this assumes that the aggregate SQL can be
      # inserted into this query and provide a valid query. We also leave it to
      # the extractor to set this value if needed
      if group_by_sql
        s = scope.select("#{group_by_sql} AS bucket, #{to_sql}")
        s.inject({}) {|accum, el|
          attr = el.attributes.keys.select do |key|
            !%w(id bucket).include?(key)
          end.first
          accum[el["bucket"]] = el.attributes[attr].to_f
          accum
        }
      else
        s = scope.select(to_sql)

        # This isn't a group by, we should return a single value rather than a
        # scope
        if s.group_values.empty?
          raise "Unexpected size" unless s.to_a.size == 1
          record = s.first
          # Assumes that the select statement as a single value attribute
          # AR always add id, so get rid of that
          value_attr = record.attributes.keys.first { |a| a != "id" }
          record.attributes[value_attr]
        else
          s
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
prosperity-0.0.11 lib/prosperity/aggregate/sql.rb