Class: Dommy::Rack::History

Inherits:
Object
  • Object
show all
Defined in:
lib/dommy/rack/history.rb

Overview

Browser-tab-style navigation history: an ordered stack of visited URLs with a cursor. Visiting a new URL truncates any forward entries.

Instance Method Summary collapse

Constructor Details

#initializeHistory

Returns a new instance of History.



8
9
10
11
# File 'lib/dommy/rack/history.rb', line 8

def initialize
  @stack = []
  @index = -1
end

Instance Method Details

#backObject

Move the cursor back one entry and return that URL, or nil at the start.



21
22
23
24
25
26
# File 'lib/dommy/rack/history.rb', line 21

def back
  return nil if @index <= 0

  @index -= 1
  current
end

#currentObject



36
37
38
# File 'lib/dommy/rack/history.rb', line 36

def current
  @stack[@index] if @index >= 0
end

#entriesObject



40
41
42
# File 'lib/dommy/rack/history.rb', line 40

def entries
  @stack.dup
end

#forwardObject

Move the cursor forward one entry and return that URL, or nil at the end.



29
30
31
32
33
34
# File 'lib/dommy/rack/history.rb', line 29

def forward
  return nil if @index >= @stack.size - 1

  @index += 1
  current
end

#push(url) ⇒ Object



13
14
15
16
17
18
# File 'lib/dommy/rack/history.rb', line 13

def push(url)
  kept = @index >= 0 ? @stack[0..@index] : []
  @stack = kept + [url]
  @index = @stack.size - 1
  url
end