Sha256: a5de9303e219fbaca4c2375960d7f83fe302a7b327808a0e96c9920743e4ef6b

Contents?: true

Size: 932 Bytes

Versions: 3

Compression:

Stored size: 932 Bytes

Contents

# encoding: utf-8
module Watir
  class ElementCollection
    include Enumerable

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

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

    def length
      to_a.length
    end
    alias_method :size, :length

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

    def first
      to_a[0] || @element_class.new(@parent, :index, 0)
    end

    def last
      to_a[-1] || @element_class.new(@parent, :index, -1)
    end

    def to_a
      # TODO: optimize - lazily @element_class instance
      @to_a ||= elements.map { |e| @element_class.new(@parent, :element, e) }
    end

    def elements
      @elements ||= ElementLocator.new(
        @parent.wd,
        @element_class.default_selector,
        @element_class.attribute_list
      ).locate_all
    end

  end # ElementCollection
end # Watir

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
watir-webdriver-0.0.1.dev3 lib/watir-webdriver/collections/element_collection.rb
watir-webdriver-0.0.1.dev2 lib/watir-webdriver/collections/element_collection.rb
watir-webdriver-0.0.1.dev lib/watir-webdriver/collections/element_collection.rb