Class: MockRedis::MultiDbWrapper

Inherits:
Object
  • Object
show all
Includes:
UndefRedisMethods
Defined in:
lib/mock_redis/multi_db_wrapper.rb

Instance Method Summary collapse

Methods included from UndefRedisMethods

included

Constructor Details

#initialize(db) ⇒ MultiDbWrapper

Returns a new instance of MultiDbWrapper.



7
8
9
10
11
12
13
14
# File 'lib/mock_redis/multi_db_wrapper.rb', line 7

def initialize(db)
  @db_index = 0

  @prototype_db = db.clone

  @databases = Hash.new { |h, k| h[k] = @prototype_db.clone }
  @databases[@db_index] = db
end

Instance Method Details

#flushallObject

Redis commands



33
34
35
36
# File 'lib/mock_redis/multi_db_wrapper.rb', line 33

def flushall
  @databases.each_value(&:flushdb)
  'OK'
end

#initialize_copy(source) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/mock_redis/multi_db_wrapper.rb', line 24

def initialize_copy(source)
  super
  @databases = @databases.clone
  @databases.each_key do |k|
    @databases[k] = @databases[k].clone
  end
end

#move(key, db_index) ⇒ Object



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
# File 'lib/mock_redis/multi_db_wrapper.rb', line 38

def move(key, db_index)
  src = current_db
  dest = db(db_index)

  if !src.exists?(key) || dest.exists?(key)
    false
  else
    case current_db.type(key)
    when 'hash'
      dest.hmset(key, *src.hgetall(key).map { |k, v| [k, v] }.flatten)
    when 'list'
      while value = src.rpop(key)
        dest.lpush(key, value)
      end
    when 'set'
      while value = src.spop(key)
        dest.sadd(key, value)
      end
    when 'string'
      dest.set(key, src.get(key))
    when 'zset'
      src.zrange(key, 0, -1, :with_scores => true).each do |(m, s)|
        dest.zadd(key, s, m)
      end
    else
      raise ArgumentError,
      "Can't move a key of type #{current_db.type(key).inspect}"
    end

    src.del(key)
    true
  end
end

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

Returns:

  • (Boolean)


16
17
18
# File 'lib/mock_redis/multi_db_wrapper.rb', line 16

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

#select(db_index) ⇒ Object



72
73
74
75
# File 'lib/mock_redis/multi_db_wrapper.rb', line 72

def select(db_index)
  @db_index = db_index.to_i
  'OK'
end