Class: TinyFakeRedis

Inherits:
Object
  • Object
show all
Defined in:
lib/tiny_fake_redis.rb,
lib/tiny_fake_redis/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"0.1.0"

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missingObject



96
97
98
# File 'lib/tiny_fake_redis.rb', line 96

def method_missing(*)
  return nil
end

Instance Method Details

#dataObject



8
9
10
# File 'lib/tiny_fake_redis.rb', line 8

def data
  @data ||= {}
end

#decr(key) ⇒ Object



17
18
19
20
# File 'lib/tiny_fake_redis.rb', line 17

def decr(key)
  data[key] ||= 0
  data[key] -= 1
end

#del(*keys) ⇒ Object



89
90
91
92
93
94
# File 'lib/tiny_fake_redis.rb', line 89

def del(*keys)
  keys.flatten!
  i = 0
  keys.map {|k| data.delete(k) && (i += 1) }
  i
end

#get(key) ⇒ Object



26
27
28
# File 'lib/tiny_fake_redis.rb', line 26

def get(key)
  data[key].dup
end

#incr(key) ⇒ Object



12
13
14
15
# File 'lib/tiny_fake_redis.rb', line 12

def incr(key)
  data[key] ||= 0
  data[key] += 1
end

#keys(pattern = ".*") ⇒ Object



85
86
87
# File 'lib/tiny_fake_redis.rb', line 85

def keys(pattern = ".*")
  data.keys.filter {|k| k.to_s =~ %r{^#{pattern}$}}
end

#lpush(key, val) ⇒ Object



30
31
32
33
34
# File 'lib/tiny_fake_redis.rb', line 30

def lpush(key, val)
  require_array(key)
  data[key].unshift val
  data[key].size
end

#lrange(key, start_idx, end_idx = -1)) ⇒ Object



58
59
60
61
# File 'lib/tiny_fake_redis.rb', line 58

def lrange(key, start_idx, end_idx = -1)
  require_array(key)
  data[key][start_idx..end_idx] || []
end

#lrem(key, count, val) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/tiny_fake_redis.rb', line 36

def lrem(key, count, val)
  require_array(key)
  arr = get(key)
  removed = 0

  if count == 0
    removed = (arr.select {|v| v == val}).size
    arr.select! {|v| v != val}
  else
    arr.reverse! if count < 0

    count.abs.times do
      removed += 1 unless arr.delete_at(arr.index(val) || arr.size).nil?
    end

    arr.reverse! if count < 0
  end

  data[key] = arr
  removed
end

#rpop(key) ⇒ Object



74
75
76
77
# File 'lib/tiny_fake_redis.rb', line 74

def rpop(key)
  require_array(key)
  data[key].pop
end

#rpoplpush(one, two) ⇒ Object



79
80
81
82
83
# File 'lib/tiny_fake_redis.rb', line 79

def rpoplpush(one, two)
  val = rpop(one)
  lpush two, val
  val
end

#rpush(key, val) ⇒ Object



63
64
65
66
67
# File 'lib/tiny_fake_redis.rb', line 63

def rpush(key, val)
  require_array(key)
  data[key] << val
  data[key].size
end

#rpushx(key, val) ⇒ Object



69
70
71
72
# File 'lib/tiny_fake_redis.rb', line 69

def rpushx(key, val)
  return 0 unless data[key]
  rpush key, val
end

#set(key, val) ⇒ Object



22
23
24
# File 'lib/tiny_fake_redis.rb', line 22

def set(key, val)
  data[key] = val
end