Class: WatirNokogiri::ElementCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/watir-nokogiri/element_collection.rb

Overview

Base class for element collections.

Instance Method Summary collapse

Constructor Details

#initialize(parent, selector) ⇒ ElementCollection

Returns a new instance of ElementCollection.



10
11
12
13
# File 'lib/watir-nokogiri/element_collection.rb', line 10

def initialize(parent, selector)
  @parent = parent
  @selector = selector
end

Instance Method Details

#[](idx) ⇒ WatirNokogiri::Element

Get the element at the given index. Note that this is 0-indexed.

Parameters:

  • idx (Fixnum)

    Index of wanted element, 0-indexed

Returns:



44
45
46
# File 'lib/watir-nokogiri/element_collection.rb', line 44

def [](idx)
  to_a[idx] || element_class.new(@parent, :index => idx)
end

#each {|element| ... } ⇒ Object

Yields each element in collection.

Yield Parameters:



21
22
23
# File 'lib/watir-nokogiri/element_collection.rb', line 21

def each(&blk)
  to_a.each(&blk)
end

#firstWatirNokogiri::Element

First element of this collection

Returns:



54
55
56
# File 'lib/watir-nokogiri/element_collection.rb', line 54

def first
  self[0]
end

#lastWatirNokogiri::Element

Last element of the collection

Returns:



64
65
66
# File 'lib/watir-nokogiri/element_collection.rb', line 64

def last
  self[-1]
end

#lengthFixnum Also known as: size

Returns number of elements in collection.

Returns:

  • (Fixnum)


31
32
33
# File 'lib/watir-nokogiri/element_collection.rb', line 31

def length
  elements.length
end

#to_aArray<WatirNokogiri::Element>

This collection as an Array.

Returns:



74
75
76
# File 'lib/watir-nokogiri/element_collection.rb', line 74

def to_a()
  @to_a ||= elements.map{ |e| element_class.new(@parent, :element => e) }
end