Class: Navigator

Inherits:
Object
  • Object
show all
Defined in:
lib/espnscrape/Navigator.rb

Overview

Array Navigator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(list) ⇒ Navigator

Store array and initialize cursor



7
8
9
10
11
# File 'lib/espnscrape/Navigator.rb', line 7

def initialize(list)
  @list    = list
  @bounds  = [-1, list.size]
  @cursor  = -1
end

Instance Attribute Details

#listObject (readonly)

Array of data to Navigate



4
5
6
# File 'lib/espnscrape/Navigator.rb', line 4

def list
  @list
end

Instance Method Details

#[](idx = nil) ⇒ Element

Updates the navigation cursor if out of bounds. Returns the item at the given location. Returns the underlying array if no index is given.



34
35
36
37
38
39
# File 'lib/espnscrape/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[Object]

Return the player located at the current navigation cursor



15
16
17
# File 'lib/espnscrape/Navigator.rb', line 15

def curr
  self[@cursor] if @cursor >= 0
end

#firstObject

Updates the cursor and returns the first element of the array



47
48
49
# File 'lib/espnscrape/Navigator.rb', line 47

def first
  self[@cursor = 0]
end

#inbounds?(idx) ⇒ Boolean

Checks if the requested index is within the array bounderies



42
43
44
# File 'lib/espnscrape/Navigator.rb', line 42

def inbounds?(idx)
  (@bounds[0]..@bounds[1]).cover?(idx)
end

#lastObject

Updates the cursor and returns the last element of the array



52
53
54
# File 'lib/espnscrape/Navigator.rb', line 52

def last
  self[@cursor = @list.size - 1]
end

#next[Object]

Increments the navigation cursor and return the item at that location



21
22
23
# File 'lib/espnscrape/Navigator.rb', line 21

def next
  self[(@cursor += 1)]
end

#prev[Object]

Decrements the navigation cursor and return the item at that location



27
28
29
# File 'lib/espnscrape/Navigator.rb', line 27

def prev
  self[(@cursor -= 1)]
end

#sizeObject

Returns the size of the underlying array



57
58
59
# File 'lib/espnscrape/Navigator.rb', line 57

def size
  @list.size
end