Module: Daikon::NamespaceTools

Included in:
Client
Defined in:
lib/daikon/namespace_tools.rb

Instance Method Summary collapse

Instance Method Details

#add_namespace(namespace, key) ⇒ Object



104
105
106
107
108
109
110
111
# File 'lib/daikon/namespace_tools.rb', line 104

def add_namespace(namespace, key)
  return key unless namespace

  case key
  when String then "#{namespace}:#{key}"
  when Array  then key.map {|k| add_namespace(namespace, k)}
  end
end

#denamespace_output(namespace, command, result) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/daikon/namespace_tools.rb', line 92

def denamespace_output(namespace, command, result)
  case command.to_s.downcase

  when "keys"
    remove_namespace namespace, result

  else
    result

  end
end

#namespace_input(ns, command, *args) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/daikon/namespace_tools.rb', line 3

def namespace_input(ns, command, *args)
  command = command.to_s.downcase

  case command

  when "multi", "exec", "discard"

    # No arguments.

    [ command ]

  when "exists", "del", "type", "keys", "ttl", "set", "get", "getset",
       "setnx", "incr", "incrby", "decr", "decrby", "rpush", "lpush",
       "llen", "lrange", "ltrim", "lindex", "lset", "lrem", "lpop", "rpop",
       "sadd", "srem", "spop", "scard", "sismember", "smembers", "srandmember",
       "zadd", "zrem", "zincrby", "zrange", "zrevrange", "zrangebyscore",
       "zcard", "zscore", "zremrangebyscore", "expire", "expireat", "hlen",
       "hkeys", "hvals", "hgetall", "hset", "hget", "hincrby", "hexists",
       "hdel", "hmset"

    # Only the first argument is a key.

    head = add_namespace(ns, args.first)
    tail = args[1, args.length - 1] || []

    [ command, head, *tail ]

  when "smove"

    # The first two parmeters are keys.

    result = [ command ]

    args.each_with_index do |arg, i|
      result << ((i == 0 || i == 1) ? add_namespace(ns, arg) : arg)
    end

    result

  when "mget", "rpoplpush", "sinter", "sunion", "sdiff", "info",
       "sinterstore", "sunionstore", "sdiffstore", "dbsize"

    # All arguments are keys.

    keys = add_namespace(ns, args)

    [ command, *keys ]

  when "mset", "msetnx"

    # Every other argument is a key, starting with the first.

    hash1 = Hash[*args]
    hash2 = {}

    hash1.each do |k, v|
      hash2[add_namespace(ns, k)] = hash1.delete(k)
    end

    [ command, hash2 ]

  when "sort"

    return [] if args.count == 0

    key = add_namespace(ns, args.shift)
    parms = {}

    while keyword = args.shift.andand.downcase
      case keyword
      when "by", "get", "store"
        k = keyword.intern
        v = add_namespace(ns, args.shift)

        parms[k] = v
      when "limit"
        parms[:limit] = [ args.shift.to_i, args.shift.to_i ]
      when "asc", "desc", "alpha"
        parms[:order].andand << " "
        parms[:order] ||= ""
        parms[:order] << keyword
      end
    end

    [ command, key, parms ]

  end
end

#remove_namespace(namespace, key) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/daikon/namespace_tools.rb', line 113

def remove_namespace(namespace, key)
  return key unless namespace

  case key
  when String then key.gsub(/^#{namespace}:/, "")
  when Array  then key.map {|k| remove_namespace(namespace, k)}
  end
end