Class: Sohm::List
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
Instance Method Summary collapse
-
#delete(model) ⇒ Object
Delete a model from the list.
-
#first ⇒ Object
Returns the first element of the list using LINDEX.
-
#ids ⇒ Object
Returns an array with all the ID’s of the list.
-
#include?(model) ⇒ Boolean
Checks if the model is part of this List.
-
#initialize(key, namespace, model) ⇒ List
constructor
A new instance of List.
-
#last ⇒ Object
Returns the last element of the list using LINDEX.
-
#push(model) ⇒ Object
Pushes the model to the end of the list using RPUSH.
-
#range(start, stop) ⇒ Object
Returns an array of elements from the list using LRANGE.
-
#size ⇒ Object
Returns the total size of the list using LLEN.
-
#unshift(model) ⇒ Object
Pushes the model to the beginning of the list using LPUSH.
Methods included from Collection
#each, #empty?, #fetch, #to_a, #to_json
Constructor Details
#initialize(key, namespace, model) ⇒ List
Returns a new instance of List.
186 187 188 189 190 |
# File 'lib/sohm.rb', line 186 def initialize(key, namespace, model) @key = key @namespace = namespace @model = model end |
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
182 183 184 |
# File 'lib/sohm.rb', line 182 def key @key end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
184 185 186 |
# File 'lib/sohm.rb', line 184 def model @model end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
183 184 185 |
# File 'lib/sohm.rb', line 183 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 < Sohm::Model
end
class Post < Sohm::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
281 282 283 284 285 |
# File 'lib/sohm.rb', line 281 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 |
#first ⇒ Object
Returns the first element of the list using LINDEX.
198 199 200 |
# File 'lib/sohm.rb', line 198 def first model[redis.call("LINDEX", key, 0)] end |
#ids ⇒ Object
Returns an array with all the ID’s of the list.
class Comment < Sohm::Model
end
class Post < Sohm::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"]
307 308 309 |
# File 'lib/sohm.rb', line 307 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.
242 243 244 |
# File 'lib/sohm.rb', line 242 def include?(model) ids.include?(model.id) end |
#last ⇒ Object
Returns the last element of the list using LINDEX.
203 204 205 |
# File 'lib/sohm.rb', line 203 def last model[redis.call("LINDEX", key, -1)] end |
#push(model) ⇒ Object
Pushes the model to the end of the list using RPUSH.
247 248 249 |
# File 'lib/sohm.rb', line 247 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 < Sohm::Model
end
class Post < Sohm::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
231 232 233 |
# File 'lib/sohm.rb', line 231 def range(start, stop) fetch(redis.call("LRANGE", key, start, stop)) end |
#size ⇒ Object
Returns the total size of the list using LLEN.
193 194 195 |
# File 'lib/sohm.rb', line 193 def size redis.call("LLEN", key) end |
#unshift(model) ⇒ Object
Pushes the model to the beginning of the list using LPUSH.
252 253 254 |
# File 'lib/sohm.rb', line 252 def unshift(model) redis.call("LPUSH", key, model.id) end |