File: history.rb

package info (click to toggle)
ruby-mechanize 2.7.6-1%2Bdeb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,480 kB
  • sloc: ruby: 11,380; makefile: 5; sh: 4
file content (82 lines) | stat: -rw-r--r-- 1,254 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
##
# This class manages history for your mechanize object.

class Mechanize::History < Array

  attr_accessor :max_size

  def initialize(max_size = nil)
    @max_size       = max_size
    @history_index  = {}
  end

  def initialize_copy(orig)
    super
    @history_index = orig.instance_variable_get(:@history_index).dup
  end

  def inspect # :nodoc:
    uris = map(&:uri).join ', '

    "[#{uris}]"
  end

  def push(page, uri = nil)
    super page

    index = uri ? uri : page.uri
    @history_index[index.to_s] = page

    shift while length > @max_size if @max_size

    self
  end

  alias :<< :push

  def visited? uri
    page = @history_index[uri.to_s]

    return page if page # HACK

    uri = uri.dup
    uri.path = '/' if uri.path.empty?

    @history_index[uri.to_s]
  end

  alias visited_page visited?

  def clear
    @history_index.clear
    super
  end

  def shift
    return nil if length == 0
    page    = self[0]
    self[0] = nil

    super

    remove_from_index(page)
    page
  end

  def pop
    return nil if length == 0
    page = super
    remove_from_index(page)
    page
  end

  private

  def remove_from_index(page)
    @history_index.each do |k,v|
      @history_index.delete(k) if v == page
    end
  end

end