Class: VCloudSdk::Xml::WrapperFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/cloud/vcloud/xml/wrapper.rb

Constant Summary collapse

@@xml_dictionary =
{}

Class Method Summary collapse

Class Method Details

.create_instance(type_name, ns = nil, namespace_defintions = nil, *args) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/cloud/vcloud/xml/wrapper.rb', line 38

def create_instance(type_name, ns = nil, namespace_defintions = nil, *args)
  xml = @@xml_dictionary[type_name]
  if (xml)
    wrap_document(xml, ns, namespace_defintions, *args)
  else
    raise Bosh::Clouds::CpiError,
      "XML type #{type_name} not found in xml_templates dir."
  end
end

.find_wrapper_class(type_name) ⇒ Object

TODO: We might run into a bug later if there are ever XML node types of the same name but different namespace



29
30
31
32
33
34
35
36
# File 'lib/cloud/vcloud/xml/wrapper.rb', line 29

def find_wrapper_class(type_name)
  # for Ruby 1.9, we would need pass false in as the 2nd parameter
  if Xml.constants.find { |c| c.to_sym == type_name.to_sym }
    Xml.const_get(type_name.to_sym)
  else
    Wrapper
  end
end

.wrap_document(xml, ns = nil, namespace_defintions = nil, *args) ⇒ Object



9
10
11
12
13
14
# File 'lib/cloud/vcloud/xml/wrapper.rb', line 9

def wrap_document(xml, ns = nil, namespace_defintions = nil, *args)
  doc = Nokogiri::XML(xml)
  type_name = doc.root.name
  node_class = find_wrapper_class(type_name)
  node_class.new(doc, ns, namespace_defintions, *args)
end

.wrap_node(node, ns, namespace_defintions = nil, *args) ⇒ Object



16
17
18
19
20
# File 'lib/cloud/vcloud/xml/wrapper.rb', line 16

def wrap_node(node, ns, namespace_defintions = nil, *args)
  type_name = node.node_name
  node_class = find_wrapper_class(type_name)
  node_class.new(node, ns, namespace_defintions, *args)
end

.wrap_nodes(nodes, ns, namespace_defintions) ⇒ Object



22
23
24
25
# File 'lib/cloud/vcloud/xml/wrapper.rb', line 22

def wrap_nodes(nodes, ns, namespace_defintions)
  nodes.collect { |node| WrapperFactory.wrap_node(node, ns,
    namespace_defintions) }
end