Class: Conflow::Redis::SortedSetField

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

Overview

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

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)



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

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

#[]=(value, rank) ⇒ Integer

Set score of given element.

Examples:

field[:last] = 24 #=> 0

Parameters:

  • value (String, Symbol)

    element of the set

  • rank (Numeric)

    score to be assigned

Returns:

  • (Integer)

    Number of added elements (1 if key didn’t exist, 0 otherwise)



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

def []=(value, rank)
  command :zadd, [key, rank, value]
end

#add(hash) ⇒ String

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



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

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

#delete(value) ⇒ Integer

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)



54
55
56
# File 'lib/conflow/redis/sorted_set_field.rb', line 54

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

#first(num = 1) ⇒ String+

Returns first n elements of the sorted set

Parameters:

  • num (Integer) (defaults to: 1)

    amount of elements to be returned. Defaults to 1.

Returns:

  • (String, Array<String>)

    first num elements from the set



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

def first(num = 1)
  result = command :zrange, [key, 0, num - 1]
  num == 1 ? result[0] : result
end

#last(num = 1) ⇒ String+

Returns last n elements of the sorted set

Parameters:

  • num (Integer) (defaults to: 1)

    amount of elements to be returned. Defaults to 1.

Returns:

  • (String, Array<String>)

    last num elements from the set



69
70
71
72
# File 'lib/conflow/redis/sorted_set_field.rb', line 69

def last(num = 1)
  result = command :zrevrange, [key, 0, num - 1]
  num == 1 ? result[0] : result
end

#overwrite(hash) ⇒ String

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

Parameters:

  • hash (Hash)

    new values of the set

Returns:

  • (String)

    Redis response



83
84
85
86
87
88
89
90
# File 'lib/conflow/redis/sorted_set_field.rb', line 83

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

#sizeInteger

Number of elements in the set

Examples:

field.size #=> 4

Returns:

  • (Integer)

    Size of the set



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

def size
  command :zcard, [key]
end

#to_hHash

Creates regular Ruby Hash based on Redis values.

Returns:

  • (Hash)

    Hash representing this Sorted set



76
77
78
# File 'lib/conflow/redis/sorted_set_field.rb', line 76

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