Class: ActiveReporter::Dimension::Bin
- Inherits:
-
Base
- Object
- Base
- ActiveReporter::Dimension::Bin
show all
- Defined in:
- lib/active_reporter/dimension/bin.rb,
lib/active_reporter/dimension/bin/set.rb,
lib/active_reporter/dimension/bin/table.rb
Defined Under Namespace
Classes: Set, Table
Constant Summary
collapse
- MAX_BINS =
2_000
Instance Attribute Summary
Attributes inherited from Base
#name, #options, #report
Instance Method Summary
collapse
Methods inherited from Base
#attribute, #expression, #extract_sql_value, #filtering?, #grouping?, #initialize, #model, #null_order, #nulls_last?, #order, #order_expression, #params, #relate, #sort_desc?, #sort_order
Instance Method Details
#bin_end ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/active_reporter/dimension/bin.rb', line 25
def bin_end
@bin_end ||= if max.blank? || min.blank? || min > max
nil
else
bin_edge = bin_start + bin_width
loop do
break if bin_edge >= max
bin_edge += bin_width
end
bin_edge += bin_width unless filter_values_for(:max).present?
bin_edge
end
end
|
#domain ⇒ Object
51
52
53
|
# File 'lib/active_reporter/dimension/bin.rb', line 51
def domain
min.nil? || max.nil? ? 0 : (max - min)
end
|
#filter(relation) ⇒ Object
63
64
65
|
# File 'lib/active_reporter/dimension/bin.rb', line 63
def filter(relation)
filter_values.filter(relation, expression)
end
|
#filter_max ⇒ Object
47
48
49
|
# File 'lib/active_reporter/dimension/bin.rb', line 47
def filter_max
filter_values_for(:max).max
end
|
#filter_min ⇒ Object
43
44
45
|
# File 'lib/active_reporter/dimension/bin.rb', line 43
def filter_min
filter_values_for(:min).min
end
|
#filter_values ⇒ Object
59
60
61
|
# File 'lib/active_reporter/dimension/bin.rb', line 59
def filter_values
@filter_values ||= to_bins(super)
end
|
#group(relation) ⇒ Object
67
68
69
|
# File 'lib/active_reporter/dimension/bin.rb', line 67
def group(relation)
group_values.group(relation, expression, sql_value_name)
end
|
#group_values ⇒ Object
55
56
57
|
# File 'lib/active_reporter/dimension/bin.rb', line 55
def group_values
@group_values ||= to_bins(array_param(:bins).presence || autopopulate_bins)
end
|
#max ⇒ Object
report values are less than max, grouped by bin_width
21
22
23
|
# File 'lib/active_reporter/dimension/bin.rb', line 21
def max
@max ||= filter_max || report.records.maximum(expression)
end
|
#max_bins ⇒ Object
10
11
12
|
# File 'lib/active_reporter/dimension/bin.rb', line 10
def max_bins
self.class::MAX_BINS
end
|
#min ⇒ Object
Also known as:
bin_start
report values are greater than or equal to min, grouped by bin_width
15
16
17
|
# File 'lib/active_reporter/dimension/bin.rb', line 15
def min
@min ||= filter_min || report.records.minimum(expression)
end
|
#validate_params! ⇒ Object
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/active_reporter/dimension/bin.rb', line 71
def validate_params!
super
if params.key?(:bin_count)
invalid_param!(:bin_count, "must be numeric") unless ActiveReporter.numeric?(params[:bin_count])
invalid_param!(:bin_count, "must be greater than 0") unless params[:bin_count].to_i > 0
invalid_param!(:bin_count, "must be less than #{max_bins}") unless params[:bin_count].to_i <= max_bins
end
if array_param(:bins).present?
invalid_param!(:bins, "must be hashes with min/max keys and valid values, or nil") unless group_values.all?(&:valid?)
end
if array_param(:only).present?
invalid_param!(:only, "must be hashes with min/max keys and valid values, or nil") unless filter_values.all?(&:valid?)
end
end
|