Class: RedisObj::List

Inherits:
Base
  • Object
show all
Includes:
Enumerable
Defined in:
lib/redis_obj/list.rb

Instance Attribute Summary

Attributes inherited from Base

#key, #redis

Instance Method Summary collapse

Methods inherited from Base

#==, #clear, #del_key, #get_keys, #initialize

Constructor Details

This class inherits a constructor from RedisObj::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *arguments, &blk) ⇒ Object

Make future proof for new versions of redis



83
84
85
86
87
88
89
90
91
# File 'lib/redis_obj/list.rb', line 83

def method_missing method, *arguments, &blk
  if redis.respond_to?(method)
    # If its a method available to redis just pass it along with the key
    # as the first argument
    redis.__send__(method,key,*arguments,&blk)
  else
    super
  end
end

Instance Method Details

#drop(n) ⇒ Object



64
65
66
67
# File 'lib/redis_obj/list.rb', line 64

def drop n
  redis.ltrim(key,n,-1)
  self
end

#first(num = 1) ⇒ Object



4
5
6
# File 'lib/redis_obj/list.rb', line 4

def first num=1
  lrange(0,num-1)
end

#include?(val) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
# File 'lib/redis_obj/list.rb', line 59

def include?(val)
  warn('Calling include on redis list, must pull down entire list to process')
  !!find {|v| v == val}
end

#last(num = 1) ⇒ Object



12
13
14
# File 'lib/redis_obj/list.rb', line 12

def last num=1
  lrange(-1*num,-1)
end

#lindex(index) ⇒ Object Also known as: [], at



20
21
22
# File 'lib/redis_obj/list.rb', line 20

def lindex(index)
  redis.lindex(key,index)
end

#llenObject Also known as: length, size, count



31
32
33
# File 'lib/redis_obj/list.rb', line 31

def llen
  redis.llen(key)
end

#lpopObject Also known as: shift



38
39
40
# File 'lib/redis_obj/list.rb', line 38

def lpop
  redis.lpop(key)
end

#lpush(val) ⇒ Object Also known as: unshift



43
44
45
# File 'lib/redis_obj/list.rb', line 43

def lpush val
  redis.lpush(key,val)
end

#lset(index, value) ⇒ Object Also known as: []=



26
27
28
# File 'lib/redis_obj/list.rb', line 26

def lset(index, value)
  redis.lset(key, index, value)
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/redis_obj/list.rb', line 93

def respond_to_missing?(method, include_private = false)
  redis.respond_to?(method) || super
end

#rpopObject Also known as: pop



48
49
50
# File 'lib/redis_obj/list.rb', line 48

def rpop
  redis.rpop(key)
end

#rpush(val) ⇒ Object Also known as: <<, push



53
54
55
# File 'lib/redis_obj/list.rb', line 53

def rpush val
  redis.rpush(key,val)
end

#sample(n = 1) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/redis_obj/list.rb', line 69

def sample n=1
  cnt = llen
  if n==1
    lindex(rand(cnt))
  else
    arr = []
    n.times do
      arr << lindex(rand(cnt))
    end
    arr
  end
end

#take(n) ⇒ Object



8
9
10
# File 'lib/redis_obj/list.rb', line 8

def take n
  first(n)
end

#to_aObject



16
17
18
# File 'lib/redis_obj/list.rb', line 16

def to_a
  lrange(0,-1)
end