Class: RedisArray

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/redis_array.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key = nil) ⇒ RedisArray

Returns a new instance of RedisArray.



30
31
32
# File 'lib/redis_array.rb', line 30

def initialize(key = nil)
  @key = key.nil? ? generated_key : key
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



6
7
8
# File 'lib/redis_array.rb', line 6

def key
  @key
end

Class Method Details

.get(key) ⇒ Object



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

def self.get(key)
  raise NoRedisConnectionError if !defined?(@@redis) || @@redis.nil?
  RedisArray.new(key)
end

.namespaceObject



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

def self.namespace
  (defined?(@@namespace) && !@@namespace.nil?) ? @@namespace : "redisarray"
end

.namespace=(namespace) ⇒ Object



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

def self.namespace=(namespace)
  @@namespace = namespace
end

.namespaced_key_for(key) ⇒ Object

– Storage helpers



151
152
153
# File 'lib/redis_array.rb', line 151

def self.namespaced_key_for(key)
  "#{RedisArray.namespace}:#{key}"
end

.redisObject



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

def self.redis
  @@redis
end

.redis=(redis) ⇒ Object



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

def self.redis=(redis)
  raise InvalidRedisInstanceError unless redis.is_a?(Redis)
  @@redis = redis
end

Instance Method Details

#+(value) ⇒ Object



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

def +(value)
  if value.is_a?(Array)
    value.each do |item|
      push(item)
    end
  else
    push(value)
  end
end

#<<(value) ⇒ Object



99
100
101
# File 'lib/redis_array.rb', line 99

def <<(value)
  push(value)
end

#==(comp) ⇒ Object

– Comparison In the case where comp is another RedisArray object we just compare the key We don’t want to be comparing redis values because the results will be the same if the keys match



68
69
70
# File 'lib/redis_array.rb', line 68

def ==(comp)
  comp.is_a?(RedisArray) ? @key == comp.key : to_a == comp
end

#[](index) ⇒ Object



45
46
47
48
49
# File 'lib/redis_array.rb', line 45

def [](index)
  value = @@redis.lindex(namespaced_key, index)
  return nil if value.nil?
  sublist?(value) ? RedisArray.new(remove_namespace(value)) : value
end

#[]=(index, value) ⇒ Object

– Modification



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/redis_array.rb', line 82

def []=(index, value)
  value = storable_value(value)

  if index > 0 && !@@redis.exists(namespaced_key)
    index.times { @@redis.rpush(namespaced_key, "") }
  end

  llen = @@redis.llen(namespaced_key)

  if index < llen
    @@redis.lset(namespaced_key, index, value)
  else
    (index - llen).times { @@redis.rpush(namespaced_key, "") }
    @@redis.rpush(namespaced_key, value)
  end
end

#allObject



51
52
53
54
55
56
57
58
# File 'lib/redis_array.rb', line 51

def all
  array = @@redis.lrange(namespaced_key, 0, @@redis.llen(namespaced_key))
  array.each_with_index do |value, i|
    array[i] = RedisArray.new(remove_namespace(value)) if sublist?(value)
  end

  array
end

#clearObject



146
147
148
# File 'lib/redis_array.rb', line 146

def clear
  @@redis.del(namespaced_key)
end

#countObject



60
61
62
# File 'lib/redis_array.rb', line 60

def count
  @@redis.llen(namespaced_key)
end

#delete(value) ⇒ Object



141
142
143
144
# File 'lib/redis_array.rb', line 141

def delete(value)
  value = value.is_a?(RedisArray) ? sublist_value(value) : value
  @@redis.lrem(namespaced_key, 0, value)
end

#delete_at(index) ⇒ Object

Redis doesn’t support delete at index, so we’re copying the values in redis, keeping all but the index we’re removing, deleting the list, and reloading it. Due to the complexity of this call it is recommended that you do not use it.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/redis_array.rb', line 124

def delete_at(index)
  len = count
  values = @@redis.lrange(namespaced_key, 0, len-1)
  @@redis.multi do
    new_values = []

    values.each_with_index do |value, i|
      new_values << value unless i == index
    end

    @@redis.del(namespaced_key)
    new_values.each do |value|
      @@redis.rpush(namespaced_key, value)
    end
  end
end

#eachObject

– Selection / Iteration



35
36
37
38
39
40
41
42
43
# File 'lib/redis_array.rb', line 35

def each
  @@redis.lrange(namespaced_key, 0, @@redis.llen(namespaced_key)).each do |value|
    if sublist?(value)
      yield RedisArray.new(remove_namespace(value))
    else
      yield value
    end
  end
end

#namespaced_keyObject



155
156
157
# File 'lib/redis_array.rb', line 155

def namespaced_key
  RedisArray.namespaced_key_for(@key)
end

#popObject



117
118
119
# File 'lib/redis_array.rb', line 117

def pop
  @@redis.ltrim(namespaced_key, 0, -2)
end

#push(value) ⇒ Object



113
114
115
# File 'lib/redis_array.rb', line 113

def push(value)
  @@redis.rpush(namespaced_key, storable_value(value))
end

#to_aObject

– Conversion This returns a raw, deep array



74
75
76
77
78
79
# File 'lib/redis_array.rb', line 74

def to_a
  array = all
  array.each_with_index do |value, i|
    array[i] = value.to_a if value.is_a?(RedisArray)
  end
end