Class: ActiveReporting::ReportingDimension
- Inherits:
-
Object
- Object
- ActiveReporting::ReportingDimension
- Extended by:
- Forwardable
- Defined in:
- lib/active_reporting/reporting_dimension.rb
Constant Summary collapse
- SUPPORTED_DBS =
%w[PostgreSQL PostGIS].freeze
- DATETIME_HIERARCHIES =
Values for the Postgres ‘date_trunc` method. See www.postgresql.org/docs/10/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC
i[microseconds milliseconds second minute hour day week month quarter year decade century millennium].freeze
Class Method Summary collapse
- .build_from_dimensions(fact_model, dimensions) ⇒ Object
-
.label_config(label) ⇒ Object
If you pass a symbol it means you just indicate the field on that dimension.
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, label_name: 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, label_name: nil) ⇒ ReportingDimension
Returns a new instance of ReportingDimension.
42 43 44 45 46 47 |
# File 'lib/active_reporting/reporting_dimension.rb', line 42 def initialize(dimension, label: nil, label_name: nil) @dimension = dimension determine_label_field(label) determine_label_name(label_name) end |
Class Method Details
.build_from_dimensions(fact_model, dimensions) ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/active_reporting/reporting_dimension.rb', line 14 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] raise(UnknownDimension, "Dimension '#{dim}' not found on fact model '#{fact_model}'") if found_dimension.nil? new(found_dimension, label_config(label)) end end |
.label_config(label) ⇒ Object
If you pass a symbol it means you just indicate the field on that dimension. With a hash you can customize the name of the label
30 31 32 33 34 35 36 37 |
# File 'lib/active_reporting/reporting_dimension.rb', line 30 def self.label_config(label) return { label: label } unless label.is_a?(Hash) { label: label[:field], label_name: label[:name] } end |
Instance Method Details
#foreign_key ⇒ String
The foreign key to use in queries
52 53 54 |
# File 'lib/active_reporting/reporting_dimension.rb', line 52 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
70 71 72 73 74 75 76 |
# File 'lib/active_reporting/reporting_dimension.rb', line 70 def group_by_statement(with_identifier: true) return [degenerate_fragment] if type == Dimension::TYPES[: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
91 92 93 |
# File 'lib/active_reporting/reporting_dimension.rb', line 91 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
81 82 83 84 85 86 |
# File 'lib/active_reporting/reporting_dimension.rb', line 81 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 == Dimension::TYPES[:degenerate] "#{label_fragment} #{direction}" end |
#select_statement(with_identifier: true) ⇒ Array
Fragments of a select statement for queries that use the dimension
59 60 61 62 63 64 65 |
# File 'lib/active_reporting/reporting_dimension.rb', line 59 def select_statement(with_identifier: true) return [degenerate_select_fragment] if type == Dimension::TYPES[:degenerate] ss = ["#{label_fragment} AS #{@label_name}"] ss << "#{identifier_fragment} AS #{name}_identifier" if with_identifier ss end |