Class: StartHer::StubedRedis

Inherits:
Object
  • Object
show all
Defined in:
lib/start_her/testing.rb

Defined Under Namespace

Classes: Subscription

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespace:) ⇒ StubedRedis

Returns a new instance of StubedRedis.



40
41
42
43
44
45
46
47
48
# File 'lib/start_her/testing.rb', line 40

def initialize(namespace:)
  @namespace = namespace
  @db = {
    lists:  {},
    lists_expiration:  {},
    channels: {},
    strings: {}
  }
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



38
39
40
# File 'lib/start_her/testing.rb', line 38

def db
  @db
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



38
39
40
# File 'lib/start_her/testing.rb', line 38

def namespace
  @namespace
end

Instance Method Details

#clear_dbObject



50
51
52
53
54
55
56
57
# File 'lib/start_her/testing.rb', line 50

def clear_db
  @db = {
    lists:  {},
    lists_expiration:  {},
    channels: {},
    strings: {}
  }
end

#expire(key, ttl) ⇒ Object

Keys command



81
82
83
# File 'lib/start_her/testing.rb', line 81

def expire(key, ttl)
  lists_expiration[key] = ttl
end

#get(key) ⇒ Object

String command



86
87
88
# File 'lib/start_her/testing.rb', line 86

def get(key)
  strings[key]
end

#getset(key, value) ⇒ Object

String command



91
92
93
94
95
# File 'lib/start_her/testing.rb', line 91

def getset(key, value)
  tmp = strings[key]
  strings[key] = value
  tmp
end

#keys(pattern) ⇒ Object

Keys command



65
66
67
# File 'lib/start_her/testing.rb', line 65

def keys(pattern)
  lists.keys.select { |key| key.match(pattern.gsub '**', '*') }
end

#lpush(list, data) ⇒ Object

List command



107
108
109
# File 'lib/start_her/testing.rb', line 107

def lpush(list, data)
  (lists[fullkey(list)] ||= []).insert(0, data)
end

#lrange(list, min, max) ⇒ Object

List command



112
113
114
# File 'lib/start_her/testing.rb', line 112

def lrange(list, min, max)
  lists[fullkey(list)][min..max]
end

#multi {|_self| ... } ⇒ Object

Keys command

Yields:

  • (_self)

Yield Parameters:



60
61
62
# File 'lib/start_her/testing.rb', line 60

def multi
  yield self
end

#psubscribe(*channels) ⇒ Object

PubSub command rubocop:disable Metrics/AbcSize



127
128
129
130
131
132
133
# 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) ⇒ Object

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



118
119
120
121
122
# 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) ⇒ Object



97
98
99
100
101
102
103
104
# 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) ⇒ Object

Keys command



70
71
72
73
74
75
76
77
78
# 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