Class: Locator::Xpath

Inherits:
Array
  • Object
show all
Defined in:
lib/locator/xpath.rb

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, attributes = {}) ⇒ Xpath

Returns a new instance of Xpath.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/locator/xpath.rb', line 5

def initialize(name = nil, attributes = {})
  super(Array(name || '*').map { |name| xpath?(name) ? name : ".//#{name}" })

  attributes.each do |name, value|
    case name
    when :class
      and!(has_class(name, value))
    else
      and!(equals(name, value))
    end
  end

  flatten!
end

Instance Method Details

#and!(other) ⇒ Object



39
40
41
# File 'lib/locator/xpath.rb', line 39

def and!(other)
  replace(self.map { |l| other.map { |r| "#{l}#{r}" } })
end

#contains(value) ⇒ Object



35
36
37
# File 'lib/locator/xpath.rb', line 35

def contains(value)
  "/descendant-or-self::*[contains(., \"#{value}\")]"
end

#equals(names, values) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/locator/xpath.rb', line 20

def equals(names, values)
  case values
  when TrueClass
    "[@#{names}]"
  else
    values = Array(values).map { |value| quote(value) }
    expr= Array(names).map { |name| values.map { |value| "@#{name}=#{value}" } }.flatten
    expr.empty? ? '' : '[' + expr.join(' or ') + ']'
  end
end

#has_class(name, value) ⇒ Object



31
32
33
# File 'lib/locator/xpath.rb', line 31

def has_class(name, value)
  "[contains(concat(' ', @#{name}, ' '), concat(' ', \"#{value}\", ' '))]"
end

#to_sObject



43
44
45
# File 'lib/locator/xpath.rb', line 43

def to_s
  flatten.join(' | ')
end