Class: RubyMVC::Toolkit::BrowserHistory

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_mvc/toolkit/browser_history.rb

Overview

This class provides a mechanism for tracking browser history operations where the underlying peer controls don’t do this well enough for our purposes (yes, I’m looking at you Wx::HtmlWindow…)

Instance Method Summary collapse

Constructor Details

#initialize(maxsize = 100) ⇒ BrowserHistory

Returns a new instance of BrowserHistory.



35
36
37
38
39
40
# File 'lib/ruby_mvc/toolkit/browser_history.rb', line 35

def initialize(maxsize = 100)
  @mutex = Mutex.new
  @history = []
  @maxsize = maxsize
  @index = 0
end

Instance Method Details

#<<(entry) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ruby_mvc/toolkit/browser_history.rb', line 42

def <<(entry)
  @mutex.synchronize {
    return if @history[-1] == entry
    if @history.size >= @maxsize
      @history.delete(0)
    else
      @history = @history[0..@index]
    end
    @history << entry if @history[-1] != entry
    @index = @history.size - 1
#        dump_history
  }
end

#currentObject



56
57
58
# File 'lib/ruby_mvc/toolkit/browser_history.rb', line 56

def current
  @mutex.synchronize { @history[@index] }
end

#each(&block) ⇒ Object



88
89
90
91
92
# File 'lib/ruby_mvc/toolkit/browser_history.rb', line 88

def each(&block)
  @mutex.synchronize {
    @history.each(&block)
  }
end

#has_next?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/ruby_mvc/toolkit/browser_history.rb', line 64

def has_next?
  @index < @history.size - 1
end

#has_prev?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/ruby_mvc/toolkit/browser_history.rb', line 60

def has_prev?
  @index > 0
end

#nextObject



78
79
80
81
82
83
84
85
86
# File 'lib/ruby_mvc/toolkit/browser_history.rb', line 78

def next
  @mutex.synchronize {
    if has_next?
      @index += 1
    end
#        dump_history
  }
  current
end

#prevObject



68
69
70
71
72
73
74
75
76
# File 'lib/ruby_mvc/toolkit/browser_history.rb', line 68

def prev
  @mutex.synchronize {
    if has_prev?
      @index -= 1
    end
#        dump_history
  }
  current
end

#reverse_each(&block) ⇒ Object



94
95
96
97
98
# File 'lib/ruby_mvc/toolkit/browser_history.rb', line 94

def reverse_each(&block)
  @mutex.synchronize {
    @history.reverse_each(&block)
  }
end