Sha256: b3b769337acee43d931e3f98d6db53281cb89edb775fefacdf4ba0b17511c977

Contents?: true

Size: 1.45 KB

Versions: 6

Compression:

Stored size: 1.45 KB

Contents

class MockRedis
  class Stream
    class Id
      include Comparable

      attr_accessor :timestamp, :sequence

      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 self <= min
            raise Redis::CommandError,
                  'ERR The ID specified in XADD is equal or smaller than ' \
                  'the target stream top item'
          end
        end
      end

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

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
mock_redis-0.27.3 lib/mock_redis/stream/id.rb
mock_redis-0.27.2 lib/mock_redis/stream/id.rb
mock_redis-0.27.1 lib/mock_redis/stream/id.rb
mock_redis-0.27.0 lib/mock_redis/stream/id.rb
mock_redis-0.26.0 lib/mock_redis/stream/id.rb
mock_redis-0.25.0 lib/mock_redis/stream/id.rb