Class: Kitchen::SearchQuery

Inherits:
Object show all
Defined in:
lib/kitchen/search_query.rb

Overview

Records the search history that was used to find a certain element

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(css_or_xpath: nil, only: nil, except: nil) ⇒ SearchQuery

Create a new SearchQuery

Parameters:

  • css_or_xpath (String, Array<String>) (defaults to: nil)

    selectors to use to limit iteration results a “$” in this argument can be replaced with a default selector via #apply_default_css_or_xpath_and_normalize

  • only (Symbol, Callable) (defaults to: nil)

    the name of a method to call on an element or a lambda or proc that accepts an element; elements will only be included in the search results if the method or callable returns true

  • except (Symbol, Callable) (defaults to: nil)

    the name of a method to call on an element or a lambda or proc that accepts an element; elements will not be included in the search results if the method or callable returns false



23
24
25
26
27
28
# File 'lib/kitchen/search_query.rb', line 23

def initialize(css_or_xpath: nil, only: nil, except: nil)
  @css_or_xpath = css_or_xpath
  @only = only.is_a?(String) ? only.to_sym : only
  @except = except.is_a?(String) ? except.to_sym : except
  @default_already_applied = false
end

Instance Attribute Details

#css_or_xpathObject (readonly)

Returns the value of attribute css_or_xpath.



7
8
9
# File 'lib/kitchen/search_query.rb', line 7

def css_or_xpath
  @css_or_xpath
end

#exceptObject (readonly)

Returns the value of attribute except.



9
10
11
# File 'lib/kitchen/search_query.rb', line 9

def except
  @except
end

#onlyObject (readonly)

Returns the value of attribute only.



8
9
10
# File 'lib/kitchen/search_query.rb', line 8

def only
  @only
end

Instance Method Details

#apply_default_css_or_xpath_and_normalize(default_css_or_xpath = nil, config: nil) ⇒ Object

Replaces ‘$’ in the ‘css_or_xpath` with the provided value; also normalizes `css_or_xpath` to an array.

Parameters:

  • default_css_or_xpath (String, Proc, Symbol) (defaults to: nil)

    The selectors to substitute for the “$” character when this factory is used to build an enumerator. A string argument is used literally. A proc is eventually called given the document’s Config object (for accessing selectors). A symbol is interpreted as the name of a selector and is called on the document’s Config object’s selectors object.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/kitchen/search_query.rb', line 47

def apply_default_css_or_xpath_and_normalize(default_css_or_xpath=nil, config: nil)
  return if @default_already_applied

  default_css_or_xpath = [default_css_or_xpath].flatten.map do |item|
    case item
    when Proc
      item.call(config)
    when Symbol
      config.selectors.send(item)
    else
      item
    end
  end

  @as_type = nil
  @css_or_xpath = [css_or_xpath || '$'].flatten.map do |item|
    item.gsub(/\$/, default_css_or_xpath.join(', '))
  end

  @default_already_applied = true
end

#as_typeObject

Returns the search query as a spaceless string suitable for use as an element type



71
72
73
74
75
76
77
# File 'lib/kitchen/search_query.rb', line 71

def as_type
  @as_type ||= [
    [css_or_xpath].flatten.join(','),
    stringify_condition(only, 'only'),
    stringify_condition(except, 'except')
  ].compact.join(';')
end

#conditions_match?(element) ⇒ Boolean

Returns true iff the element passes the ‘only` and `except` conditions

Returns:

  • (Boolean)


34
35
36
# File 'lib/kitchen/search_query.rb', line 34

def conditions_match?(element)
  condition_passes?(except, element, false) && condition_passes?(only, element, true)
end

#expects_substitution?Boolean

Returns true if the query has the substitution character (‘$’)

Returns:

  • (Boolean)


87
88
89
# File 'lib/kitchen/search_query.rb', line 87

def expects_substitution?
  css_or_xpath.nil? || [css_or_xpath].flatten.all? { |item| item.include?('$') }
end

#to_sObject

Returns a string representation of the search query



81
82
83
# File 'lib/kitchen/search_query.rb', line 81

def to_s
  as_type
end