Class: Redstruct::Types::Set

Inherits:
Struct show all
Defined in:
lib/redstruct/types/set.rb

Overview

Note: keep in mind Redis converts everything to a string on the DB side

Instance Attribute Summary

Attributes inherited from Base

#key

Instance Method Summary collapse

Methods inherited from Struct

#delete, #exists?, #expire, #expire_at, #inspectable_attributes, #persist, #type

Methods included from Utils::Inspectable

#inspect, #inspectable_attributes, #to_s

Methods inherited from Base

#initialize, #inspectable_attributes, #to_h, #with

Constructor Details

This class inherits a constructor from Redstruct::Types::Base

Instance Method Details

#+(other) ⇒ Object



39
40
41
# File 'lib/redstruct/types/set.rb', line 39

def +(other)
  return ::Set.new(self.connection.sunion(@key, other.key))
end

#-(other) ⇒ Object



35
36
37
# File 'lib/redstruct/types/set.rb', line 35

def -(other)
  return ::Set.new(self.connection.sdiff(@key, other.key))
end

#add(*members) ⇒ Object Also known as: <<



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

def add(*members)
  return self.connection.sadd(@key, members)
end

#clearObject



5
6
7
# File 'lib/redstruct/types/set.rb', line 5

def clear
  delete
end

#contain?(member) ⇒ Boolean Also known as: include?

Returns:

  • (Boolean)


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

def contain?(member)
  return self.connection.sismember(@key, member)
end

#difference(other, dest: nil) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/redstruct/types/set.rb', line 47

def difference(other, dest: nil)
  destination = coerce_destination(dest)
  return self - other if destination.nil?

  self.connection.sdiffstore(destination.key, @key, other.key)
  return destination
end

#each(options = {}, &block) ⇒ Object



79
80
81
# File 'lib/redstruct/types/set.rb', line 79

def each(options = {}, &block)
  return self.connection.sscan_each(@key, options, &block)
end

#empty?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/redstruct/types/set.rb', line 13

def empty?
  return !exists?
end

#intersection(other, dest: nil) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/redstruct/types/set.rb', line 55

def intersection(other, dest: nil)
  destination = coerce_destination(dest)
  return self - other if destination.nil?

  self.connection.sinterstore(destination.key, @key, other.key)
  return destination
end

#pop(count: 1) ⇒ Object



71
72
73
# File 'lib/redstruct/types/set.rb', line 71

def pop(count: 1)
  return self.connection.spop(@key, count.to_i)
end

#random(count: 1) ⇒ Object



9
10
11
# File 'lib/redstruct/types/set.rb', line 9

def random(count: 1)
  return self.connection.srandmember(@key, count.to_i)
end

#remove(*members) ⇒ Object



75
76
77
# File 'lib/redstruct/types/set.rb', line 75

def remove(*members)
  return self.connection.srem(@key, *members)
end

#sizeObject



31
32
33
# File 'lib/redstruct/types/set.rb', line 31

def size
  return self.connection.scard(@key).to_i
end

#to_aObject



22
23
24
# File 'lib/redstruct/types/set.rb', line 22

def to_a
  return self.connection.smembers(@key)
end

#union(other, dest: nil) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/redstruct/types/set.rb', line 63

def union(other, dest: nil)
  destination = coerce_destination(dest)
  return self - other if destination.nil?

  self.connection.sunionstore(destination.key, @key, other.key)
  return destination
end

#|(other) ⇒ Object



43
44
45
# File 'lib/redstruct/types/set.rb', line 43

def |(other)
  return ::Set.new(self.connection.sinter(@key, other.key))
end