module MockRedis::StreamMethods
Public Instance Methods
xadd(key, entry, opts = {})
click to toggle source
# File lib/mock_redis/stream_methods.rb, line 32 def xadd(key, entry, opts = {}) id = opts[:id] || '*' with_stream_at(key) do |stream| stream.add id, entry stream.trim opts[:maxlen] if opts[:maxlen] return stream.last_id end end
xlen(key)
click to toggle source
# File lib/mock_redis/stream_methods.rb, line 47 def xlen(key) with_stream_at(key) do |stream| return stream.count end end
xrange(key, first = '-', last = '+', count: nil)
click to toggle source
# File lib/mock_redis/stream_methods.rb, line 53 def xrange(key, first = '-', last = '+', count: nil) args = [first, last, false] args += ['COUNT', count] if count with_stream_at(key) do |stream| return stream.range(*args) end end
xread(keys, ids, count: nil, block: nil)
click to toggle source
# File lib/mock_redis/stream_methods.rb, line 69 def xread(keys, ids, count: nil, block: nil) args = [] args += ['COUNT', count] if count args += ['BLOCK', block.to_i] if block result = {} keys = keys.is_a?(Array) ? keys : [keys] ids = ids.is_a?(Array) ? ids : [ids] keys.each_with_index do |key, index| with_stream_at(key) do |stream| data = stream.read(ids[index], *args) result[key] = data unless data.empty? end end result end
xrevrange(key, last = '+', first = '-', count: nil)
click to toggle source
# File lib/mock_redis/stream_methods.rb, line 61 def xrevrange(key, last = '+', first = '-', count: nil) args = [first, last, true] args += ['COUNT', count] if count with_stream_at(key) do |stream| return stream.range(*args) end end
xtrim(key, count)
click to toggle source
# File lib/mock_redis/stream_methods.rb, line 41 def xtrim(key, count) with_stream_at(key) do |stream| stream.trim count end end
Private Instance Methods
assert_streamy(key)
click to toggle source
# File lib/mock_redis/stream_methods.rb, line 95 def assert_streamy(key) unless streamy?(key) raise Redis::CommandError, 'WRONGTYPE Operation against a key holding the wrong kind of value' end end
streamy?(key)
click to toggle source
# File lib/mock_redis/stream_methods.rb, line 91 def streamy?(key) data[key].nil? || data[key].is_a?(Stream) end
with_stream_at(key, &blk)
click to toggle source
# File lib/mock_redis/stream_methods.rb, line 87 def with_stream_at(key, &blk) with_thing_at(key, :assert_streamy, proc { Stream.new }, &blk) end