Class: Pivotable::Rotation

Inherits:
Object
  • Object
show all
Defined in:
lib/pivotable/rotation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, name, parent = nil, &block) ⇒ Rotation

Returns a new instance of Rotation.



6
7
8
9
10
11
12
13
14
15
# File 'lib/pivotable/rotation.rb', line 6

def initialize(model, name, parent = nil, &block)
  @model   = model
  @name    = name
  @parent  = parent
  @selects = []
  @groups  = []
  @joins   = []
  @block   = block
  @loaded  = false
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



3
4
5
# File 'lib/pivotable/rotation.rb', line 3

def block
  @block
end

#groupsObject (readonly)

Returns the value of attribute groups.



3
4
5
# File 'lib/pivotable/rotation.rb', line 3

def groups
  @groups
end

#joins(*args) ⇒ Object (readonly)

Returns the value of attribute joins.



3
4
5
# File 'lib/pivotable/rotation.rb', line 3

def joins
  @joins
end

#loadedObject (readonly) Also known as: loaded?

Returns the value of attribute loaded.



3
4
5
# File 'lib/pivotable/rotation.rb', line 3

def loaded
  @loaded
end

#modelObject (readonly)

Returns the value of attribute model.



3
4
5
# File 'lib/pivotable/rotation.rb', line 3

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/pivotable/rotation.rb', line 3

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



3
4
5
# File 'lib/pivotable/rotation.rb', line 3

def parent
  @parent
end

#selectsObject (readonly)

Returns the value of attribute selects.



3
4
5
# File 'lib/pivotable/rotation.rb', line 3

def selects
  @selects
end

Instance Method Details

#average(*cols) ⇒ Object

Calculate sum. Calls #calculate with :via => :average. See #calulate for examples.



40
41
42
43
# File 'lib/pivotable/rotation.rb', line 40

def average(*cols)
  opts = cols.extract_options!.update :function => :average
  calculate *(cols << opts)
end

#by(*cols) ⇒ Object

Group by a column. Examples:

# Simple function
by :page_id
# => SELECT table.page_id FROM table GROUP BY page_id

# Use a special column
by :pageid, :via => page_id
# => SELECT table.page_id AS pageid FROM table GROUP BY page_id

# Use a custom SQL
by :page_id, :via => "page_id * 2"
# => SELECT page_id * 2 AS page_id FROM table GROUP BY page_id * 2

# Use an AREL attributes
by :pageid, :via => Model.arel_table[:page_id]
# => SELECT table.page_id AS pageid FROM table GROUP BY page_id


88
89
90
91
92
93
94
95
# File 'lib/pivotable/rotation.rb', line 88

def by(*cols)
  opts = cols.extract_options!
  cols.each do |col|
    expr = Pivotable::Expression::Generic.new(model, col, opts)
    @selects << expr
    @groups  << expr
  end
end

#calculate(*cols) ⇒ Object

Calculate value. Examples:

# Simple function
calculate :views, :function => :sum
# => SELECT SUM(table.views) AS views FROM table

# Use a special column
calculate :page_views, :via => :views, :function => :sum
# => SELECT SUM(table.views) AS page_views FROM table

# Use a custom SQL
calculate :page_views, :via => "SUM(table.views)"
# => SELECT SUM(table.views) AS page_views FROM table

# Use an AREL expressions
calculate :page_views, :via => Model.arel_table[:views].sum
# => SELECT SUM(table.views) AS page_views FROM table


63
64
65
66
67
68
# File 'lib/pivotable/rotation.rb', line 63

def calculate(*cols)
  opts = cols.extract_options!
  cols.each do |col|
    @selects << Pivotable::Expression::Calculation.new(model, col, opts)
  end
end

#load!Object



119
120
121
122
123
124
125
# File 'lib/pivotable/rotation.rb', line 119

def load!
  return if loaded?

  instance_eval &model.pivotable(parent).block if parent
  instance_eval &block
  @loaded = true
end

#maximum(*cols) ⇒ Object

Calculate maximum. Calls #calculate with :via => :maximum. See #calulate for examples.



26
27
28
29
# File 'lib/pivotable/rotation.rb', line 26

def maximum(*cols)
  opts = cols.extract_options!.update :function => :maximum
  calculate *(cols << opts)
end

#merge(relation) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/pivotable/rotation.rb', line 101

def merge(relation)
  load! unless loaded?

  selects.each do |column|
    relation = relation.select(column.to_select)
  end

  groups.each do |column|
    relation = relation.group(column.to_group)
  end

  joins.each do |join|
    relation = relation.joins(join)
  end

  relation
end

#minimum(*cols) ⇒ Object

Calculate minimum. Calls #calculate with :via => :minimum. See #calulate for examples.



19
20
21
22
# File 'lib/pivotable/rotation.rb', line 19

def minimum(*cols)
  opts = cols.extract_options!.update :function => :minimum
  calculate *(cols << opts)
end

#sum(*cols) ⇒ Object

Calculate sum. Calls #calculate with :via => :sum. See #calulate for examples.



33
34
35
36
# File 'lib/pivotable/rotation.rb', line 33

def sum(*cols)
  opts = cols.extract_options!.update :function => :sum
  calculate *(cols << opts)
end