Class: Prosperity::Aggregate::Sql

Inherits:
Base
  • Object
show all
Defined in:
lib/prosperity/aggregate/sql.rb

Instance Method Summary collapse

Constructor Details

#initialize(sql) ⇒ Sql

Returns a new instance of Sql.



3
4
5
# File 'lib/prosperity/aggregate/sql.rb', line 3

def initialize(sql)
  @sql = sql
end

Instance Method Details

#apply(scope, group_by_sql: nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/prosperity/aggregate/sql.rb', line 11

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

#to_sqlObject



7
8
9
# File 'lib/prosperity/aggregate/sql.rb', line 7

def to_sql
  @sql
end