Class: Ohm::List

Inherits:
Object
  • Object
show all
Includes:
Collection
Defined in:
lib/ohm.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Collection

#each, #empty?, #fetch, #to_a, #to_json

Constructor Details

#initialize(key, namespace, model) ⇒ List

Returns a new instance of List.



168
169
170
171
172
# File 'lib/ohm.rb', line 168

def initialize(key, namespace, model)
  @key = key
  @namespace = namespace
  @model = model
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



164
165
166
# File 'lib/ohm.rb', line 164

def key
  @key
end

#modelObject (readonly)

Returns the value of attribute model.



166
167
168
# File 'lib/ohm.rb', line 166

def model
  @model
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



165
166
167
# File 'lib/ohm.rb', line 165

def namespace
  @namespace
end

Instance Method Details

#delete(model) ⇒ Object

Delete a model from the list.

Note: If your list contains the model multiple times, this method will delete all instances of that model in one go.

Example:

class Comment < Ohm::Model
end

class Post < Ohm::Model
list :comments, :Comment
end

p = Post.create
c = Comment.create

p.comments.push(c)
p.comments.push(c)

p.comments.delete(c)

p.comments.size == 0
# => true


291
292
293
294
295
296
# File 'lib/ohm.rb', line 291

def delete(model)

  # LREM key 0 <id> means remove all elements matching <id>
  # @see http://redis.io/commands/lrem
  key.call("LREM", 0, model.id)
end

#firstObject

Returns the first element of the list using LINDEX.



180
181
182
# File 'lib/ohm.rb', line 180

def first
  model[key.call("LINDEX", 0)]
end

#idsObject

Returns an array with all the ID's of the list.

class Comment < Ohm::Model
end

class Post < Ohm::Model
list :comments, :Comment
end

post = Post.create
post.comments.push(Comment.create)
post.comments.push(Comment.create)
post.comments.push(Comment.create)

post.comments.map(&:id)
# => ["1", "2", "3"]

post.comments.ids
# => ["1", "2", "3"]


318
319
320
# File 'lib/ohm.rb', line 318

def ids
  key.call("LRANGE", 0, -1)
end

#include?(model) ⇒ Boolean

Checks if the model is part of this List.

An important thing to note is that this method loads all of the elements of the List since there is no command in Redis that allows you to actually check the list contents efficiently.

You may want to avoid doing this if your list has say, 10K entries.

Returns:

  • (Boolean)


224
225
226
# File 'lib/ohm.rb', line 224

def include?(model)
  ids.include?(model.id)
end

#lastObject

Returns the last element of the list using LINDEX.



185
186
187
# File 'lib/ohm.rb', line 185

def last
  model[key.call("LINDEX", -1)]
end

#push(model) ⇒ Object

Pushes the model to the end of the list using RPUSH.



257
258
259
# File 'lib/ohm.rb', line 257

def push(model)
  key.call("RPUSH", model.id)
end

#range(start, stop) ⇒ Object

Returns an array of elements from the list using LRANGE. #range receives 2 integers, start and stop

Example:

class Comment < Ohm::Model
end

class Post < Ohm::Model
list :comments, :Comment
end

c1 = Comment.create
c2 = Comment.create
c3 = Comment.create

post = Post.create

post.comments.push(c1)
post.comments.push(c2)
post.comments.push(c3)

[c1, c2] == post.comments.range(0, 1)
# => true


213
214
215
# File 'lib/ohm.rb', line 213

def range(start, stop)
  fetch(key.call("LRANGE", start, stop))
end

#replace(models) ⇒ Object

Replace all the existing elements of a list with a different collection of models. This happens atomically in a MULTI-EXEC block.

Example:

user = User.create
p1 = Post.create
user.posts.push(p1)

p2, p3 = Post.create, Post.create
user.posts.replace([p2, p3])

user.posts.include?(p1)
# => false


244
245
246
247
248
249
250
251
252
253
254
# File 'lib/ohm.rb', line 244

def replace(models)
  ids = models.map(&:id)

  model.synchronize do
    redis.queue("MULTI")
    redis.queue("DEL", key)
    ids.each { |id| redis.queue("RPUSH", key, id) }
    redis.queue("EXEC")
    redis.commit
  end
end

#sizeObject

Returns the total size of the list using LLEN.



175
176
177
# File 'lib/ohm.rb', line 175

def size
  key.call("LLEN")
end

#unshift(model) ⇒ Object

Pushes the model to the beginning of the list using LPUSH.



262
263
264
# File 'lib/ohm.rb', line 262

def unshift(model)
  key.call("LPUSH", model.id)
end