Class: Innodb::List::Cursor

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) ⇒ Cursor

Returns a new instance of Cursor.



121
122
123
124
# File 'lib/innodb/list.rb', line 121

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.



149
150
151
152
153
154
155
# File 'lib/innodb/list.rb', line 149

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.



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

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).



129
130
131
# File 'lib/innodb/list.rb', line 129

def reset
  @cursor = nil
end