Class: Locator::Xpath
- Inherits:
-
Array
- Object
- Array
- Locator::Xpath
- Defined in:
- lib/locator/xpath.rb
Class Method Summary collapse
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.
- #or!(other) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(name = nil, attributes = {}) ⇒ Xpath
Returns a new instance of Xpath.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/locator/xpath.rb', line 11 def initialize(name = nil, attributes = {}) super(Array(name || '*').map { |name| self.class.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 |
Class Method Details
.xpath?(string) ⇒ Boolean
6 7 8 |
# File 'lib/locator/xpath.rb', line 6 def xpath?(string) string =~ %r(^\.?//) end |
Instance Method Details
#and!(other) ⇒ Object
45 46 47 |
# File 'lib/locator/xpath.rb', line 45 def and!(other) replace(self.map { |l| Array(other).map { |r| "#{l}#{r}" } }.flatten) end |
#contains(value) ⇒ Object
41 42 43 |
# File 'lib/locator/xpath.rb', line 41 def contains(value) "/descendant-or-self::*[contains(., \"#{value}\")]" end |
#equals(names, values) ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/locator/xpath.rb', line 26 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
37 38 39 |
# File 'lib/locator/xpath.rb', line 37 def has_class(name, value) "[contains(concat(' ', @#{name}, ' '), concat(' ', \"#{value}\", ' '))]" end |
#or!(other) ⇒ Object
49 50 51 |
# File 'lib/locator/xpath.rb', line 49 def or!(other) other.empty? ? self : replace([self.dup, other]) end |
#to_s ⇒ Object
53 54 55 |
# File 'lib/locator/xpath.rb', line 53 def to_s flatten.join(' | ') end |