Class: Redpear::Store::Set
- Inherits:
-
Enumerable
- Object
- Base
- Enumerable
- Redpear::Store::Set
- 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
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Comparator.
-
#add(value) ⇒ Object
(also: #<<)
Adds a single value.
-
#all ⇒ Set
(also: #to_set, #value)
All members.
- #delete(value) ⇒ Object (also: #remove)
-
#diff(*others) ⇒ Array
(also: #-)
Subtracts values using other sets.
-
#diffstore(target, *others) ⇒ Redpear::Store::Set
Store the result of a diff in a new
target
key. - #each {|field, value| ... } ⇒ Object
-
#empty? ⇒ Boolean
True, if empty.
-
#include?(value) ⇒ Boolean
(also: #member?)
True, if value is included.
-
#interstore(target, *others) ⇒ Redpear::Store::Set
Store the result of an intersection in a new
target
key. -
#length ⇒ Integer
The number of items in the set.
-
#members ⇒ Array
(also: #to_a)
The array of members.
-
#move(target, value) ⇒ Object
Move a value to
target
set. -
#pop ⇒ String
Removes a random value.
-
#random ⇒ String
A random member.
-
#union(*others) ⇒ Array
(also: #+, #|, #merge)
Merges values of two sets.
-
#unionstore(target, *others) ⇒ Redpear::Store::Set
Store the result of a union in a new
target
key.
Methods inherited from Enumerable
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
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'
38 39 40 41 |
# File 'lib/redpear/store/set.rb', line 38 def add(value) conn.sadd key, value self end |
#all ⇒ Set Also known as: to_set, value
Returns 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
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
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
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
7 8 9 |
# File 'lib/redpear/store/set.rb', line 7 def each(&block) members.each(&block) end |
#empty? ⇒ Boolean
Returns 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.
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
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 |
#length ⇒ Integer
Returns the number of items in the set.
30 31 32 |
# File 'lib/redpear/store/set.rb', line 30 def length conn.scard key end |
#members ⇒ Array Also known as: to_a
Returns 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
152 153 154 |
# File 'lib/redpear/store/set.rb', line 152 def move(target, value) conn.smove key, target.to_s, value end |
#pop ⇒ String
Removes a random value
69 70 71 |
# File 'lib/redpear/store/set.rb', line 69 def pop conn.spop key end |
#random ⇒ String
Returns 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
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
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 |