Class: Conflow::Redis::SortedSetField Private

Inherits:
Field
  • Object
show all
Defined in:
lib/conflow/redis/sorted_set_field.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Represents Redis sorted set. Closest Ruby representation is a Hash where keys are elements of the set and values represent score.

Instance Attribute Summary

Attributes inherited from Field

#key

Instance Method Summary collapse

Methods inherited from Field

#initialize

Constructor Details

This class inherits a constructor from Conflow::Redis::Field

Instance Method Details

#[](value) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Access score of given element.

Examples:

field[:last] #=> 10

Parameters:

  • value (String, Symbol)

    element of the set

Returns:

  • (String)

    Score of the element (nil if element not present in set)



25
26
27
# File 'lib/conflow/redis/sorted_set_field.rb', line 25

def [](value)
  command :zscore, [key, value]
end

#add(hash) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Adds one or more keys to the set.

Examples:

Adding multiple fields

field.add(last: 10, tied: 2, second: 4, first: 2)

Parameters:

  • hash (Hash)

    hash of values and scores to be added

Returns:

  • (String)

    Redis response



15
16
17
# File 'lib/conflow/redis/sorted_set_field.rb', line 15

def add(hash)
  command :zadd, [key, hash_to_array(hash)]
end

#delete(value) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Remove element from the set.

Examples:

field.delete(:last) #=> 1

Parameters:

  • value (String, Symbol)

    element of the set

Returns:

  • (Integer)

    Number of removed elements (1 if key existed, 0 otherwise)



44
45
46
# File 'lib/conflow/redis/sorted_set_field.rb', line 44

def delete(value)
  command :zrem, [key, value]
end

#overwrite(hash) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Removes old values from the set and overrides them with new.

Parameters:

  • hash (Hash)

    new values of the set

Returns:

  • (String)

    Redis response



57
58
59
60
61
62
63
64
# File 'lib/conflow/redis/sorted_set_field.rb', line 57

def overwrite(hash)
  redis.with do |conn|
    conn.pipelined do
      conn.del(key)
      conn.zadd(key, hash_to_array(hash))
    end
  end
end

#sizeInteger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Number of elements in the set

Examples:

field.size #=> 4

Returns:

  • (Integer)

    Size of the set



34
35
36
# File 'lib/conflow/redis/sorted_set_field.rb', line 34

def size
  command :zcard, [key]
end

#to_hHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates regular Ruby Hash based on Redis values.

Returns:

  • (Hash)

    Hash representing this Sorted set



50
51
52
# File 'lib/conflow/redis/sorted_set_field.rb', line 50

def to_h
  Hash[command :zrange, [key, 0, -1, with_scores: true]]
end