Class: Familia::Set

Inherits:
RedisType show all
Defined in:
lib/familia/types/unsorted_set.rb

Instance Attribute Summary

Attributes inherited from RedisType

#dump_method, #load_method, #name, #opts, #parent

Instance Method Summary collapse

Methods inherited from RedisType

#class?, #db, inherited, #initialize, #parent?, #parent_class?, #parent_instance?, #redis, #rediskey, register, #ttl, #uri, valid_keys_only

Methods included from RedisType::Serialization

#from_redis, #multi_from_redis, #multi_from_redis_with_nil, #to_redis, #update_expiration

Methods included from RedisType::Commands

#delete!, #echo, #exists?, #expire, #expireat, #persist, #realttl, #rename, #renamenx, #type

Methods included from Base

add_feature

Constructor Details

This class inherits a constructor from Familia::RedisType

Instance Method Details

#<<(v) ⇒ Object



20
21
22
# File 'lib/familia/types/unsorted_set.rb', line 20

def <<(v)
  add v
end

#add(*values) ⇒ Object



14
15
16
17
18
# File 'lib/familia/types/unsorted_set.rb', line 14

def add *values
  values.flatten.compact.each { |v| redis.sadd? rediskey, to_redis(v) }
  update_expiration
  self
end

#collect(&blk) ⇒ Object



44
45
46
# File 'lib/familia/types/unsorted_set.rb', line 44

def collect(&blk)
  members.collect(&blk)
end

#collectraw(&blk) ⇒ Object



60
61
62
# File 'lib/familia/types/unsorted_set.rb', line 60

def collectraw(&blk)
  membersraw.collect(&blk)
end

#delete(val) ⇒ Object Also known as: remove, rem, del



73
74
75
# File 'lib/familia/types/unsorted_set.rb', line 73

def delete(val)
  redis.srem rediskey, to_redis(val)
end

#each(&blk) ⇒ Object



36
37
38
# File 'lib/familia/types/unsorted_set.rb', line 36

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

#each_with_index(&blk) ⇒ Object



40
41
42
# File 'lib/familia/types/unsorted_set.rb', line 40

def each_with_index(&blk)
  members.each_with_index(&blk)
end

#eachraw(&blk) ⇒ Object



52
53
54
# File 'lib/familia/types/unsorted_set.rb', line 52

def eachraw(&blk)
  membersraw.each(&blk)
end

#eachraw_with_index(&blk) ⇒ Object



56
57
58
# File 'lib/familia/types/unsorted_set.rb', line 56

def eachraw_with_index(&blk)
  membersraw.each_with_index(&blk)
end

#empty?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/familia/types/unsorted_set.rb', line 10

def empty?
  size.zero?
end

#intersection(*setkeys) ⇒ Object



80
81
82
# File 'lib/familia/types/unsorted_set.rb', line 80

def intersection *setkeys
  # TODO
end

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

Returns:

  • (Boolean)


68
69
70
# File 'lib/familia/types/unsorted_set.rb', line 68

def member?(val)
  redis.sismember rediskey, to_redis(val)
end

#membersObject Also known as: all, to_a



24
25
26
27
28
# File 'lib/familia/types/unsorted_set.rb', line 24

def members
  echo :members, caller(1..1).first if Familia.debug
  el = membersraw
  multi_from_redis(*el)
end

#membersrawObject



32
33
34
# File 'lib/familia/types/unsorted_set.rb', line 32

def membersraw
  redis.smembers(rediskey)
end

#move(dstkey, val) ⇒ Object



88
89
90
# File 'lib/familia/types/unsorted_set.rb', line 88

def move(dstkey, val)
  redis.smove rediskey, dstkey, val
end

#popObject



84
85
86
# File 'lib/familia/types/unsorted_set.rb', line 84

def pop
  redis.spop rediskey
end

#randomObject



92
93
94
# File 'lib/familia/types/unsorted_set.rb', line 92

def random
  from_redis randomraw
end

#randomrawObject



96
97
98
# File 'lib/familia/types/unsorted_set.rb', line 96

def randomraw
  redis.srandmember(rediskey)
end

#select(&blk) ⇒ Object



48
49
50
# File 'lib/familia/types/unsorted_set.rb', line 48

def select(&blk)
  members.select(&blk)
end

#selectraw(&blk) ⇒ Object



64
65
66
# File 'lib/familia/types/unsorted_set.rb', line 64

def selectraw(&blk)
  membersraw.select(&blk)
end

#selfObject

Make the value stored at KEY identical to the given list define_method :“#RedisType#name_sync” do |*latest|

latest = latest.flatten.compact
# Do nothing if we're given an empty Array.
# Otherwise this would clear all current values
if latest.empty?
  false
else
  # Convert to a list of index values if we got the actual objects
  latest = latest.collect { |obj| obj.index } if klass === latest.first
  current = send("#{name_plural}raw")
  added = latest-current
  removed = current-latest
  #Familia.info "#{self.index}: adding: #{added}"
  added.each { |v| self.send("add_#{name_singular}", v) }
  #Familia.info "#{self.index}: removing: #{removed}"
  removed.each { |v| self.send("remove_#{name_singular}", v) }
  true
end

end



121
# File 'lib/familia/types/unsorted_set.rb', line 121

Familia::RedisType.register self, :set

#sizeObject Also known as: length



5
6
7
# File 'lib/familia/types/unsorted_set.rb', line 5

def size
  redis.scard rediskey
end