Class: MockRedis::Stream::Id

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/mock_redis/stream/id.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, min: nil, sequence: 0) ⇒ Id

Returns a new instance of Id.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/mock_redis/stream/id.rb', line 8

def initialize(id, min: nil, sequence: 0)
  case id
  when '*'
    @timestamp = (Time.now.to_f * 1000).to_i
    @sequence = 0
    if self <= min
      @timestamp = min.timestamp
      @sequence = min.sequence + 1
    end
  when '-'
    @timestamp = @sequence = 0
  when '+'
    @timestamp = @sequence = Float::INFINITY
  else
    if id.is_a? String
      (_, @timestamp, @sequence) = id.match(/^(\d+)-?(\d+)?$/)
                                     .to_a
      if @timestamp.nil?
        raise Redis::CommandError,
              'ERR Invalid stream ID specified as stream command argument'
      end
      @timestamp = @timestamp.to_i
    else
      @timestamp = id
    end
    @sequence = @sequence.nil? ? sequence : @sequence.to_i
    if (@timestamp == 0 && @sequence == 0) || self <= min
      raise Redis::CommandError,
            'ERR The ID specified in XADD is equal or smaller than ' \
            'the target stream top item'
    end
  end
end

Instance Attribute Details

#sequenceObject

Returns the value of attribute sequence.



6
7
8
# File 'lib/mock_redis/stream/id.rb', line 6

def sequence
  @sequence
end

#timestampObject

Returns the value of attribute timestamp.



6
7
8
# File 'lib/mock_redis/stream/id.rb', line 6

def timestamp
  @timestamp
end

Instance Method Details

#<=>(other) ⇒ Object



46
47
48
49
50
# File 'lib/mock_redis/stream/id.rb', line 46

def <=>(other)
  return 1 if other.nil?
  return @sequence <=> other.sequence if @timestamp == other.timestamp
  @timestamp <=> other.timestamp
end

#to_sObject



42
43
44
# File 'lib/mock_redis/stream/id.rb', line 42

def to_s
  "#{@timestamp}-#{@sequence}"
end