Class: ActiveReporting::ReportingDimension
- Inherits:
-
Object
- Object
- ActiveReporting::ReportingDimension
- Extended by:
- Forwardable
- Defined in:
- lib/active_reporting/reporting_dimension.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#foreign_key ⇒ String
The foreign key to use in queries.
-
#group_by_statement(with_identifier: true) ⇒ Array
Fragments of a group by clause for queries that use the dimension.
-
#initialize(dimension, label: nil) ⇒ ReportingDimension
constructor
A new instance of ReportingDimension.
-
#label_callback ⇒ Lambda, NilClass
Looks up the dimension label callback for the label.
-
#order_by_statement(direction:) ⇒ String
Fragment of an order by clause for queries that sort by the dimension.
-
#select_statement(with_identifier: true) ⇒ Array
Fragments of a select statement for queries that use the dimension.
Constructor Details
#initialize(dimension, label: nil) ⇒ ReportingDimension
Returns a new instance of ReportingDimension.
18 19 20 21 |
# File 'lib/active_reporting/reporting_dimension.rb', line 18 def initialize(dimension, label: nil) @dimension = dimension determine_label(label) end |
Class Method Details
.build_from_dimensions(fact_model, dimensions) ⇒ Object
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/active_reporting/reporting_dimension.rb', line 7 def self.build_from_dimensions(fact_model, dimensions) Array(dimensions).map do |dim| dimension_name, label = dim.is_a?(Hash) ? Array(dim).flatten : [dim, nil] found_dimension = fact_model.dimensions[dimension_name.to_sym] if found_dimension.nil? raise UnknownDimension, "Dimension '#{dim}' not found on fact model '#{fact_model}'" end new(found_dimension, label: label) end end |
Instance Method Details
#foreign_key ⇒ String
The foreign key to use in queries
26 27 28 |
# File 'lib/active_reporting/reporting_dimension.rb', line 26 def foreign_key association ? association.foreign_key : name end |
#group_by_statement(with_identifier: true) ⇒ Array
Fragments of a group by clause for queries that use the dimension
44 45 46 47 48 49 50 |
# File 'lib/active_reporting/reporting_dimension.rb', line 44 def group_by_statement(with_identifier: true) return [degenerate_fragment] if type == :degenerate group = [label_fragment] group << identifier_fragment if with_identifier group end |
#label_callback ⇒ Lambda, NilClass
Looks up the dimension label callback for the label
65 66 67 |
# File 'lib/active_reporting/reporting_dimension.rb', line 65 def label_callback klass.fact_model.dimension_label_callbacks[@label] end |
#order_by_statement(direction:) ⇒ String
Fragment of an order by clause for queries that sort by the dimension
55 56 57 58 59 60 |
# File 'lib/active_reporting/reporting_dimension.rb', line 55 def order_by_statement(direction:) direction = direction.to_s.upcase raise "Ording direction should be 'asc' or 'desc'" unless %w(ASC DESC).include?(direction) return "#{degenerate_fragment} #{direction}" if type == :degenerate "#{label_fragment} #{direction}" end |
#select_statement(with_identifier: true) ⇒ Array
Fragments of a select statement for queries that use the dimension
33 34 35 36 37 38 39 |
# File 'lib/active_reporting/reporting_dimension.rb', line 33 def select_statement(with_identifier: true) return [degenerate_fragment] if type == :degenerate ss = ["#{label_fragment} AS #{name}"] ss << "#{identifier_fragment} AS #{name}_identifier" if with_identifier ss end |