Class: Redpear::Store::Set

Inherits:
Enumerable show all
Includes:
Enumerable
Defined in:
lib/redpear/store/set.rb

Constant Summary

Constants inherited from Base

Base::IS_NIL, Base::IS_ONE, Base::IS_TRUE, Base::IS_ZERO, Base::PICK_FIRST, Base::TO_INT, Base::TO_SET

Instance Attribute Summary

Attributes inherited from Base

#conn, #key

Instance Method Summary collapse

Methods inherited from Enumerable

#count, #size

Methods inherited from Base

#clear, #exists?, #expire, #expire_at, #expire_in, #initialize, #inspect, #purge!, temporary, #ttl, #type, #watch

Constructor Details

This class inherits a constructor from Redpear::Store::Base

Instance Method Details

#==(other) ⇒ Boolean

Comparator

Returns:

  • (Boolean)

    true if contains same members as other



143
144
145
# File 'lib/redpear/store/set.rb', line 143

def ==(other)
  other.respond_to?(:to_set) && other.to_set == to_set
end

#add(value) ⇒ Object Also known as: <<

Adds a single value. Chainable example:

set << 'a' << 'b'

Parameters:

  • value (String)

    A value to add



38
39
40
41
# File 'lib/redpear/store/set.rb', line 38

def add(value)
  conn.sadd key, value
  self
end

#allSet Also known as: to_set, value

Returns all members.

Returns:

  • (Set)

    all members



12
13
14
15
16
17
18
19
# File 'lib/redpear/store/set.rb', line 12

def all
  case value = members
  when Redis::Future
    value.instance_eval { @transformation = TO_SET }
  else
    value.to_set
  end
end

#delete(value) ⇒ Object Also known as: remove

Parameters:

  • value (String)

    A value to delete



46
47
48
# File 'lib/redpear/store/set.rb', line 46

def delete(value)
  conn.srem key, value
end

#diff(*others) ⇒ Array Also known as: -

Subtracts values using other sets

Parameters:

  • others (multiple)

    The other sets

Returns:

  • (Array)

    remaining values



77
78
79
# File 'lib/redpear/store/set.rb', line 77

def diff(*others)
  conn.sdiff key, *others.map(&:to_s)
end

#diffstore(target, *others) ⇒ Redpear::Store::Set

Store the result of a diff in a new target key

Parameters:

  • target (String)

    The target key

  • others (multiple)

    The other sets

Returns:



88
89
90
91
# File 'lib/redpear/store/set.rb', line 88

def diffstore(target, *others)
  conn.sdiffstore target.to_s, key, *others.map(&:to_s)
  self.class.new target.to_s, conn
end

#each {|field, value| ... } ⇒ Object

Yields:

  • over a field-value pair

Yield Parameters:

  • field (String)
  • value (String)


7
8
9
# File 'lib/redpear/store/set.rb', line 7

def each(&block)
  members.each(&block)
end

#empty?Boolean

Returns true, if empty.

Returns:

  • (Boolean)

    true, if empty



58
59
60
61
62
63
64
65
# File 'lib/redpear/store/set.rb', line 58

def empty?
  case value = length
  when Redis::Future
    value.instance_eval { @transformation = IS_ZERO }
  else
    value.zero?
  end
end

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

Returns true, if value is included.

Returns:

  • (Boolean)

    true, if value is included



52
53
54
# File 'lib/redpear/store/set.rb', line 52

def include?(value)
  conn.sismember(key, value)
end

#interstore(target, *others) ⇒ Redpear::Store::Set

Store the result of an intersection in a new target key

Parameters:

  • target (String)

    The target key

  • others (multiple)

    The other sets

Returns:



131
132
133
134
# File 'lib/redpear/store/set.rb', line 131

def interstore(target, *others)
  conn.sinterstore target.to_s, key, *others.map(&:to_s)
  self.class.new target.to_s, conn
end

#lengthInteger

Returns the number of items in the set.

Returns:

  • (Integer)

    the number of items in the set



30
31
32
# File 'lib/redpear/store/set.rb', line 30

def length
  conn.scard key
end

#membersArray Also known as: to_a

Returns the array of members.

Returns:

  • (Array)

    the array of members



24
25
26
# File 'lib/redpear/store/set.rb', line 24

def members
  conn.smembers(key)
end

#move(target, value) ⇒ Object

Move a value to target set

Parameters:

  • target (String)

    The key of the target set

  • value (String)

    The value to move



152
153
154
# File 'lib/redpear/store/set.rb', line 152

def move(target, value)
  conn.smove key, target.to_s, value
end

#popString

Removes a random value

Returns:

  • (String)

    the removed value



69
70
71
# File 'lib/redpear/store/set.rb', line 69

def pop
  conn.spop key
end

#randomString

Returns a random member.

Returns:

  • (String)

    a random member



137
138
139
# File 'lib/redpear/store/set.rb', line 137

def random
  conn.srandmember key
end

#union(*others) ⇒ Array Also known as: +, |, merge

Merges values of two sets

Parameters:

  • others (multiple)

    The other sets

Returns:

  • (Array)

    union



97
98
99
# File 'lib/redpear/store/set.rb', line 97

def union(*others)
  conn.sunion key, *others.map(&:to_s)
end

#unionstore(target, *others) ⇒ Redpear::Store::Set

Store the result of a union in a new target key

Parameters:

Returns:



110
111
112
113
# File 'lib/redpear/store/set.rb', line 110

def unionstore(target, *others)
  conn.sunionstore target.to_s, key, *others.map(&:to_s)
  self.class.new target.to_s, conn
end