Class: Hoodoo::TransientStore::Mocks::Redis

Inherits:
Object
  • Object
show all
Defined in:
lib/hoodoo/transient_store/mocks/redis.rb

Overview

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 Memcached. You will probably want to add:

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

…to “clean out Memcached” before or between tests. You can check the contents of mock Memcached 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.

Constant Summary collapse

@@store =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.resetObject

Wipe out all saved data.



54
55
56
# File 'lib/hoodoo/transient_store/mocks/redis.rb', line 54

def self.reset
  @@store = {}
end

.storeObject

For test analysis, return the hash of ‘memcached’ 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.



48
49
50
# File 'lib/hoodoo/transient_store/mocks/redis.rb', line 48

def self.store
  @@store
end

Instance Method Details

#[](key) ⇒ Object

Alias for #get.



75
76
77
# File 'lib/hoodoo/transient_store/mocks/redis.rb', line 75

def []( key )
  get( key )
end

#[]=(key, value) ⇒ Object

Alias for #set.



96
97
98
# File 'lib/hoodoo/transient_store/mocks/redis.rb', line 96

def []=( key, value )
  set( key, value )
end

#del(key) ⇒ Object

Remove data for the given key.



117
118
119
120
121
122
123
124
# File 'lib/hoodoo/transient_store/mocks/redis.rb', line 117

def del( key )
  if @@store.has_key?( key )
    @@store.delete( key )
    1
  else
    0
  end
end

#expire(key, ttl) ⇒ Object

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.



106
107
108
109
110
111
112
113
# File 'lib/hoodoo/transient_store/mocks/redis.rb', line 106

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) ⇒ Object

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

key

Key to look up (see #set).



63
64
65
66
67
68
69
70
71
# File 'lib/hoodoo/transient_store/mocks/redis.rb', line 63

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) ⇒ Object

Mock ‘info’ health check.



132
133
134
# File 'lib/hoodoo/transient_store/mocks/redis.rb', line 132

def info( command )
  { :alive => command }
end

#quitObject

Stub for ‘closing’ a connection.



128
# File 'lib/hoodoo/transient_store/mocks/redis.rb', line 128

def quit; end

#set(key, value, ttl = nil) ⇒ Object

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.



89
90
91
92
# File 'lib/hoodoo/transient_store/mocks/redis.rb', line 89

def set( key, value, ttl = nil )
  @@store[ key ] = { :value => value }
  'OK'
end