Class: Kitchen::ElementEnumeratorFactory

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

Overview

Builds specific subclasses of ElementEnumeratorBase

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enumerator_class:, default_css_or_xpath: nil, sub_element_class: nil, detect_sub_element_class: false) ⇒ ElementEnumeratorFactory

Creates a new instance

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. The easiest way to get a Proc is to pass ‘Selector.named(:name_of_selector)`

  • sub_element_class (ElementBase) (defaults to: nil)

    The element class to use for what the enumerator finds.

  • enumerator_class (ElementEnumeratorBase)

    The enumerator class to return

  • detect_sub_element_class (Boolean) (defaults to: false)

    If true, infers the sub_element_class from the node



24
25
26
27
28
29
30
# File 'lib/kitchen/element_enumerator_factory.rb', line 24

def initialize(enumerator_class:, default_css_or_xpath: nil, sub_element_class: nil,
               detect_sub_element_class: false)
  @default_css_or_xpath = default_css_or_xpath
  @sub_element_class = sub_element_class
  @enumerator_class = enumerator_class
  @detect_sub_element_class = detect_sub_element_class
end

Instance Attribute Details

#default_css_or_xpathObject (readonly)

Returns the value of attribute default_css_or_xpath.



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

def default_css_or_xpath
  @default_css_or_xpath
end

#detect_sub_element_classObject (readonly)

Returns the value of attribute detect_sub_element_class.



11
12
13
# File 'lib/kitchen/element_enumerator_factory.rb', line 11

def detect_sub_element_class
  @detect_sub_element_class
end

#enumerator_classObject (readonly)

Returns the value of attribute enumerator_class.



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

def enumerator_class
  @enumerator_class
end

#sub_element_classObject (readonly)

Returns the value of attribute sub_element_class.



10
11
12
# File 'lib/kitchen/element_enumerator_factory.rb', line 10

def sub_element_class
  @sub_element_class
end

Instance Method Details

#build_within(enumerator_or_element, search_query: SearchQuery.new, reload: false) ⇒ ElementEnumeratorBase

Builds a new enumerator within the scope of the provided argument (either an enumerator or a class). Accepts optional selectors to further limit the scope of results found.

Parameters:

Returns:

  • (ElementEnumeratorBase)

    actually returns the concrete enumerator class given to the factory in its constructor.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/kitchen/element_enumerator_factory.rb', line 42

def build_within(enumerator_or_element, search_query: SearchQuery.new, reload: false)
  case enumerator_or_element
  when ElementBase
    build_within_element(enumerator_or_element,
                         search_query: search_query,
                         reload: reload)
  when ElementEnumeratorBase
    if enumerator_class != ElementEnumerator && !search_query.expects_substitution?
      raise "Query #{search_query} is missing the substitution character ('$') but " \
            "is run with an enumerator #{enumerator_class.name} that has its own " \
            "selectors for substitution."
    end

    build_within_other_enumerator(enumerator_or_element,
                                  search_query: search_query,
                                  reload: reload)
  end
end

#or_with(other_factory) ⇒ ElementEnumeratorFactory

Builds a new enumerator that finds elements matching either this factory’s or the provided factory’s selectors.

Parameters:

Returns:



67
68
69
70
71
72
73
# File 'lib/kitchen/element_enumerator_factory.rb', line 67

def or_with(other_factory)
  self.class.new(
    default_css_or_xpath: [default_css_or_xpath, other_factory.default_css_or_xpath],
    enumerator_class: TypeCastingElementEnumerator,
    detect_sub_element_class: true
  )
end