Class Ohm::List
In: lib/ohm.rb
Parent: Object

Methods

delete   first   ids   include?   last   new   push   range   replace   size   unshift  

Included Modules

Collection

Attributes

key  [R] 
model  [R] 
namespace  [R] 

Public Class methods

Public Instance methods

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

Returns the first element of the list using LINDEX.

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"]

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 the last element of the list using LINDEX.

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

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

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

Returns the total size of the list using LLEN.

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

[Validate]