Class: OlapReport::Cube::Level

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dimension, name, options = {}) ⇒ Object

New level instance initialization

Parameters:

  • dimension (OlapReport::Cube::Dimension)
  • name (Symbol)
    • level name

  • options (Hash) (defaults to: {})
    • hash containing :column, :joins, :type

    :column overrides column name, that is usually taken from level name :joins specifies joined relations :type specifies level type (:hour, :day, :week, :month, :year for grouping of date/time based fields)

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
# File 'lib/olap_report/cube/level.rb', line 15

def initialize(dimension, name, options = {})
  raise ArgumentError unless dimension && name
  raise ArgumentError if options[:joins].is_a?(Hash) && options[:joins].size > 1

  @model = dimension.model
  @dimension_name = dimension.name
  @name = name
  @column_name = options[:column] || @name
  @joins, @type = options.values_at(:joins, :type)
end

Instance Attribute Details

#column_nameObject (readonly)

Returns the value of attribute column_name.



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

def column_name
  @column_name
end

#dimension_nameObject (readonly)

Returns the value of attribute dimension_name.



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

def dimension_name
  @dimension_name
end

#joinsObject (readonly)

Returns the value of attribute joins.



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

def joins
  @joins
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#build_relation(relation) ⇒ Object

Applies necessary select, joins & group_by statements to passed relation



27
28
29
30
31
# File 'lib/olap_report/cube/level.rb', line 27

def build_relation(relation)
  relation.select(column_with_alias(column, name)).
    joins(joins).
    group(@group_by || column)
end