Module: WildcardFinders::Finders

Included in:
Capybara::Node::Base
Defined in:
lib/wildcard_finders/finders.rb

Constant Summary collapse

METHODS =
[]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.method_added(name) ⇒ Object



47
48
49
# File 'lib/wildcard_finders/finders.rb', line 47

def self.method_added(name)
  METHODS.push(name)
end

Instance Method Details

#find_exactly(tag, matcher) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/wildcard_finders/finders.rb', line 24

def find_exactly(tag, matcher)
  xpath = XPath.generate do |x|
    attr_matcher = matcher.map do |key, value|
      case key
      when :text
        x.text.equals(value)
      when :class
        x.attr(:class).contains(value)
      else
        x.attr(key).equals(value)
      end
    end

    x.descendant(tag.to_sym)[attr_matcher.inject(&:&)]
  end

  begin
    find(:xpath, xpath)
  rescue Capybara::ElementNotFound
    nil
  end
end

#find_tag_like(tag, matcher = nil, opts = {}, &block) ⇒ Object

not to add METHODS



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/wildcard_finders/finders.rb', line 9

def find_tag_like(tag, matcher = nil, opts = {}, &block)
  if matcher.is_a?(Hash) && matcher.values.all? {|v| v.is_a?(String) }
    find_exactly(tag, matcher)
  else
    all(tag).select do |e|
      if matcher.is_a?(Hash)
        hash = matcher.keys.each_with_object({}) {|key, h| h[key] = e[key] }
        WildcardMatchers.wildcard_match?(hash, matcher)
      else
        WildcardMatchers.wildcard_match?(e, block || matcher)
      end
    end.first # not compatible with capybara 2.x
  end
end