class Navigator
Attributes
list[R]
Array
of data to Navigate
Public Class Methods
new(list)
click to toggle source
Store array and initialize cursor
# File lib/hoopscrape/Navigator.rb, line 7 def initialize(list) @list = list @bounds = [-1, list.size] @cursor = -1 end
Public Instance Methods
[](idx = nil)
click to toggle source
Updates the navigation cursor if out of bounds. Returns the item at the given location. Returns the underlying array if no index is given. @return [Element] Array
Element or Array
# File lib/hoopscrape/Navigator.rb, line 34 def [](idx = nil) return @list if idx.nil? @cursor = @bounds[0] if @cursor < @bounds[0] @cursor = @bounds[1] if @cursor > @bounds[1] @list[idx] if [inbounds?(idx), !@list.nil?].all? end
curr()
click to toggle source
Return the player located at the current navigation cursor @return [[Object]] Array
Element
# File lib/hoopscrape/Navigator.rb, line 15 def curr self[@cursor] if @cursor >= 0 end
first()
click to toggle source
Updates the cursor and returns the first element of the array
# File lib/hoopscrape/Navigator.rb, line 47 def first self[@cursor = 0] end
inbounds?(idx)
click to toggle source
Checks if the requested index is within the array bounderies
# File lib/hoopscrape/Navigator.rb, line 42 def inbounds?(idx) (@bounds[0]..@bounds[1]).cover?(idx) end
last()
click to toggle source
Updates the cursor and returns the last element of the array
# File lib/hoopscrape/Navigator.rb, line 52 def last self[@cursor = @list.size - 1] end
next()
click to toggle source
Increments the navigation cursor and return the item at that location @return (see curr
)
# File lib/hoopscrape/Navigator.rb, line 21 def next self[(@cursor += 1)] end
prev()
click to toggle source
Decrements the navigation cursor and return the item at that location @return (see curr
)
# File lib/hoopscrape/Navigator.rb, line 27 def prev self[(@cursor -= 1)] end
size()
click to toggle source
Returns the size of the underlying array
# File lib/hoopscrape/Navigator.rb, line 57 def size @list.size end