class ActiveReporter::Dimension::Bin

Constants

MAX_BINS

Public Instance Methods

bin_start()
Alias for: min
domain() click to toggle source
# File lib/active_reporter/dimension/bin.rb, line 29
def domain
  min.nil? || max.nil? ? 0 : (max - min)
end
filter(relation) click to toggle source
# File lib/active_reporter/dimension/bin.rb, line 41
def filter(relation)
  filter_values.filter(relation, expression)
end
filter_max() click to toggle source
# File lib/active_reporter/dimension/bin.rb, line 25
def filter_max
  filter_values_for(:max).max
end
filter_min() click to toggle source
# File lib/active_reporter/dimension/bin.rb, line 21
def filter_min
  filter_values_for(:min).min
end
filter_values() click to toggle source
# File lib/active_reporter/dimension/bin.rb, line 37
def filter_values
  @filter_values ||= to_bins(super)
end
group(relation) click to toggle source
# File lib/active_reporter/dimension/bin.rb, line 45
def group(relation)
  group_values.group(relation, expression, sql_value_name)
end
group_values() click to toggle source
# File lib/active_reporter/dimension/bin.rb, line 33
def group_values
  @group_values ||= to_bins(array_param(:bins).presence || autopopulate_bins)
end
max() click to toggle source
# File lib/active_reporter/dimension/bin.rb, line 17
def max
  @max ||= filter_max || report.records.maximum(expression)
end
max_bins() click to toggle source
# File lib/active_reporter/dimension/bin.rb, line 8
def max_bins
  self.class::MAX_BINS
end
min() click to toggle source
# File lib/active_reporter/dimension/bin.rb, line 12
def min
  @min ||= filter_min || report.records.minimum(expression)
end
Also aliased as: bin_start
validate_params!() click to toggle source
# File lib/active_reporter/dimension/bin.rb, line 49
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

Private Instance Methods

autopopulate_bins() click to toggle source
# File lib/active_reporter/dimension/bin.rb, line 97
def autopopulate_bins
  return [] if bin_start.blank? || max.blank?

  bin_max = filter_values_for(:max).present? ? (max - bin_width) : max
  
  bin_count = (bin_max - bin_start)/(bin_width)
  invalid_param!(:bin_width, "is too small for the domain; would generate #{bin_count.to_i} bins") if bin_count > max_bins

  bin_edge = bin_start
  bins = []

  loop do
    break if bin_edge > bin_max

    bin = { min: bin_edge, max: bin_edge + bin_width }
    bins << bin
    bin_edge = bin[:max]
  end

  bins.reverse! if sort_desc?
  ( nulls_last? ? bins.push(nil) : bins.unshift(nil) ) if data_contains_nil?

  bins
end
data_contains_nil?() click to toggle source
# File lib/active_reporter/dimension/bin.rb, line 93
def data_contains_nil?
  report.records.where("#{expression} IS NULL").exists?
end
filter_values_for(key) click to toggle source
# File lib/active_reporter/dimension/bin.rb, line 69
def filter_values_for(key)
  filter_values.map { |filter_value| filter_value.send(key) }.compact
end
sanitize_sql_value(value) click to toggle source
# File lib/active_reporter/dimension/bin.rb, line 89
def sanitize_sql_value(value)
  set.from_sql(value)
end
set() click to toggle source
# File lib/active_reporter/dimension/bin.rb, line 77
def set
  self.class.const_get(:Set)
end
table() click to toggle source
# File lib/active_reporter/dimension/bin.rb, line 73
def table
  self.class.const_get(:Table)
end
to_bin(bin) click to toggle source
# File lib/active_reporter/dimension/bin.rb, line 85
def to_bin(bin)
  set.from_hash(bin)
end
to_bins(bins) click to toggle source
# File lib/active_reporter/dimension/bin.rb, line 81
def to_bins(bins)
  table.new(bins.map(&method(:to_bin)))
end