Class: OlapReport::Cube::Measure

Inherits:
Object
  • Object
show all
Defined in:
lib/olap_report/cube/measure.rb

Defined Under Namespace

Classes: Scope, Statement

Constant Summary collapse

ALLOWED_FUNCTIONS =
[:avg, :sum, :count]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, name, function = :sum, options = {}) ⇒ Measure

Returns a new instance of Measure.

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/olap_report/cube/measure.rb', line 9

def initialize(model, name, function=:sum, options={})
  raise ArgumentError unless model && name

  if !ALLOWED_FUNCTIONS.include?(function) && !function.is_a?(Proc)
    raise OlapReport::Cube::ProhibitedFunctionError, "Function :#{function} is not allowed to use!"
  end

  @model = model
  @name, @function = name, function
  @column = options[:column] || name

  measures_scope.define_singleton_method name do
    Statement.new(column_sql)
  end
end

Instance Attribute Details

#columnObject (readonly)

Returns the value of attribute column.



3
4
5
# File 'lib/olap_report/cube/measure.rb', line 3

def column
  @column
end

#functionObject (readonly)

Returns the value of attribute function.



3
4
5
# File 'lib/olap_report/cube/measure.rb', line 3

def function
  @function
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/olap_report/cube/measure.rb', line 3

def name
  @name
end

Instance Method Details

#build_relation(relation) ⇒ Object



25
26
27
# File 'lib/olap_report/cube/measure.rb', line 25

def build_relation(relation)
  relation.select column_with_alias(column_sql, name)
end

#column_typeObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/olap_report/cube/measure.rb', line 29

def column_type
  # @TODO: does this needs refactoring?
  case function
  when :count
    :integer
  when :avg
    :float
  else
    #model.columns.find{|col| col.name.to_sym == column}.type
    nil
  end
end