class Redis::DailyCounter

Attributes

current_date[R]
original_key[R]

Public Class Methods

new(key, *args) click to toggle source
Calls superclass method
# File lib/redis/daily_counter.rb, line 5
def initialize(key, *args)
  @original_key = key
  @current_date = Date.today
  super(redis_daily_field_key(current_date), *args)
end

Public Instance Methods

[](date, length = nil) click to toggle source
# File lib/redis/daily_counter.rb, line 13
def [](date, length = nil)
  if date.is_a? Range
    range(date.first, date.max)
  elsif length
    case length <=> 0
    when 1  then range(date, date + length - 1)
    when 0  then []
    when -1 then nil  # Ruby does this (a bit weird)
    end
  else
    at(date)
  end
end
Also aliased as: slice
at(date) click to toggle source
# File lib/redis/daily_counter.rb, line 37
def at(date)
  redis.get(redis_daily_field_key(date)).to_i
end
delete(date) click to toggle source
# File lib/redis/daily_counter.rb, line 28
def delete(date)
  redis.del(redis_daily_field_key(date))
end
range(start_date, end_date) click to toggle source
# File lib/redis/daily_counter.rb, line 32
def range(start_date, end_date)
  keys = (start_date..end_date).map { |date| redis_daily_field_key(date) }
  redis.mget(*keys).map(&:to_i)
end
slice(date, length = nil)
Alias for: []

Private Instance Methods

redis_daily_field_key(date) click to toggle source
# File lib/redis/daily_counter.rb, line 43
def redis_daily_field_key(date)
  [original_key, date.to_date].flatten.join(':')
end