Class: Pivotable::Expression::Calculation

Inherits:
Abstract
  • Object
show all
Defined in:
lib/pivotable/expression/calculation.rb

Instance Attribute Summary collapse

Attributes inherited from Abstract

#model, #name, #via

Instance Method Summary collapse

Constructor Details

#initialize(model, name, options = {}) ⇒ Calculation

Returns a new instance of Calculation.



5
6
7
8
9
10
11
12
13
# File 'lib/pivotable/expression/calculation.rb', line 5

def initialize(model, name, options = {})
  super
  @function = options[:function] if options[:function].is_a?(Symbol)

  if via.is_a?(Arel::Attribute)
    raise ArgumentError, "No calculation function provided for '#{@name}'. Please provide a :function option" unless function
    @via = via.send(function)
  end
end

Instance Attribute Details

#functionObject (readonly)

Returns the value of attribute function.



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

def function
  @function
end

Instance Method Details

#to_groupObject

Raises:

  • (RuntimeError)


24
25
26
# File 'lib/pivotable/expression/calculation.rb', line 24

def to_group
  raise RuntimeError, "Cannot group by calculation"
end

#to_selectObject



15
16
17
18
19
20
21
22
# File 'lib/pivotable/expression/calculation.rb', line 15

def to_select
  case via
  when String
    "#{via} AS #{name}"
  else
    via.clone.as(name)
  end
end