Class: Innodb::List::ListCursor
- Inherits:
-
Object
- Object
- Innodb::List::ListCursor
- 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
-
#initialize(list, node = nil) ⇒ ListCursor
constructor
A new instance of ListCursor.
-
#next ⇒ Object
Return the next entry from the current position, and advance the cursor position to the returned entry.
-
#prev ⇒ Object
Return the previous entry from the current position, and advance the cursor position to the returned entry.
-
#reset ⇒ Object
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).
Constructor Details
#initialize(list, node = nil) ⇒ ListCursor
Returns a new instance of ListCursor.
137 138 139 140 |
# File 'lib/innodb/list.rb', line 137 def initialize(list, node=nil) @list = list @cursor = node end |
Instance Method Details
#next ⇒ Object
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.
165 166 167 168 169 170 171 |
# File 'lib/innodb/list.rb', line 165 def next if @cursor @cursor = @list.next(@cursor) else @cursor = @list.first end end |
#prev ⇒ Object
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.
153 154 155 156 157 158 159 |
# File 'lib/innodb/list.rb', line 153 def prev if @cursor @cursor = @list.prev(@cursor) else @cursor = @list.last end end |
#reset ⇒ Object
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).
145 146 147 |
# File 'lib/innodb/list.rb', line 145 def reset @cursor = nil end |