Class: OlapReport::Cube::Dimension

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, name) ⇒ Dimension

Returns a new instance of Dimension.

Raises:

  • (ArgumentError)


5
6
7
8
9
10
# File 'lib/olap_report/cube/dimension.rb', line 5

def initialize(model, name)
  raise ArgumentError unless model && name
  @levels = []
  @name = name
  @model = model
end

Instance Attribute Details

#levelsObject (readonly)

Returns the value of attribute levels.



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

def levels
  @levels
end

#modelObject (readonly)

Returns the value of attribute model.



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

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#[](level_name) ⇒ OlapReport::Cube::Level

Finds level by name from dimension

Parameters:

  • level_name (Symbol)

Returns:



34
35
36
# File 'lib/olap_report/cube/dimension.rb', line 34

def [](level_name)
  levels.find{|l| l.name == level_name} || raise(KeyError, "Level '#{level_name}' not found for dimension '#{self.name}'")
end

#dates(field, options = {}) ⇒ Object

Defines date-based levels for dimension

Parameters:

  • field (Symbol)
    • column name for date levels

  • options (Hash) (defaults to: {})
    • date periods for levels



22
23
24
25
26
27
28
29
# File 'lib/olap_report/cube/dimension.rb', line 22

def dates(field, options = {})
  levels = options[:by]
  if levels.present?
    levels.each{ |name| level name, column: field, type: name }
  else
    level field
  end
end

#level(name, options = {}) ⇒ Object

Defines level for dimension

Parameters:

  • name (Symbol)
    • level name (in general column name from table)

  • options (Hash) (defaults to: {})
    • options hash



15
16
17
# File 'lib/olap_report/cube/dimension.rb', line 15

def level(name, options = {})
  @levels << Cube::Level.new(self, name, options)
end

#level_index(level_name) ⇒ Object



38
39
40
# File 'lib/olap_report/cube/dimension.rb', line 38

def level_index(level_name)
  levels.map(&:name).index(level_name)
end

#next_level(level_name) ⇒ Object



46
47
48
# File 'lib/olap_report/cube/dimension.rb', line 46

def next_level(level_name)
  levels[level_index(level_name) + 1]
end

#prev_level(level_name) ⇒ Object



42
43
44
# File 'lib/olap_report/cube/dimension.rb', line 42

def prev_level(level_name)
  levels[level_index(level_name) - 1]
end