Class: ActiveReporting::Dimension

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fact_model, name:) ⇒ Dimension

Returns a new instance of Dimension.

Parameters:

  • model (ActiveRecord::Base)
  • name (Symbol)


7
8
9
10
# File 'lib/active_reporting/dimension.rb', line 7

def initialize(fact_model, name:)
  @fact_model = fact_model
  @name       = name.to_s
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#associationActiveRecord::Reflection

Returns the reflected association of the fact model to dimension name

Returns:

  • (ActiveRecord::Reflection)


55
56
57
# File 'lib/active_reporting/dimension.rb', line 55

def association
  @association_info ||= model.reflect_on_association(@name)
end

#hierarchical?Boolean

Tells if the dimension is hierarchical

Returns:

  • (Boolean)


33
34
35
# File 'lib/active_reporting/dimension.rb', line 33

def hierarchical?
  @hierarchical ||= !klass.fact_model.hierarchical_levels.empty?
end

#klassBoolean

Returns either the model of the dimension’s association or the model itself if the dimension lives on the fact model

Returns:

  • (Boolean)


41
42
43
# File 'lib/active_reporting/dimension.rb', line 41

def klass
  @klass ||= association ? association.klass : model
end

#modelActiveRecord::Base

Returns the fact model’s dimension

Returns:

  • (ActiveRecord::Base)


48
49
50
# File 'lib/active_reporting/dimension.rb', line 48

def model
  @model ||= @fact_model.model
end

#typeSymbol

Determins the type of the dimension

A dimension type is either:

  • standard - The dimension is a relation to the fact model’s model

  • degenerate - The dimension is the model’s attribute

Returns:

  • (Symbol)


20
21
22
23
24
25
26
27
28
# File 'lib/active_reporting/dimension.rb', line 20

def type
  @type ||= if model.column_names.include?(@name)
              :degenerate
            elsif association
              :standard
            else
              raise UnknownDimension, "Dimension '#{@name}' not found on fact model '#{@fact_model}'"
            end
end