class Hoodoo::TransientStore::Mocks::Redis

Mock known uses of Redis with test implementations. Use explicitly, or as an RSpec implicit mock via something like this:

allow( Redis ).to(
  receive( :new ).
  and_return( Hoodoo::TransientStore::Mocks::Redis.new )
)

…whenever you need to stub out real Redis. You will# probably want to add:

before :all do # (or ":each")
  Hoodoo::TransientStore::Mocks::Redis.reset()
end

…to “clean out Redis” before or between tests. You can check the contents of mock Redis by examining ::store‘s hash of data.

The test coverage for Hoodoo::TransientStore selects this backend in passing. Generally speaking you should favour Hoodoo::TransientStore over hard-coding to a storage engine available by the Hoodoo abstraction and, as a result, may never need this mock class at all.

Public Class Methods

reset() click to toggle source

Wipe out all saved data.

# File lib/hoodoo/transient_store/mocks/redis.rb, line 53
def self.reset
  @@store = {}
end
store() click to toggle source

For test analysis, return the hash of ‘Redis’ mock data.

Entries are referenced by the key you used to originally store them; values are hashes with “:expires_at” giving an expiry time or “nil” and “:value” giving your stored value.

# File lib/hoodoo/transient_store/mocks/redis.rb, line 47
def self.store
  @@store
end

Public Instance Methods

[]( key ) click to toggle source

Alias for get.

# File lib/hoodoo/transient_store/mocks/redis.rb, line 74
def []( key )
  get( key )
end
[]=( key, value ) click to toggle source

Alias for set.

# File lib/hoodoo/transient_store/mocks/redis.rb, line 95
def []=( key, value )
  set( key, value )
end
del( key ) click to toggle source

Remove data for the given key.

# File lib/hoodoo/transient_store/mocks/redis.rb, line 116
def del( key )
  if @@store.has_key?( key )
    @@store.delete( key )
    1
  else
    0
  end
end
expire( key, ttl ) click to toggle source

Set expiry time for a given key, which must exist.

ttl

time-to-live (‘live’ as in living, not as in ‘live TV’). A value in seconds, after which the data is considered expired.

# File lib/hoodoo/transient_store/mocks/redis.rb, line 105
def expire( key, ttl )
  unless @@store.has_key?( key )
    raise "Hoodoo::TransientStore::Mocks::Redis\#expire: Cannot find key '#{ key }'"
  end

  @@store[ key ][ :expires_at ] = Time.now.utc + ttl
  true
end
get( key ) click to toggle source

Get the data stored under the given key. Returns nil if not found / expired.

key

Key to look up (see set).

# File lib/hoodoo/transient_store/mocks/redis.rb, line 62
def get( key )
  data = @@store[ key ]
  return nil if data.nil?

  expires_at = data[ :expires_at ]
  return nil unless expires_at.nil? || Time.now < expires_at

  return data[ :value ]
end
info( command ) click to toggle source

Mock ‘info’ health check.

# File lib/hoodoo/transient_store/mocks/redis.rb, line 131
def info( command )
  { :alive => command }
end
quit() click to toggle source

Stub for ‘closing’ a connection.

# File lib/hoodoo/transient_store/mocks/redis.rb, line 127
def quit; end
set( key, value, ttl = nil ) click to toggle source

Set data for a given key.

key

Key under which to store data.

value

Data to store.

ttl

(Optional) time-to-live (‘live’ as in living, not as in ‘live TV’) - a value in seconds, after which the data is considered expired. If omitted, the data does not expire.

# File lib/hoodoo/transient_store/mocks/redis.rb, line 88
def set( key, value, ttl = nil )
  @@store[ key ] = { :value => value }
  'OK'
end