Class: ActiveReporting::Dimension

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

Constant Summary collapse

TYPES =
{ degenerate: :degenerate, standard: :standard }.freeze

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)


10
11
12
13
# File 'lib/active_reporting/dimension.rb', line 10

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.



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

def name
  @name
end

Instance Method Details

#associationActiveRecord::Reflection

Returns the reflected association of the fact model to dimension name

Returns:

  • (ActiveRecord::Reflection)


58
59
60
# File 'lib/active_reporting/dimension.rb', line 58

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

#hierarchical?Boolean

Tells if the dimension is hierarchical

Returns:

  • (Boolean)


36
37
38
# File 'lib/active_reporting/dimension.rb', line 36

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)


44
45
46
# File 'lib/active_reporting/dimension.rb', line 44

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

#modelActiveRecord::Base

Returns the fact model’s dimension

Returns:

  • (ActiveRecord::Base)


51
52
53
# File 'lib/active_reporting/dimension.rb', line 51

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)


23
24
25
26
27
28
29
30
31
# File 'lib/active_reporting/dimension.rb', line 23

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