Class: Kitchen::ElementFactory

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

Overview

Builds Elements from Nokogiri Nodes

Constant Summary collapse

ELEMENT_CLASSES =
ElementBase.descendants

Class Method Summary collapse

Class Method Details

.build_from_node(node:, document:, element_class: nil, default_short_type: nil, detect_element_class: false) ⇒ ElementBase

Builds a new concrete subclass of ElementBase for the provided node.

Parameters:

  • node (Nokogiri::XML::Node)

    the node to wrap in an element

  • document (Document)

    the document

  • element_class (ElementBase) (defaults to: nil)

    actually a subclass of ElementBase to use when wrapping the node.

  • default_short_type (Symbol, String) (defaults to: nil)

    if we are making an instance of an element class where we know the short_type we’ll use that; otherwise we’ll make an Element instance and use this argument as the short_type.

  • detect_element_class (Boolean) (defaults to: false)

    if true and element_class is not given, attempts to detect the element class from the node

Returns:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/kitchen/element_factory.rb', line 23

def self.build_from_node(node:,
                         document:,
                         element_class: nil,
                         default_short_type: nil,
                         detect_element_class: false)
  element_class ||= detect_element_class ? find_element_class(node, document.config) : Element

  if element_class == Element
    element_class.new(node: node,
                      document: document,
                      short_type: default_short_type)
  else
    element_class.new(node: node,
                      document: document)
  end
end

.find_element_class(node, config) ⇒ Object



40
41
42
43
44
# File 'lib/kitchen/element_factory.rb', line 40

def self.find_element_class(node, config)
  ELEMENT_CLASSES.find do |klass|
    klass.is_the_element_class_for?(node, config: config)
  end || Element
end