Class: Conflow::Redis::SortedSetField
- 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
Instance Method Summary collapse
-
#[](value) ⇒ String
Access score of given element.
-
#[]=(value, rank) ⇒ Integer
Set score of given element.
-
#add(hash) ⇒ String
Adds one or more keys to the set.
-
#delete(value) ⇒ Integer
Remove element from the set.
-
#first(num = 1) ⇒ String+
Returns first n elements of the sorted set.
-
#last(num = 1) ⇒ String+
Returns last n elements of the sorted set.
-
#overwrite(hash) ⇒ String
Removes old values from the set and overrides them with new.
-
#size ⇒ Integer
Number of elements in the set.
-
#to_h ⇒ Hash
Creates regular Ruby Hash based on Redis values.
Methods inherited from Field
Constructor Details
This class inherits a constructor from Conflow::Redis::Field
Instance Method Details
#[](value) ⇒ String
Access score of given element.
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.
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.
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.
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
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
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.
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 |
#size ⇒ Integer
Number of elements in the set
44 45 46 |
# File 'lib/conflow/redis/sorted_set_field.rb', line 44 def size command :zcard, [key] end |
#to_h ⇒ Hash
Creates regular Ruby Hash based on Redis values.
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 |