Class: Redpear::Store::List
- Inherits:
-
Enumerable
- Object
- Base
- Enumerable
- Redpear::Store::List
- Defined in:
- lib/redpear/store/list.rb
Constant Summary
Constants inherited from Base
Base::IS_NIL, Base::IS_ONE, Base::IS_TRUE, Base::IS_ZERO, Base::PICK_FIRST, Base::TO_INT, Base::TO_SET
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Comparator.
-
#all ⇒ Array
(also: #to_a)
All items.
-
#delete(item, count = 0) ⇒ Object
Remove ‘item` from list.
- #each {|item| ... } ⇒ Object
-
#insert_after(pivot, item) ⇒ Object
Insert ‘item` after `pivot`.
-
#insert_before(pivot, item) ⇒ Object
Insert ‘item` before `pivot`.
-
#length ⇒ Integer
The number of items in the set.
-
#pop ⇒ String
Removes the last item.
-
#pop_unshift(target) ⇒ Object
Removes the last item and prepends it to ‘target`.
-
#push(item) ⇒ Object
(also: #<<)
Appends a single item.
-
#range(start, finish) ⇒ Array
Items.
-
#shift ⇒ String
Removes the first item.
-
#slice(start, length = nil) ⇒ Object
(also: #[])
Returns a slice of the list.
-
#slice(start, length) ⇒ Object
Destructive slice.
- #trim(start, finish) ⇒ Object
-
#unshift(item) ⇒ Object
Prepends a single item.
Methods inherited from Enumerable
Methods inherited from Base
#clear, #exists?, #expire, #expire_at, #expire_in, #initialize, #inspect, #purge!, temporary, #ttl, #type, #value, #watch
Constructor Details
This class inherits a constructor from Redpear::Store::Base
Instance Method Details
#==(other) ⇒ Boolean
Comparator
114 115 116 |
# File 'lib/redpear/store/list.rb', line 114 def ==(other) other.respond_to?(:to_a) && other.to_a == to_a end |
#all ⇒ Array Also known as: to_a
Returns all items.
10 11 12 |
# File 'lib/redpear/store/list.rb', line 10 def all slice(0..-1) end |
#delete(item, count = 0) ⇒ Object
Remove ‘item` from list
125 126 127 |
# File 'lib/redpear/store/list.rb', line 125 def delete(item, count = 0) conn.lrem key, count, item end |
#each {|item| ... } ⇒ Object
5 6 7 |
# File 'lib/redpear/store/list.rb', line 5 def each(&block) all.each(&block) end |
#insert_after(pivot, item) ⇒ Object
Insert ‘item` after `pivot`
139 140 141 |
# File 'lib/redpear/store/list.rb', line 139 def insert_after(pivot, item) conn.linsert key, :after, pivot, item end |
#insert_before(pivot, item) ⇒ Object
Insert ‘item` before `pivot`
132 133 134 |
# File 'lib/redpear/store/list.rb', line 132 def insert_before(pivot, item) conn.linsert key, :before, pivot, item end |
#length ⇒ Integer
Returns the number of items in the set.
71 72 73 |
# File 'lib/redpear/store/list.rb', line 71 def length conn.llen key end |
#pop ⇒ String
Removes the last item
87 88 89 |
# File 'lib/redpear/store/list.rb', line 87 def pop conn.rpop key end |
#pop_unshift(target) ⇒ Object
Removes the last item and prepends it to ‘target`
107 108 109 |
# File 'lib/redpear/store/list.rb', line 107 def pop_unshift(target) conn.rpoplpush key, target.to_s end |
#push(item) ⇒ Object Also known as: <<
Appends a single item. Chainable example:
list << 'a' << 'b'
79 80 81 82 |
# File 'lib/redpear/store/list.rb', line 79 def push(item) conn.rpush key, item self end |
#range(start, finish) ⇒ Array
Returns items.
60 61 62 |
# File 'lib/redpear/store/list.rb', line 60 def range(start, finish) conn.lrange(key, start, finish) || [] end |
#shift ⇒ String
Removes the first item
101 102 103 |
# File 'lib/redpear/store/list.rb', line 101 def shift conn.lpop key end |
#slice(index) ⇒ String #slice(start, length) ⇒ Array #slice(range) ⇒ Array Also known as: []
Returns a slice of the list
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/redpear/store/list.rb', line 29 def slice(start, length = nil) case start when Integer if length range(start, start + length - 1) else conn.lindex(key, start) rescue nil end when Range, Array range *range_pair(start) else [] end end |
#slice(start, length) ⇒ Object
Destructive slice. Returns specified range and removes other items.
47 48 49 50 51 52 53 54 55 |
# File 'lib/redpear/store/list.rb', line 47 def slice!(start, length = nil) result = case start when Range, Array trim *range_pair(start) else trim(start, start + length - 1) end Redis::Future === result ? result : to_a end |
#trim(start, finish) ⇒ Object
66 67 68 |
# File 'lib/redpear/store/list.rb', line 66 def trim(start, finish) conn.ltrim(key, start, finish) end |
#unshift(item) ⇒ Object
Prepends a single item.
94 95 96 97 |
# File 'lib/redpear/store/list.rb', line 94 def unshift(item) conn.lpush key, item self end |