Class: EasyRedis::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/easyredis.rb

Overview

class representing a generic collection

Direct Known Subclasses

Sort

Instance Method Summary collapse

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.

Parameters:

  • index (Range, Number)

    either a number corresponding to a specific element, a range corresponding to a range of elements, or a number indicating an offset, if limit is also specified

  • limit (Number) (defaults to: nil)

    index is interpreted as an offset and limit is the number of elements to return from that offset



120
121
122
123
124
125
126
127
128
129
# File 'lib/easyredis.rb', line 120

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

#eachObject

iterate through all members of this collection



132
133
134
# File 'lib/easyredis.rb', line 132

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



137
138
139
140
141
142
143
# File 'lib/easyredis.rb', line 137

def first(n = nil)
  if n
    self[0,n]
  else
    self[0]
  end
end

#last(n = nil) ⇒ Object

return the last element of this collection, or the last n elements, if n is given



146
147
148
149
150
151
152
# File 'lib/easyredis.rb', line 146

def last(n = nil)
  if n
    self[-n..-1]
  else
    self[-1]
  end
end