class Insite::ElementCollection

Attributes

browser[RW]
selector[R]

Public Class Methods

collection?() click to toggle source
# File lib/insite/element/element_collection.rb, line 6
def self.collection?
  true
end
collection_member_type() click to toggle source
# File lib/insite/element/element_collection.rb, line 23
def self.collection_member_type
  if Insite::CLASS_MAP.values.include?(self)
    @collection_member_type = self.to_s.gsub('Collection', '').constantize
  elsif Insite::CLASS_MAP.values.include?(self.superclass)
    @collection_member_type = self.superclass.to_s.gsub('Collection', '').constantize
  else
    raise "Unable to determine collection member type for #{self}."
  end
end
new(query_scope, *args) click to toggle source
# File lib/insite/element/element_collection.rb, line 33
def initialize(query_scope, *args)
  @collection_member_type = self.class.collection_member_type

  # Figure out the correct query scope.
  query_scope.respond_to?(:target) ? obj = query_scope : obj = query_scope.site
  @query_scope   = obj
  @site     = query_scope.class.ancestors.include?(Insite) ? query_scope : query_scope.site
  @browser  = @site.browser
  if args[0].is_a?(Insite::Element) || args[0].is_a?(Watir::Element)
    @target   = args[0].target
    @args     = @target.selector.dup
    @selector = @args
  elsif args[0].is_a?(Insite::ElementCollection) || args[0].is_a?(Watir::ElementCollection)
    @target   = args[0]
    @args     = @target.instance_variable_get(:@selector).dup
    @selector   = @args
  else
    if @collection_member_type == Insite::HTMLElement
      @args     = parse_args(args)
      @selector = @args

      # Figure out the correct query scope.
      @non_relative = @args.delete(:non_relative) || false
      if @non_relative
        @query_scope = query_scope.site
      else
        query_scope.respond_to?(:target) ? obj = query_scope : obj = query_scope.site
        @query_scope = obj
      end

      @target   = Watir::HTMLElementCollection.new(@query_scope.target, @args)
    else
      @args   = parse_args(args).merge(
        tag_name: Insite.class_to_tag(@collection_member_type)
      )
      @selector = @args

      # Figure out the correct query scope.
      @non_relative = @args.delete(:non_relative) || false
      if @non_relative
        @query_scope = query_scope.site
      else
        query_scope.respond_to?(:target) ? obj = query_scope : obj = query_scope.site
        @query_scope = obj
      end
      @selector = @args

      @target   = Insite::CLASS_MAP.key(self.class).new(@query_scope.target, @args)
    end
  end
end

Public Instance Methods

==(other) click to toggle source
# File lib/insite/element/element_collection.rb, line 10
def ==(other)
  to_a == other.to_a
end
Also aliased as: eql?
[](idx) click to toggle source
# File lib/insite/element/element_collection.rb, line 15
def[](idx)
  to_a[idx]
end
collection?() click to toggle source
# File lib/insite/element/element_collection.rb, line 19
def collection?
  true
end
count()
Alias for: length
each(&block) click to toggle source
# File lib/insite/element/element_collection.rb, line 89
def each(&block)
  to_a.each(&block)
end
empty?() click to toggle source
# File lib/insite/element/element_collection.rb, line 93
def empty?
  length == 0
end
eql?(other)
Alias for: ==
first() click to toggle source
# File lib/insite/element/element_collection.rb, line 85
def first
  to_a[0]
end
inspect() click to toggle source
# File lib/insite/element/element_collection.rb, line 97
def inspect
  @selector.empty? ? s = '{element: (selenium element)}' : s = @selector.to_s
  "#<#{self.class}: @query_scope: #{@query_scope}; @selector=#{s}>"
end
last() click to toggle source
# File lib/insite/element/element_collection.rb, line 102
def last
  to_a[-1]
end
length() click to toggle source
# File lib/insite/element/element_collection.rb, line 106
def length
  to_a.length
end
Also aliased as: count, size
size()
Alias for: length
to_a() click to toggle source
# File lib/insite/element/element_collection.rb, line 112
def to_a
  out = []
  @target.to_a.each_with_index do |elem, idx|
    out << @collection_member_type.new(
      @query_scope,
      @args.merge(index: idx)
    )
  end
  out
end