class Timecop::Redis::Traveler

Attributes

redis[R]

Public Class Methods

new(redis) click to toggle source
# File lib/timecop/redis/traveler.rb, line 6
def initialize(redis)
  @redis = redis
end

Public Instance Methods

travel(from:, to:) click to toggle source
# File lib/timecop/redis/traveler.rb, line 10
def travel(from:, to:)
  raise ArgumentError, 'Auto-rewind block is not yet supported' if block_given?

  advanced_milliseconds = ((to - from) * 1000).to_i

  expirable_keys.each do |key, old_remaining_milliseconds|
    new_remaining_milliseconds = old_remaining_milliseconds - advanced_milliseconds

    if new_remaining_milliseconds > 0
      redis.pexpire(key, new_remaining_milliseconds)
    else
      redis.del(key)
    end
  end
end

Private Instance Methods

expirable_keys() { |key, remaining_milliseconds| ... } click to toggle source
# File lib/timecop/redis/traveler.rb, line 28
def expirable_keys
  return to_enum(__method__) unless block_given?

  redis.keys('*').each do |key|
    # https://redis.io/commands/pttl
    # The command returns -2 if the key does not exist.
    # The command returns -1 if the key exists but has no associated expire.
    remaining_milliseconds = redis.pttl(key)
    next if remaining_milliseconds < 0
    yield key, remaining_milliseconds
  end
end