Sha256: bc737d8abdf5053f13f64e2eba19e48739fd56fa64ea9b2000426a4665602c81

Contents?: true

Size: 869 Bytes

Versions: 4

Compression:

Stored size: 869 Bytes

Contents

module FakeRedis
  class ZSet < Hash

    def []=(key, val)
      super(key, _floatify(val))
    end

    def identical_scores?
      values.uniq.size == 1
    end

    # Increments the value of key by val
    def increment(key, val)
      self[key] += _floatify(val)
    end

    def select_by_score min, max
      min = _floatify(min, true)
      max = _floatify(max, false)
      reject {|_,v| v < min || v > max }
    end

    private

    # Originally lifted from redis-rb
    def _floatify(str, increment = true)
      if (( inf = str.to_s.match(/^([+-])?inf/i) ))
        (inf[1] == "-" ? -1.0 : 1.0) / 0.0
      elsif (( number = str.to_s.match(/^\((\d+)/i) ))
        number[1].to_i + (increment ? 1 : -1)
      else
        Float str.to_s
      end
    rescue ArgumentError
      raise Redis::CommandError, "ERR value is not a valid float"
    end

  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
fakeredis-0.7.0 lib/fakeredis/zset.rb
kuende-fakeredis-0.10.0 lib/fakeredis/zset.rb
fakeredis-0.6.0 lib/fakeredis/zset.rb
kuende-fakeredis-0.6.0 lib/fakeredis/zset.rb