Class: ReportsKit::Reports::Series
- Inherits:
-
AbstractSeries
- Object
- AbstractSeries
- ReportsKit::Reports::Series
- Defined in:
- lib/reports_kit/reports/series.rb
Constant Summary collapse
- VALID_KEYS =
[:measure, :dimensions, :filters, :limit, :report_options]
Instance Attribute Summary collapse
-
#context_record ⇒ Object
Returns the value of attribute context_record.
-
#dimensions ⇒ Object
Returns the value of attribute dimensions.
-
#filters ⇒ Object
Returns the value of attribute filters.
-
#properties ⇒ Object
Returns the value of attribute properties.
Class Method Summary collapse
Instance Method Summary collapse
- #aggregate_function ⇒ Object
- #aggregation_config ⇒ Object
- #aggregation_expression ⇒ Object
- #aggregation_key ⇒ Object
- #base_relation ⇒ Object
- #edit_relation_method ⇒ Object
- #filtered_relation ⇒ Object
- #has_two_dimensions? ⇒ Boolean
-
#initialize(properties, context_record: nil) ⇒ Series
constructor
A new instance of Series.
- #key ⇒ Object
- #label ⇒ Object
- #limit ⇒ Object
- #model_class ⇒ Object
- #relation_name ⇒ Object
Methods inherited from AbstractSeries
Constructor Details
#initialize(properties, context_record: nil) ⇒ Series
Returns a new instance of Series.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/reports_kit/reports/series.rb', line 8 def initialize(properties, context_record: nil) properties = { measure: properties } if properties.is_a?(String) properties = properties.deep_symbolize_keys.dup measure_properties = properties[:measure] properties[:measure] = measure_properties properties[:measure] = { key: properties[:measure] } if properties[:measure].is_a?(String) raise ArgumentError.new("Measure properties must be a String or Hash, not a #{properties.class.name}: #{properties.inspect}") unless properties.is_a?(Hash) dimension_hashes = properties[:dimensions] || [] dimension_hashes = dimension_hashes.values if dimension_hashes.is_a?(Hash) && dimension_hashes.key?(:'0') filter_hashes = properties[:filters] || [] filter_hashes = filter_hashes.values if filter_hashes.is_a?(Hash) && filter_hashes.key?(:'0') self.properties = properties self.dimensions = dimension_hashes.map { |dimension_hash| DimensionWithSeries.new(dimension: Dimension.new(dimension_hash), series: self) } self.filters = filter_hashes.map { |filter_hash| FilterWithSeries.new(filter: Filter.new(filter_hash), series: self) } self.context_record = context_record end |
Instance Attribute Details
#context_record ⇒ Object
Returns the value of attribute context_record.
6 7 8 |
# File 'lib/reports_kit/reports/series.rb', line 6 def context_record @context_record end |
#dimensions ⇒ Object
Returns the value of attribute dimensions.
6 7 8 |
# File 'lib/reports_kit/reports/series.rb', line 6 def dimensions @dimensions end |
#filters ⇒ Object
Returns the value of attribute filters.
6 7 8 |
# File 'lib/reports_kit/reports/series.rb', line 6 def filters @filters end |
#properties ⇒ Object
Returns the value of attribute properties.
6 7 8 |
# File 'lib/reports_kit/reports/series.rb', line 6 def properties @properties end |
Class Method Details
.new_from_properties!(properties, context_record:) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/reports_kit/reports/series.rb', line 96 def self.new_from_properties!(properties, context_record:) series_hashes = properties[:series].presence || properties.slice(*Series::VALID_KEYS) series_hashes = [series_hashes] if series_hashes.is_a?(Hash) raise ArgumentError.new('At least one series must be configured') if series_hashes.blank? series_hashes.map do |series_hash| if series_hash[:composite_operator].present? CompositeSeries.new(series_hash) else new(series_hash, context_record: context_record) end end end |
Instance Method Details
#aggregate_function ⇒ Object
47 48 49 |
# File 'lib/reports_kit/reports/series.rb', line 47 def aggregate_function aggregation_expression || :count end |
#aggregation_config ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/reports_kit/reports/series.rb', line 65 def aggregation_config @aggregation_config ||= begin return unless aggregation_key raise ArgumentError.new("A '#{aggregation_key}' aggregation on the #{model_class} model hasn't been configured") unless model_class.respond_to?(:reports_kit_configuration) config = model_class.reports_kit_configuration.aggregations.find { |aggregation| aggregation[:key] == aggregation_key } raise ArgumentError.new("A '#{aggregation_key}' aggregation on the #{model_class} model hasn't been configured") unless config config end end |
#aggregation_expression ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/reports_kit/reports/series.rb', line 51 def aggregation_expression return unless aggregation_config expression = aggregation_config[:expression] if expression.is_a?(Array) expression else raise ArgumentError.new("The '#{aggregation_key}' aggregation on the #{model_class} model isn't valid") end end |
#aggregation_key ⇒ Object
61 62 63 |
# File 'lib/reports_kit/reports/series.rb', line 61 def aggregation_key properties[:measure][:aggregation] end |
#base_relation ⇒ Object
75 76 77 78 |
# File 'lib/reports_kit/reports/series.rb', line 75 def base_relation return context_record.public_send(relation_name) if context_record model_class end |
#edit_relation_method ⇒ Object
39 40 41 |
# File 'lib/reports_kit/reports/series.rb', line 39 def edit_relation_method ReportsKit.configuration.custom_method(properties[:report_options].try(:[], :edit_relation_method)) end |
#filtered_relation ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/reports_kit/reports/series.rb', line 84 def filtered_relation relation = base_relation filters.each do |filter| relation = filter.apply(relation) end relation end |
#has_two_dimensions? ⇒ Boolean
92 93 94 |
# File 'lib/reports_kit/reports/series.rb', line 92 def has_two_dimensions? dimensions.length == 2 end |
#key ⇒ Object
27 28 29 |
# File 'lib/reports_kit/reports/series.rb', line 27 def key properties[:measure][:key].underscore end |
#label ⇒ Object
31 32 33 |
# File 'lib/reports_kit/reports/series.rb', line 31 def label properties[:measure][:name].presence || key.pluralize.titleize end |
#limit ⇒ Object
35 36 37 |
# File 'lib/reports_kit/reports/series.rb', line 35 def limit properties[:limit] end |
#model_class ⇒ Object
80 81 82 |
# File 'lib/reports_kit/reports/series.rb', line 80 def model_class key.camelize.constantize end |
#relation_name ⇒ Object
43 44 45 |
# File 'lib/reports_kit/reports/series.rb', line 43 def relation_name key.tableize end |