lib/redis/list.rb in redis-objects-0.1.0 vs lib/redis/list.rb in redis-objects-0.1.1

- old
+ new

@@ -2,75 +2,81 @@ # # Class representing a Redis list. Instances of Redis::List are designed to # behave as much like Ruby arrays as possible. # class List + require 'enumerator' + include Enumerable + attr_reader :key, :options, :redis - def initialize(key, options={}) + def initialize(key, redis=$redis, options={}) @key = key + @redis = redis @options = options - @redis = options[:redis] || $redis || Redis::Objects.redis - @options[:start] ||= 0 - @options[:type] ||= @options[:start] == 0 ? :increment : :decrement - @redis.setnx(key, @options[:start]) unless @options[:start] == 0 || @options[:init] === false end - + + # Works like push. Can chain together: list << 'a' << 'b' def <<(value) push(value) + self # for << 'a' << 'b' end - + + # Add a member to the end of the list. Redis: RPUSH def push(value) redis.rpush(key, value) - @values << value end + # Remove a member from the end of the list. Redis: RPOP def pop redis.rpop(key) - @values.pop end + # Add a member to the start of the list. Redis: LPUSH def unshift(value) redis.lpush(key, value) - @values.unshift value end + # Remove a member from the start of the list. Redis: LPOP def shift redis.lpop(key) end + # Return all values in the list. Redis: LRANGE(0,-1) def values - @values ||= get + range(0, -1) end + alias_method :get, :values - def value=(val) - redis.set(key, val) - @values = val - end - - def get - @values = range(0, -1) - end - - def [](index) - case index - when Range + # Same functionality as Ruby arrays. If a single number is given, return + # just the element at that index using Redis: LINDEX. Otherwise, return + # a range of values using Redis: LRANGE. + def [](index, length=nil) + if index.is_a? Range range(index.first, index.last) + elsif length + range(index, length) else - range(index, index) + at(index) end end def delete(name, count=0) - redis.lrem(name, count) + redis.lrem(key, count, name) # weird api + get end + def each(&block) + values.each(&block) + end + def range(start_index, end_index) redis.lrange(key, start_index, end_index) end - + + # Return the value at the given index. def at(index) - redis.lrange(key, index, index) + redis.lindex(key, index) end def last redis.lrange(key, -1, -1) end @@ -78,18 +84,22 @@ def clear redis.del(key) end def length - redis.length + redis.llen(key) end alias_method :size, :length def empty? - values.empty? + length == 0 end - + def ==(x) values == x + end + + def to_s + values.join(', ') end end end \ No newline at end of file