Class: Locator::Xpath
- Inherits:
-
Array
- Object
- Array
- Locator::Xpath
- Defined in:
- lib/locator/xpath.rb
Instance Method Summary collapse
- #and!(other) ⇒ Object
- #contains(value) ⇒ Object
- #equals(names, values) ⇒ Object
- #has_class(name, value) ⇒ Object
-
#initialize(name = nil, attributes = {}) ⇒ Xpath
constructor
A new instance of Xpath.
- #to_s ⇒ Object
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_s ⇒ Object
43 44 45 |
# File 'lib/locator/xpath.rb', line 43 def to_s flatten.join(' | ') end |