class Repor::Dimensions::TimeDimension

Constants

BIN_STEPS
DURATION_PATTERN
STEPS

Public Instance Methods

bin_start() click to toggle source
# 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_width() click to toggle source
# 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!() click to toggle source
# 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

Private Instance Methods

custom_bin_width() click to toggle source
# File lib/repor/dimensions/time_dimension.rb, line 41
def custom_bin_width
  if params[:bin_width].is_a?(Hash)
    params[:bin_width].map { |step, n| n.send(step) }.sum
  elsif params[:bin_width].is_a?(String)
    n, step = params[:bin_width].split.map(&:strip)
    n.to_i.send(step)
  end
end
default_bin_width() click to toggle source
# File lib/repor/dimensions/time_dimension.rb, line 61
def default_bin_width
  case domain
  when 0 then 1.day
  when 0..1.minute then 1.second
  when 0..2.hours then 1.minute
  when 0..2.days then 1.hour
  when 0..2.weeks then 1.day
  when 0..2.months then 1.week
  when 0..2.years then 1.month
  else 1.year
  end
end
valid_duration?(d) click to toggle source
# File lib/repor/dimensions/time_dimension.rb, line 50
def valid_duration?(d)
  case d
  when Hash
    d.all? { |step, n| step.to_sym.in?(STEPS) && n.is_a?(Fixnum) }
  when String
    d =~ DURATION_PATTERN
  else
    false
  end
end