Class: Dommy::Rack::History
- Inherits:
-
Object
- Object
- Dommy::Rack::History
- 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
-
#back ⇒ Object
Move the cursor back one entry and return that URL, or nil at the start.
- #current ⇒ Object
- #entries ⇒ Object
-
#forward ⇒ Object
Move the cursor forward one entry and return that URL, or nil at the end.
-
#initialize ⇒ History
constructor
A new instance of History.
- #push(url) ⇒ Object
Constructor Details
#initialize ⇒ History
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
#back ⇒ Object
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 |
#current ⇒ Object
36 37 38 |
# File 'lib/dommy/rack/history.rb', line 36 def current @stack[@index] if @index >= 0 end |
#entries ⇒ Object
40 41 42 |
# File 'lib/dommy/rack/history.rb', line 40 def entries @stack.dup end |
#forward ⇒ Object
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 |