Class: Innodb::List::ListCursor

Inherits:
Object
  • Object
show all
Defined in:
lib/innodb/list.rb

Overview

A list iteration cursor used primarily by the Innodb::List #cursor method implicitly. Keeps its own state for iterating through lists efficiently.

Instance Method Summary collapse

Constructor Details

#initialize(list, node = nil) ⇒ ListCursor

Returns a new instance of ListCursor.



139
140
141
142
# File 'lib/innodb/list.rb', line 139

def initialize(list, node=nil)
  @list   = list
  @cursor = node
end

Instance Method Details

#nextObject

Return the next entry from the current position, and advance the cursor position to the returned entry. If the cursor is currently nil, return the first entry in the list and adjust the cursor position to that entry.



167
168
169
170
171
172
173
# File 'lib/innodb/list.rb', line 167

def next
  if @cursor
    @cursor = @list.next(@cursor)
  else
    @cursor = @list.first
  end
end

#prevObject

Return the previous entry from the current position, and advance the cursor position to the returned entry. If the cursor is currently nil, return the last entry in the list and adjust the cursor position to that entry.



155
156
157
158
159
160
161
# File 'lib/innodb/list.rb', line 155

def prev
  if @cursor
    @cursor = @list.prev(@cursor)
  else
    @cursor = @list.last
  end
end

#resetObject

Reset the list cursor to its default starting state, which will allow iteration forwards from the first entry (using #next) or backwards from the last entry (using #prev).



147
148
149
# File 'lib/innodb/list.rb', line 147

def reset
  @cursor = nil
end