class StartHer::StubedRedis

Attributes

db[R]
namespace[R]

Public Class Methods

new(namespace:) click to toggle source
# File lib/start_her/testing.rb, line 40
def initialize(namespace:)
  @namespace = namespace
  @db = {
    lists:  {},
    lists_expiration:  {},
    channels: {},
    strings: {}
  }
end

Public Instance Methods

clear_db() click to toggle source
# File lib/start_her/testing.rb, line 50
def clear_db
  @db = {
    lists:  {},
    lists_expiration:  {},
    channels: {},
    strings: {}
  }
end
expire(key, ttl) click to toggle source

Keys command

# File lib/start_her/testing.rb, line 81
def expire(key, ttl)
  lists_expiration[key] = ttl
end
get(key) click to toggle source

String command

# File lib/start_her/testing.rb, line 86
def get(key)
  strings[key]
end
getset(key, value) click to toggle source

String command

# File lib/start_her/testing.rb, line 91
def getset(key, value)
  tmp = strings[key]
  strings[key] = value
  tmp
end
keys(pattern) click to toggle source

Keys command

# File lib/start_her/testing.rb, line 65
def keys(pattern)
  lists.keys.select { |key| key.match(pattern.gsub '**', '*') }
end
lpush(list, data) click to toggle source

List command

# File lib/start_her/testing.rb, line 107
def lpush(list, data)
  (lists[fullkey(list)] ||= []).insert(0, data)
end
lrange(list, min, max) click to toggle source

List command

# File lib/start_her/testing.rb, line 112
def lrange(list, min, max)
  lists[fullkey(list)][min..max]
end
multi() { |self| ... } click to toggle source

Keys command

# File lib/start_her/testing.rb, line 60
def multi
  yield self
end
psubscribe(*channels) { |db[fullkey(channel)]| ... } click to toggle source

PubSub command rubocop:disable Metrics/AbcSize

# File lib/start_her/testing.rb, line 127
def psubscribe(*channels)
  channels.each do |channel|
    db[:channels][fullkey(channel)] = Subscription.new
    yield db[:channels][fullkey(channel)]
    db[:channels][fullkey(channel)].callbacks[:psubscribe].call(channel, 0)
  end
end
publish(channel, message) click to toggle source

PubSub command rubocop:disable Metric/AbcSize,Style/MultilineOperationIndentation

# File lib/start_her/testing.rb, line 118
def publish(channel, message)
  return unless channels[fullkey(channel)] && channels[fullkey(channel)].callbacks &&
    channels[fullkey(channel)].callbacks[:pmessage]
  channels[fullkey(channel)].callbacks[:pmessage].call(channel, channel, message)
end
setnx(key, value) click to toggle source
# File lib/start_her/testing.rb, line 97
def setnx(key, value)
  if strings[key]
    0
  else
    strings[key] = value
    1
  end
end
ttl(key) click to toggle source

Keys command

# File lib/start_her/testing.rb, line 70
def ttl(key)
  if lists_expiration[key]
    lists_expiration[key]
  elsif lists[key] || channels[key]
    -1
  else
    -2
  end
end

Private Instance Methods

channels() click to toggle source
# File lib/start_her/testing.rb, line 170
def channels
  db[:channels]
end
fullkey(key) click to toggle source
# File lib/start_her/testing.rb, line 154
def fullkey(key)
  if namespace
    "#{namespace}:#{key}"
  else
    key
  end
end
lists() click to toggle source
# File lib/start_her/testing.rb, line 162
def lists
  db[:lists]
end
lists_expiration() click to toggle source
# File lib/start_her/testing.rb, line 166
def lists_expiration
  db[:lists_expiration]
end
strings() click to toggle source
# File lib/start_her/testing.rb, line 174
def strings
  db[:strings]
end