Class: Babik::QuerySet::Aggregation

Inherits:
Object
  • Object
show all
Defined in:
lib/babik/queryset/components/aggregation.rb

Overview

A set of aggregation operations

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, functions) ⇒ Aggregation

Construct a new aggregation

Parameters:

  • model (Class)

    class that inherits from ActiveRecord::Base.

  • functions (Array<Avg, Max, Min, Sum>)

    array of aggregation functions.



15
16
17
18
19
20
21
# File 'lib/babik/queryset/components/aggregation.rb', line 15

def initialize(model, functions)
  @model = model
  @functions = []
  functions.each do |field_name, function|
    @functions << function.prepare(@model, field_name)
  end
end

Instance Attribute Details

#functionsObject (readonly)

Returns the value of attribute functions.



10
11
12
# File 'lib/babik/queryset/components/aggregation.rb', line 10

def functions
  @functions
end

#modelObject (readonly)

Returns the value of attribute model.



10
11
12
# File 'lib/babik/queryset/components/aggregation.rb', line 10

def model
  @model
end

Instance Method Details

#left_joins_by_aliasHash{alias: Babik::QuerySet::Join}

Return the joins grouped by alias

Returns:

  • (Hash{alias: Babik::QuerySet::Join})

    Hash where the value is the alias of the table and the value is a Babik::Join



25
26
27
28
29
30
31
# File 'lib/babik/queryset/components/aggregation.rb', line 25

def left_joins_by_alias
  left_joins_by_alias = {}
  @functions.each do |function|
    left_joins_by_alias.merge!(function.left_joins_by_alias)
  end
  left_joins_by_alias
end

#sqlString

Return aggregation SQL

Returns:

  • (String)

    Aggregation SQL



35
36
37
# File 'lib/babik/queryset/components/aggregation.rb', line 35

def sql
  @functions.map(&:sql).join(', ')
end