lib/redis/list.rb in redis-objects-1.2.1 vs lib/redis/list.rb in redis-objects-1.3.0

- old
+ new

@@ -33,12 +33,20 @@ redis.ltrim(key, -options[:maxlength], -1) if options[:maxlength] end end # Remove a member from the end of the list. Redis: RPOP - def pop - unmarshal redis.rpop(key) + def pop(n=nil) + if n + result, = redis.multi do + redis.lrange(key, -n, -1) + redis.ltrim(key, 0, -n - 1) + end + unmarshal result + else + unmarshal redis.rpop(key) + end end # Atomically pops a value from one list, pushes to another and returns the # value. Destination can be a String or a Redis::List # @@ -58,20 +66,29 @@ redis.ltrim(key, 0, options[:maxlength] - 1) if options[:maxlength] end end # Remove a member from the start of the list. Redis: LPOP - def shift - unmarshal redis.lpop(key) + def shift(n=nil) + if n + result, = redis.multi do + redis.lrange(key, 0, n - 1) + redis.ltrim(key, n, -1) + end + unmarshal result + else + unmarshal redis.lpop(key) + end end # Return all values in the list. Redis: LRANGE(0,-1) def values vals = range(0, -1) vals.nil? ? [] : vals end alias_method :get, :values + alias_method :value, :values # 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) @@ -147,8 +164,12 @@ values == x end def to_s values.join(', ') + end + + def as_json(*) + to_hash end end end