Class: Repor::Report
- Inherits:
-
Object
- Object
- Repor::Report
- Defined in:
- lib/repor/report.rb
Instance Attribute Summary collapse
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Class Method Summary collapse
- .aggregator(name, aggregator_class, opts = {}) ⇒ Object
- .aggregators ⇒ Object
-
.autoreport_association_name_columns(reflection) ⇒ Object
override this to change which columns of the association are used to auto-label it.
-
.autoreport_column(column) ⇒ Object
can override this method to skip or change certain column declarations.
-
.autoreport_on(class_or_name) ⇒ Object
autoreporting will automatically define dimensions based on columns.
- .default_class ⇒ Object
- .dimension(name, dimension_class, opts = {}) ⇒ Object
- .dimensions ⇒ Object
-
.inherited(subclass) ⇒ Object
ensure subclasses gain any aggregators or dimensions defined on their parents.
- .klass ⇒ Object
- .report_on(class_or_name) ⇒ Object
Instance Method Summary collapse
- #aggregator ⇒ Object
- #aggregator_name ⇒ Object
- #aggregators ⇒ Object
- #base_relation ⇒ Object
- #data ⇒ Object
- #dimensions ⇒ Object
- #filters ⇒ Object
-
#flat_data ⇒ Object
flat hash of { [x1, x2, x3] => y }.
- #group_values ⇒ Object
- #grouper_names ⇒ Object
- #groupers ⇒ Object
- #groups ⇒ Object
-
#initialize(params = {}) ⇒ Report
constructor
A new instance of Report.
-
#nested_data ⇒ Object
nested array of [{ key: x3, values: [{ key: x2, values: [{ key: x1, value: y }] }] }].
- #raw_data ⇒ Object
- #records ⇒ Object
- #relation ⇒ Object
- #relators ⇒ Object
- #table_name ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ Report
Returns a new instance of Report.
7 8 9 10 |
# File 'lib/repor/report.rb', line 7 def initialize(params = {}) @params = params.deep_symbolize_keys validate_params! end |
Instance Attribute Details
#params ⇒ Object (readonly)
Returns the value of attribute params.
5 6 7 |
# File 'lib/repor/report.rb', line 5 def params @params end |
Class Method Details
.aggregator(name, aggregator_class, opts = {}) ⇒ Object
109 110 111 |
# File 'lib/repor/report.rb', line 109 def aggregator(name, aggregator_class, opts = {}) aggregators[name.to_sym] = { axis_class: aggregator_class, opts: opts } end |
.aggregators ⇒ Object
105 106 107 |
# File 'lib/repor/report.rb', line 105 def aggregators @aggregators ||= {} end |
.autoreport_association_name_columns(reflection) ⇒ Object
override this to change which columns of the association are used to auto-label it
181 182 183 |
# File 'lib/repor/report.rb', line 181 def autoreport_association_name_columns(reflection) %w(name email title) end |
.autoreport_column(column) ⇒ Object
can override this method to skip or change certain column declarations
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/repor/report.rb', line 158 def autoreport_column(column) return if column.name == 'id' belongs_to_ref = klass.reflections.find { |_, a| a.foreign_key == column.name } if belongs_to_ref name, ref = belongs_to_ref name_col = (ref.klass.column_names & autoreport_association_name_columns(ref)).first if name_col name_expr = "#{ref.klass.table_name}.#{name_col}" category_dimension name, expression: name_expr, relation: ->(r) { r.joins(name) } else category_dimension column.name end elsif column.cast_type.type == :datetime time_dimension column.name elsif column.cast_type.number? number_dimension column.name else category_dimension column.name end end |
.autoreport_on(class_or_name) ⇒ Object
autoreporting will automatically define dimensions based on columns
151 152 153 154 155 |
# File 'lib/repor/report.rb', line 151 def autoreport_on(class_or_name) report_on class_or_name klass.columns.each(&method(:autoreport_column)) count_aggregator :count if aggregators.blank? end |
.default_class ⇒ Object
129 130 131 |
# File 'lib/repor/report.rb', line 129 def default_class self.name.demodulize.sub(/Report$/, '').constantize end |
.dimension(name, dimension_class, opts = {}) ⇒ Object
101 102 103 |
# File 'lib/repor/report.rb', line 101 def dimension(name, dimension_class, opts = {}) dimensions[name.to_sym] = { axis_class: dimension_class, opts: opts } end |
.dimensions ⇒ Object
97 98 99 |
# File 'lib/repor/report.rb', line 97 def dimensions @dimensions ||= {} end |
.inherited(subclass) ⇒ Object
ensure subclasses gain any aggregators or dimensions defined on their parents
144 145 146 147 148 |
# File 'lib/repor/report.rb', line 144 def inherited(subclass) instance_values.each do |ivar, ival| subclass.instance_variable_set(:"@#{ivar}", ival.dup) end end |
.klass ⇒ Object
133 134 135 136 137 |
# File 'lib/repor/report.rb', line 133 def klass @klass ||= default_class rescue NameError raise NameError, "must specify a class to report on, e.g. `report_on Post`" end |
.report_on(class_or_name) ⇒ Object
139 140 141 |
# File 'lib/repor/report.rb', line 139 def report_on(class_or_name) @klass = class_or_name.to_s.constantize end |
Instance Method Details
#aggregator ⇒ Object
24 25 26 |
# File 'lib/repor/report.rb', line 24 def aggregator @aggregator ||= aggregators[aggregator_name] end |
#aggregator_name ⇒ Object
20 21 22 |
# File 'lib/repor/report.rb', line 20 def aggregator_name params.fetch(:aggregator, default_aggregator_name).to_sym end |
#aggregators ⇒ Object
16 17 18 |
# File 'lib/repor/report.rb', line 16 def aggregators @aggregators ||= build_axes(self.class.aggregators) end |
#base_relation ⇒ Object
46 47 48 |
# File 'lib/repor/report.rb', line 46 def base_relation params.fetch(:relation, klass.all) end |
#data ⇒ Object
92 93 94 |
# File 'lib/repor/report.rb', line 92 def data nested_data end |
#dimensions ⇒ Object
12 13 14 |
# File 'lib/repor/report.rb', line 12 def dimensions @dimensions ||= build_axes(self.class.dimensions) end |
#filters ⇒ Object
38 39 40 |
# File 'lib/repor/report.rb', line 38 def filters @filters ||= dimensions.values.select(&:filtering?) end |
#flat_data ⇒ Object
flat hash of { [x1, x2, x3] => y }
82 83 84 |
# File 'lib/repor/report.rb', line 82 def flat_data @flat_data ||= Hash[group_values.map { |x| [x, raw_data[x]] }] end |
#group_values ⇒ Object
76 77 78 |
# File 'lib/repor/report.rb', line 76 def group_values @group_values ||= all_combinations_of(groupers.map(&:group_values)) end |
#grouper_names ⇒ Object
28 29 30 31 32 |
# File 'lib/repor/report.rb', line 28 def grouper_names names = params.fetch(:groupers, default_grouper_names) names = names.is_a?(Hash) ? names.values : Array.wrap(names) names.map(&:to_sym) end |
#groupers ⇒ Object
34 35 36 |
# File 'lib/repor/report.rb', line 34 def groupers @groupers ||= dimensions.values_at(*grouper_names) end |
#groups ⇒ Object
66 67 68 69 70 |
# File 'lib/repor/report.rb', line 66 def groups @groups ||= groupers.reduce(records) do |relation, dimension| dimension.group(relation) end end |
#nested_data ⇒ Object
nested array of
- { key: x3, values: [{ key: x2, values: [{ key: x1, value: y }
-
}] }]
88 89 90 |
# File 'lib/repor/report.rb', line 88 def nested_data @nested_data ||= nest_data end |
#raw_data ⇒ Object
72 73 74 |
# File 'lib/repor/report.rb', line 72 def raw_data @raw_data ||= aggregator.aggregate(groups) end |
#records ⇒ Object
60 61 62 63 64 |
# File 'lib/repor/report.rb', line 60 def records @records ||= filters.reduce(relation) do |relation, dimension| dimension.filter(relation) end end |
#relation ⇒ Object
54 55 56 57 58 |
# File 'lib/repor/report.rb', line 54 def relation @relation ||= relators.reduce(base_relation) do |relation, dimension| dimension.relate(relation) end end |
#relators ⇒ Object
42 43 44 |
# File 'lib/repor/report.rb', line 42 def relators filters | groupers end |
#table_name ⇒ Object
50 51 52 |
# File 'lib/repor/report.rb', line 50 def table_name klass.table_name end |