Class: Repor::Dimensions::TimeDimension

Inherits:
BinDimension show all
Defined in:
lib/repor/dimensions/time_dimension.rb

Defined Under Namespace

Classes: Bin

Constant Summary collapse

STEPS =
%i(seconds minutes hours days weeks months years)
BIN_STEPS =
(STEPS - [:seconds]).map { |s| s.to_s.singularize }
DURATION_PATTERN =
/\A\d+ (?:#{STEPS.map{|s| "#{s}?" }.join('|')})\z/

Instance Attribute Summary

Attributes inherited from BaseDimension

#name, #opts, #report

Instance Method Summary collapse

Methods inherited from BinDimension

#domain, #filter, #filter_max, #filter_min, #filter_values, #group, #group_values, #max, #max_bins, #min

Methods inherited from BaseDimension

#expression, #extract_sql_value, #filter, #filter_values, #filtering?, #group, #group_values, #grouping?, #initialize, #null_order, #nulls_last?, #order, #order_expression, #params, #relate, #sort_desc?, #sort_order

Constructor Details

This class inherits a constructor from Repor::Dimensions::BaseDimension

Instance Method Details

#bin_startObject



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/repor/dimensions/time_dimension.rb', line 26

def bin_start
  # ensure that each autogenerated bin represents a correctly aligned
  # day/week/month/year
  bin_start = super
  if bin_start.nil?
    nil
  elsif step = BIN_STEPS.detect { |step| bin_width == 1.send(step) }
    bin_start.send(:"beginning_of_#{step}")
  else
    bin_start
  end
end

#bin_widthObject



16
17
18
19
20
21
22
23
24
# File 'lib/repor/dimensions/time_dimension.rb', line 16

def bin_width
  @bin_width ||= if params.key?(:bin_width)
    custom_bin_width
  elsif params.key?(:bin_count) && domain > 0
    (domain / params[:bin_count].to_f).seconds
  else
    default_bin_width
  end
end

#validate_params!Object



8
9
10
11
12
13
14
# File 'lib/repor/dimensions/time_dimension.rb', line 8

def validate_params!
  super

  if params.key?(:bin_width) && !valid_duration?(params[:bin_width])
    invalid_param!(:bin_width, "must be a hash of one of #{STEPS} to an integer")
  end
end