Module: RedisModel::Types::Set

Includes:
Base
Defined in:
lib/redis_model/types/set.rb

Overview

Internal: Methods needed for Set type.

Instance Method Summary collapse

Methods included from Base

#connection, #del, #exists?

Instance Method Details

#<<(value) ⇒ Object

Public: Pushes a element into the set using SADD command.

Returns true.



26
27
28
# File 'lib/redis_model/types/set.rb', line 26

def <<(value)
  connection.sadd key_label, value
end

#countObject Also known as: length

Public: Retrieves length of Redis set using SCARD command.

Returns Integer containing cardinality of the set.



17
18
19
# File 'lib/redis_model/types/set.rb', line 17

def count
  connection.scard key_label
end

#include?(value) ⇒ Boolean

Public: Asserts value is included in the set using SISMEMBER command.

Returns true if value is included in the set, false otherwise.

Returns:

  • (Boolean)


54
55
56
# File 'lib/redis_model/types/set.rb', line 54

def include?(value)
  connection.sismember key_label, value.to_s
end

#pick(count) ⇒ Object

Public: Picks a member among elements in the set using SRANDMEMBER command.

count - Number of elements to pick.

Returns Array containing elements in the set randomly selected.



43
44
45
46
47
48
49
# File 'lib/redis_model/types/set.rb', line 43

def pick(count)
  RedisModel::Base.connection.pipelined do
    count.times do
      connection.srandmember(key_label)
    end
  end
end

#remove(value) ⇒ Object

Public: Removes a element from the set using SREM command.

Returns true.



33
34
35
# File 'lib/redis_model/types/set.rb', line 33

def remove(value)
  connection.srem key_label, value
end

#to_aObject

Public: Fetches elements in Redis set as Array using SMEMBERS command.

Returns Array containing elements in the set.



10
11
12
# File 'lib/redis_model/types/set.rb', line 10

def to_a
  connection.smembers key_label
end