Class: EasyRedis::Collection
- Inherits:
-
Object
- Object
- EasyRedis::Collection
- Includes:
- Enumerable
- Defined in:
- lib/easyredis.rb
Overview
class representing a generic collection
Instance Method Summary collapse
-
#[](index, limit = nil) ⇒ Object
access elements in this sort.
-
#each ⇒ Object
iterate through all members of this collection.
-
#first(n = nil) ⇒ Object
return the fist element of this collection, or the first n elements, if n is given.
-
#initialize(&p) ⇒ Collection
constructor
A new instance of Collection.
- #inspect ⇒ Object
-
#last(n = nil) ⇒ Object
return the last element of this collection, or the last n elements, if n is given.
Constructor Details
#initialize(&p) ⇒ Collection
Returns a new instance of Collection.
121 122 123 |
# File 'lib/easyredis.rb', line 121 def initialize(&p) @access = p end |
Instance Method Details
#[](index, limit = nil) ⇒ Object
access elements in this sort
Work’s like an Array’s [] method. It can take a specific index, a range, or an offset and an amount/limit. This method uses the underlying access method, which handles the actual retrival.
135 136 137 138 139 140 141 142 143 144 |
# File 'lib/easyredis.rb', line 135 def [](index,limit=nil) if limit offset = index self[offset...(offset+limit)] elsif index.is_a? Range access(index) elsif index.is_a? Integer self[index..index].first end end |
#each ⇒ Object
iterate through all members of this collection
147 148 149 |
# File 'lib/easyredis.rb', line 147 def each self[0..-1].each { |o| yield o } end |
#first(n = nil) ⇒ Object
return the fist element of this collection, or the first n elements, if n is given
152 153 154 155 156 157 158 |
# File 'lib/easyredis.rb', line 152 def first(n = nil) if n self[0,n] else self[0] end end |
#inspect ⇒ Object
169 170 171 |
# File 'lib/easyredis.rb', line 169 def inspect "#<EasyRedis::Collection>" end |
#last(n = nil) ⇒ Object
return the last element of this collection, or the last n elements, if n is given
161 162 163 164 165 166 167 |
# File 'lib/easyredis.rb', line 161 def last(n = nil) if n self[-n..-1] else self[-1] end end |