Class: EasyRedis::Collection

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

Overview

class representing a generic collection

Instance Method Summary collapse

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.

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



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

#eachObject

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

#inspectObject



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