Class: Mechanize::History

Inherits:
Array
  • Object
show all
Defined in:
lib/mechanize/history.rb

Overview

This class manages history for your mechanize object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_size = nil) ⇒ History

Returns a new instance of History.



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

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

Instance Attribute Details

#max_sizeObject

Returns the value of attribute max_size.



6
7
8
# File 'lib/mechanize/history.rb', line 6

def max_size
  @max_size
end

Instance Method Details

#clearObject



50
51
52
53
# File 'lib/mechanize/history.rb', line 50

def clear
  @history_index.clear
  super
end

#initialize_copy(orig) ⇒ Object



13
14
15
16
# File 'lib/mechanize/history.rb', line 13

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

#inspectObject

:nodoc:



18
19
20
21
22
# File 'lib/mechanize/history.rb', line 18

def inspect # :nodoc:
  uris = map { |page| page.uri }.join ', '

  "[#{uris}]"
end

#popObject



66
67
68
69
70
71
# File 'lib/mechanize/history.rb', line 66

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

#push(page, uri = nil) ⇒ Object Also known as: <<



24
25
26
27
28
29
30
31
32
33
# File 'lib/mechanize/history.rb', line 24

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

#shiftObject



55
56
57
58
59
60
61
62
63
64
# File 'lib/mechanize/history.rb', line 55

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

  super

  remove_from_index(page)
  page
end

#visited?(uri) ⇒ Boolean Also known as: visited_page

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
46
# File 'lib/mechanize/history.rb', line 37

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