Class: RbVmomi::NewDeserializer
- Inherits:
-
Object
- Object
- RbVmomi::NewDeserializer
- Defined in:
- lib/rbvmomi/deserialization.rb
Constant Summary collapse
- NS_XSI =
'http://www.w3.org/2001/XMLSchema-instance'
- DEMANGLED_ARRAY_TYPES =
{ 'AnyType' => 'xsd:anyType', 'DateTime' => 'xsd:dateTime', }
- BUILTIN_TYPE_ACTIONS =
{ 'xsd:string' => :string, 'xsd:boolean' => :boolean, 'xsd:byte' => :int, 'xsd:short' => :int, 'xsd:int' => :int, 'xsd:long' => :int, 'xsd:float' => :float, 'xsd:dateTime' => :date, 'PropertyPath' => :string, 'MethodName' => :string, 'TypeName' => :string, 'xsd:base64Binary' => :binary, 'KeyValue' => :keyvalue, }
Instance Method Summary collapse
- #deserialize(node, type = nil) ⇒ Object
-
#initialize(conn) ⇒ NewDeserializer
constructor
A new instance of NewDeserializer.
- #leaf_binary(node) ⇒ Object
- #leaf_date(node) ⇒ Object
-
#leaf_keyvalue(node) ⇒ Object
XXX does the value need to be deserialized?.
- #leaf_managed(node, klass) ⇒ Object
- #traverse_data(node, klass) ⇒ Object
Constructor Details
#initialize(conn) ⇒ NewDeserializer
Returns a new instance of NewDeserializer.
40 41 42 43 |
# File 'lib/rbvmomi/deserialization.rb', line 40 def initialize conn @conn = conn @loader = conn.class.loader end |
Instance Method Details
#deserialize(node, type = nil) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/rbvmomi/deserialization.rb', line 45 def deserialize node, type=nil type_attr = node['type'] # Work around for 1.5.x which doesn't populate node['type'] # XXX what changed type_attr = node.attributes['type'].value if node.attributes['type'] and not type_attr type = type_attr if type_attr if action = BUILTIN_TYPE_ACTIONS[type] case action when :string node.content when :boolean node.content == '1' || node.content == 'true' when :int node.content.to_i when :float node.content.to_f when :date leaf_date node when :binary leaf_binary node when :keyvalue leaf_keyvalue node else raise end else type = type.split(':', 2)[1] if type =~ /:/ if type =~ /^ArrayOf/ type = DEMANGLED_ARRAY_TYPES[$'] || $' return node.children.select(&:element?).map { |c| deserialize c, type } end type = type.split(':', 2)[1] if type =~ /:/ klass = @loader.get(type) or raise "no such type '#{type}'" case klass.kind when :data traverse_data node, klass when :enum node.content when :managed leaf_managed node, klass else raise end end end |
#leaf_binary(node) ⇒ Object
135 136 137 |
# File 'lib/rbvmomi/deserialization.rb', line 135 def leaf_binary node node.content.unpack('m')[0] end |
#leaf_date(node) ⇒ Object
131 132 133 |
# File 'lib/rbvmomi/deserialization.rb', line 131 def leaf_date node Time.parse node.content end |
#leaf_keyvalue(node) ⇒ Object
XXX does the value need to be deserialized?
140 141 142 143 144 145 146 147 148 |
# File 'lib/rbvmomi/deserialization.rb', line 140 def leaf_keyvalue node h = {} node.children.each do |child| next unless child.element? h[child.name] = child.content end [h['key'], h['value']] end |
#leaf_managed(node, klass) ⇒ Object
125 126 127 128 129 |
# File 'lib/rbvmomi/deserialization.rb', line 125 def leaf_managed node, klass type_attr = node['type'] klass = @loader.get(type_attr) if type_attr klass.new(@conn, node.content) end |
#traverse_data(node, klass) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/rbvmomi/deserialization.rb', line 93 def traverse_data node, klass obj = klass.new nil props = obj.props children = node.children.select(&:element?) i = 0 klass.full_props_desc.each do |desc| name = desc['name'] child_type = desc['wsdl_type'] # Ignore unknown fields while child = children[i] and not klass.full_props_set.member? child.name i += 1 end if desc['is-array'] a = [] while ((child = children[i]) && (child.name == name)) child = children[i] a << deserialize(child, child_type) i += 1 end props[name.to_sym] = a elsif ((child = children[i]) && (child.name == name)) props[name.to_sym] = deserialize(child, child_type) i += 1 end end obj end |