vendor/bundle/ruby/1.8/gems/redis-namespace-1.0.3/lib/redis/namespace.rb
# The following table defines how input parameters and result
# values should be modified for the namespace.
#
# COMMANDS is a hash. Each key is the name of a command and each
# value is a two element array.
#
# The first element in the value array describes how to modify the
# arguments passed. It can be one of:
#
#   nil
#     Do nothing.
#   :first
#     Add the namespace to the first argument passed, e.g.
#       GET key => GET namespace:key
#   :all
#     Add the namespace to all arguments passed, e.g.
#       MGET key1 key2 => MGET namespace:key1 namespace:key2
#   :exclude_first
#     Add the namespace to all arguments but the first, e.g.
#   :exclude_last
#     Add the namespace to all arguments but the last, e.g.
#       BLPOP key1 key2 timeout =>
#       BLPOP namespace:key1 namespace:key2 timeout
#   :alternate
#     Add the namespace to every other argument, e.g.
#       MSET key1 value1 key2 value2 =>
#       MSET namespace:key1 value1 namespace:key2 value2
#
# The second element in the value array describes how to modify
# the return value of the Redis call. It can be one of:
#
#   nil
#     Do nothing.
#   :all
#     Add the namespace to all elements returned, e.g.
#       key1 key2 => namespace:key1 namespace:key2
The following table defines how input parameters and result
values should be modified for the namespace.

COMMANDS is a hash. Each key is the name of a command and each
value is a two element array.

The first element in the value array describes how to modify the
arguments passed. It can be one of.

nil
Do nothing.
:first
Add the namespace to the first argument passed, e.g.
GET key => GET namespace:key
:all
Add the namespace to all arguments passed, e.g.
MGET key1 key2 => MGET namespace:key1 namespace:key2
:exclude_first
Add the namespace to all arguments but the first, e.g.
:exclude_last
Add the namespace to all arguments but the last, e.g.
BLPOP key1 key2 timeout =>
BLPOP namespace:key1 namespace:key2 timeout
:alternate
Add the namespace to every other argument, e.g.
MSET key1 value1 key2 value2 =>
MSET namespace:key1 value1 namespace:key2 value2

The second element in the value array describes how to modify
the return value of the Redis call. It can be one of.

nil
Do nothing.
:all
Add the namespace to all elements returned, e.g.
key1 key2 => namespace:key1 namespace:key2
The following table defines how input parameters and result values should be modified for the namespace . COMMANDS is a hash . Each key is the name of a command and each value is a two element array . The first element in the value array describes how to modify the arguments passed . It can be one of . nil Do nothing . : first Add the namespace to the first argument passed e . g . GET key = > GET namespace : key : all Add the namespace to all arguments passed e . g . MGET key1 key2 = > MGET namespace : key1 namespace : key2 : exclude_first Add the namespace to all arguments but the first e . g . : exclude_last Add the namespace to all arguments but the last e . g . BLPOP key1 key2 timeout = > BLPOP namespace : key1 namespace : key2 timeout : alternate Add the namespace to every other argument e . g . MSET key1 value1 key2 value2 = > MSET namespace : key1 value1 namespace : key2 value2 The second element in the value array describes how to modify the return value of the Redis call . It can be one of . nil Do nothing . : all Add the namespace to all elements returned e . g . key1 key2 = > namespace : key1 namespace : key2
def method_missing(command, *args, &block)
      handling = COMMANDS[command.to_s] ||
        COMMANDS[ALIASES[command.to_s]]
      if handling.nil?
        return @redis.send(command, *args, &block)
      end
      (before, after) = handling
      case before
      when :first
        args[0] = add_namespace(args[0]) if args[0]
      when :all
        args = add_namespace(args)
      when :exclude_first
        first = args.shift
        args = add_namespace(args)
        args.unshift(first) if first
      when :exclude_last
        last = args.pop
        args = add_namespace(args)
        args.push(last) if last
      when :alternate
        args.each_with_index { |a, i| args[i] = add_namespace(a) if i.even? }
      when :sort
        args[0] = add_namespace(args[0]) if args[0]
        [:by, :get, :store].each do |key|
          args[1][key] = add_namespace(args[1][key]) if args[1][key]
        end if args[1].is_a?(Hash)
      end
      result = @redis.send(command, *args, &block)
      result = rem_namespace(result) if after == :all
      result
    end
def method_missing ( command , * args , & block ) handling = COMMANDS [ command . to_s ] || COMMANDS [ ALIASES [ command . to_s ] ] if handling . nil? return @redis . send ( command , * args , & block ) end ( before , after ) = handling case before when :first args [ 0 ] = add_namespace ( args [ 0 ] ) if args [ 0 ] when :all args = add_namespace ( args ) when :exclude_first first = args . shift args = add_namespace ( args ) args . unshift ( first ) if first when :exclude_last last = args . pop args = add_namespace ( args ) args . push ( last ) if last when :alternate args . each_with_index { | a , i | args [ i ] = add_namespace ( a ) if i . even? } when :sort args [ 0 ] = add_namespace ( args [ 0 ] ) if args [ 0 ] [ :by , :get , :store ] . each do | key | args [ 1 ] [ key ] = add_namespace ( args [ 1 ] [ key ] ) if args [ 1 ] [ key ] end if args [ 1 ] . is_a? ( Hash ) end result = @redis . send ( command , * args , & block ) result = rem_namespace ( result ) if after == :all result end
The following table defines how input parameters and result
values should be modified for the namespace.
The following table defines how input parameters and result values should be modified for the namespace .
# redis-namespace does not know how to handle this command. # Passing it to @redis as is. # Add the namespace to any parameters that are keys. # Dispatch the command to Redis and store the result. # Remove the namespace from results that are keys.
command
args
block