Class: VCloudSdk::Xml::WrapperFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_vcloud_sdk/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



40
41
42
43
44
45
46
47
48
49
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 40

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
    fail 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



31
32
33
34
35
36
37
38
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 31

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



8
9
10
11
12
13
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 8

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



15
16
17
18
19
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 15

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



21
22
23
24
25
26
27
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 21

def wrap_nodes(nodes, ns, namespace_defintions)
  nodes.map do |node|
    WrapperFactory.wrap_node(node,
                             ns,
                             namespace_defintions)
  end
end