Class: ReportsKit::Reports::DimensionWithSeries
- Inherits:
-
Object
- Object
- ReportsKit::Reports::DimensionWithSeries
- Defined in:
- lib/reports_kit/reports/dimension_with_series.rb
Constant Summary collapse
- DEFAULT_GRANULARITY =
'week'
- VALID_GRANULARITIES =
%w(day week).freeze
- ADAPTER_NAMES_CLASSES =
{ 'mysql2' => Adapters::Mysql, 'postgresql' => Adapters::Postgresql }.freeze
Instance Attribute Summary collapse
-
#configuration ⇒ Object
Returns the value of attribute configuration.
-
#dimension ⇒ Object
Returns the value of attribute dimension.
-
#series ⇒ Object
Returns the value of attribute series.
Instance Method Summary collapse
- #adapter ⇒ Object
- #column_expression ⇒ Object
- #datetime_filters ⇒ Object
- #day_expression ⇒ Object
- #dimension_instances_limit ⇒ Object
- #first_key ⇒ Object
- #granularity ⇒ Object
- #group_expression ⇒ Object
- #inferred_dimension_settings ⇒ Object
- #inferred_settings ⇒ Object
- #inferred_settings_from_association ⇒ Object
-
#initialize(dimension:, series:) ⇒ DimensionWithSeries
constructor
A new instance of DimensionWithSeries.
- #joins ⇒ Object
- #key_to_label(key) ⇒ Object
- #last_key ⇒ Object
- #settings ⇒ Object
- #should_be_sorted_by_count? ⇒ Boolean
- #week_expression ⇒ Object
Constructor Details
#initialize(dimension:, series:) ⇒ DimensionWithSeries
Returns a new instance of DimensionWithSeries.
18 19 20 21 22 23 24 |
# File 'lib/reports_kit/reports/dimension_with_series.rb', line 18 def initialize(dimension:, series:) self.dimension = dimension self.series = series self.configuration = InferrableConfiguration.new(self, :dimensions) missing_group_setting = settings && !settings.key?(:group) raise ArgumentError.new("Dimension settings for dimension '#{key}' of #{model_class} must include :group") if missing_group_setting end |
Instance Attribute Details
#configuration ⇒ Object
Returns the value of attribute configuration.
11 12 13 |
# File 'lib/reports_kit/reports/dimension_with_series.rb', line 11 def configuration @configuration end |
#dimension ⇒ Object
Returns the value of attribute dimension.
11 12 13 |
# File 'lib/reports_kit/reports/dimension_with_series.rb', line 11 def dimension @dimension end |
#series ⇒ Object
Returns the value of attribute series.
11 12 13 |
# File 'lib/reports_kit/reports/dimension_with_series.rb', line 11 def series @series end |
Instance Method Details
#adapter ⇒ Object
114 115 116 117 118 119 120 121 |
# File 'lib/reports_kit/reports/dimension_with_series.rb', line 114 def adapter @adapter ||= begin adapter_name = model_class.connection_config[:adapter] adapter = ADAPTER_NAMES_CLASSES[adapter_name] raise ArgumentError.new("Unsupported adapter: #{adapter_name}") unless adapter adapter end end |
#column_expression ⇒ Object
123 124 125 |
# File 'lib/reports_kit/reports/dimension_with_series.rb', line 123 def column_expression "#{model_class.table_name}.#{key}" end |
#datetime_filters ⇒ Object
105 106 107 108 |
# File 'lib/reports_kit/reports/dimension_with_series.rb', line 105 def datetime_filters return [] unless series.filters.present? series.filters.map(&:filter_type).select { |filter_type| filter_type.is_a?(FilterTypes::Datetime) } end |
#day_expression ⇒ Object
127 128 129 |
# File 'lib/reports_kit/reports/dimension_with_series.rb', line 127 def day_expression adapter.truncate_to_day(column_expression) end |
#dimension_instances_limit ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/reports_kit/reports/dimension_with_series.rb', line 82 def dimension_instances_limit if configured_by_time? properties[:limit] else properties[:limit] || ReportsKit.configuration.default_dimension_limit end end |
#first_key ⇒ Object
90 91 92 93 |
# File 'lib/reports_kit/reports/dimension_with_series.rb', line 90 def first_key return unless configured_by_time? && datetime_filters.present? datetime_filters.map(&:start_at).compact.sort.first end |
#granularity ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/reports_kit/reports/dimension_with_series.rb', line 26 def granularity @granularity ||= begin return unless configured_by_time? granularity = properties[:granularity] || DEFAULT_GRANULARITY raise ArgumentError.new("Invalid granularity: #{granularity}") unless VALID_GRANULARITIES.include?(granularity) granularity end end |
#group_expression ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/reports_kit/reports/dimension_with_series.rb', line 49 def group_expression if configured_by_model? settings_from_model[:group] elsif configured_by_association? inferred_settings_from_association[:column] elsif configured_by_column? && configured_by_time? granularity == 'day' ? day_expression : week_expression elsif configured_by_column? column_expression else raise ArgumentError.new('Invalid group_expression') end end |
#inferred_dimension_settings ⇒ Object
43 44 45 46 47 |
# File 'lib/reports_kit/reports/dimension_with_series.rb', line 43 def inferred_dimension_settings { group: group_expression } end |
#inferred_settings ⇒ Object
39 40 41 |
# File 'lib/reports_kit/reports/dimension_with_series.rb', line 39 def inferred_settings configuration.inferred_settings.merge(inferred_dimension_settings) end |
#inferred_settings_from_association ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/reports_kit/reports/dimension_with_series.rb', line 63 def inferred_settings_from_association through_reflection = reflection.through_reflection if through_reflection { joins: through_reflection.name, column: "#{through_reflection.table_name}.#{reflection.source_reflection.foreign_key}" } else { column: "#{model_class.table_name}.#{reflection.foreign_key}" } end end |
#joins ⇒ Object
77 78 79 80 |
# File 'lib/reports_kit/reports/dimension_with_series.rb', line 77 def joins return settings_from_model[:joins] if configured_by_model? inferred_settings_from_association[:joins] if configured_by_association? end |
#key_to_label(key) ⇒ Object
100 101 102 103 |
# File 'lib/reports_kit/reports/dimension_with_series.rb', line 100 def key_to_label(key) return unless settings[:key_to_label] settings[:key_to_label].call(key) end |
#last_key ⇒ Object
95 96 97 98 |
# File 'lib/reports_kit/reports/dimension_with_series.rb', line 95 def last_key return unless configured_by_time? && datetime_filters.present? datetime_filters.map(&:end_at).compact.sort.last end |
#settings ⇒ Object
35 36 37 |
# File 'lib/reports_kit/reports/dimension_with_series.rb', line 35 def settings inferred_settings.merge(settings_from_model) end |
#should_be_sorted_by_count? ⇒ Boolean
110 111 112 |
# File 'lib/reports_kit/reports/dimension_with_series.rb', line 110 def should_be_sorted_by_count? !configured_by_time? end |
#week_expression ⇒ Object
131 132 133 |
# File 'lib/reports_kit/reports/dimension_with_series.rb', line 131 def week_expression adapter.truncate_to_week(column_expression) end |