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.


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

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

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key


159
160
161
# File 'lib/ohm.rb', line 159

def key
  @key
end

#modelObject (readonly)

Returns the value of attribute model


161
162
163
# File 'lib/ohm.rb', line 161

def model
  @model
end

#namespaceObject (readonly)

Returns the value of attribute namespace


160
161
162
# File 'lib/ohm.rb', line 160

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


286
287
288
289
290
291
# File 'lib/ohm.rb', line 286

def delete(model)

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

#firstObject

Returns the first element of the list using LINDEX.


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

def first
  model[redis.call("LINDEX", key, 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"]


313
314
315
# File 'lib/ohm.rb', line 313

def ids
  redis.call("LRANGE", key, 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)

219
220
221
# File 'lib/ohm.rb', line 219

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

#lastObject

Returns the last element of the list using LINDEX.


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

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

#push(model) ⇒ Object

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


252
253
254
# File 'lib/ohm.rb', line 252

def push(model)
  redis.call("RPUSH", key, 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


208
209
210
# File 'lib/ohm.rb', line 208

def range(start, stop)
  fetch(redis.call("LRANGE", key, 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


239
240
241
242
243
244
245
246
247
248
249
# File 'lib/ohm.rb', line 239

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.


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

def size
  redis.call("LLEN", key)
end

#unshift(model) ⇒ Object

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


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

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