Class: MockRedis::Stream
- Inherits:
-
Object
- Object
- MockRedis::Stream
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/mock_redis/stream.rb,
lib/mock_redis/stream/id.rb
Defined Under Namespace
Classes: Id
Instance Attribute Summary collapse
-
#members ⇒ Object
Returns the value of attribute members.
Instance Method Summary collapse
- #add(id, values) ⇒ Object
- #each(&block) ⇒ Object
-
#initialize ⇒ Stream
constructor
A new instance of Stream.
- #last_id ⇒ Object
- #range(start, finish, reversed, *opts_in) ⇒ Object
- #read(id, *opts_in) ⇒ Object
- #trim(count) ⇒ Object
Constructor Details
#initialize ⇒ Stream
Returns a new instance of Stream.
15 16 17 18 |
# File 'lib/mock_redis/stream.rb', line 15 def initialize @members = Set.new @last_id = nil end |
Instance Attribute Details
#members ⇒ Object
Returns the value of attribute members.
11 12 13 |
# File 'lib/mock_redis/stream.rb', line 11 def members @members end |
Instance Method Details
#add(id, values) ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/mock_redis/stream.rb', line 24 def add(id, values) @last_id = MockRedis::Stream::Id.new(id, min: @last_id) if @last_id.to_s == '0-0' raise Redis::CommandError, 'ERR The ID specified in XADD must be greater than 0-0' end members.add [@last_id, Hash[values.map { |k, v| [k.to_s, v.to_s] }]] @last_id.to_s end |
#each(&block) ⇒ Object
74 75 76 |
# File 'lib/mock_redis/stream.rb', line 74 def each(&block) members.each(&block) end |
#last_id ⇒ Object
20 21 22 |
# File 'lib/mock_redis/stream.rb', line 20 def last_id @last_id.to_s end |
#range(start, finish, reversed, *opts_in) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/mock_redis/stream.rb', line 48 def range(start, finish, reversed, *opts_in) opts = opts_in, ['count'] start_id = MockRedis::Stream::Id.new(start) finish_id = MockRedis::Stream::Id.new(finish, sequence: Float::INFINITY) items = if start_id.exclusive members .select { |m| (start_id < m[0]) && (finish_id >= m[0]) } .map { |m| [m[0].to_s, m[1]] } else members .select { |m| (start_id <= m[0]) && (finish_id >= m[0]) } .map { |m| [m[0].to_s, m[1]] } end items.reverse! if reversed return items.first(opts['count'].to_i) if opts.key?('count') items end |
#read(id, *opts_in) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/mock_redis/stream.rb', line 66 def read(id, *opts_in) opts = opts_in, %w[count block] stream_id = MockRedis::Stream::Id.new(id) items = members.select { |m| (stream_id < m[0]) }.map { |m| [m[0].to_s, m[1]] } return items.first(opts['count'].to_i) if opts.key?('count') items end |
#trim(count) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/mock_redis/stream.rb', line 34 def trim(count) deleted = @members.size - count if deleted > 0 @members = if count == 0 Set.new else @members.to_a[-count..].to_set end deleted else 0 end end |