ovirt-engine-sdk 4.0.1 → 4.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -16,5 +16,5 @@
16
16
 
17
17
 
18
18
  module OvirtSDK4
19
- VERSION = '4.0.1'
19
+ VERSION = '4.4.1'.freeze
20
20
  end
@@ -15,7 +15,6 @@
15
15
  #
16
16
 
17
17
  module OvirtSDK4
18
-
19
18
  #
20
19
  # This is the base class for all the XML writers used by the SDK. It contains the utility methods used by
21
20
  # all of them.
@@ -23,7 +22,6 @@ module OvirtSDK4
23
22
  # @api private
24
23
  #
25
24
  class Writer
26
-
27
25
  #
28
26
  # Writes an element with the given name and string value.
29
27
  #
@@ -42,11 +40,7 @@ module OvirtSDK4
42
40
  # @return [String]
43
41
  #
44
42
  def self.render_boolean(value)
45
- if value
46
- return 'true'
47
- else
48
- return 'false'
49
- end
43
+ value ? 'true' : 'false'
50
44
  end
51
45
 
52
46
  #
@@ -67,7 +61,7 @@ module OvirtSDK4
67
61
  # @return [String]
68
62
  #
69
63
  def self.render_integer(value)
70
- return value.to_s
64
+ value.to_s
71
65
  end
72
66
 
73
67
  #
@@ -84,11 +78,11 @@ module OvirtSDK4
84
78
  #
85
79
  # Converts the given decimal value to an string.
86
80
  #
87
- # @param value [Fixnum]
81
+ # @param value [Float]
88
82
  # @return [String]
89
83
  #
90
84
  def self.render_decimal(value)
91
- return value.to_s
85
+ value.to_s
92
86
  end
93
87
 
94
88
  #
@@ -96,7 +90,7 @@ module OvirtSDK4
96
90
  #
97
91
  # @param writer [XmlWriter]
98
92
  # @param name [String]
99
- # @param value [Fixnum]
93
+ # @param value [Float]
100
94
  #
101
95
  def self.write_decimal(writer, name, value)
102
96
  writer.write_element(name, Writer.render_decimal(value))
@@ -109,7 +103,30 @@ module OvirtSDK4
109
103
  # @return [String]
110
104
  #
111
105
  def self.render_date(value)
112
- return value.xmlschema
106
+ value.xmlschema
107
+ end
108
+
109
+ #
110
+ # Converts the given value to an string, assuming that it is of the given type.
111
+ #
112
+ # @param value [Object] The value.
113
+ # @param type [Class] The type.
114
+ # @return [String] The string that represents the value.
115
+ #
116
+ def self.render(value, type)
117
+ if type.equal?(String)
118
+ value
119
+ elsif type.equal?(TrueClass)
120
+ render_boolean(value)
121
+ elsif type.equal?(Integer)
122
+ render_integer(value)
123
+ elsif type.equal?(Float)
124
+ render_decimal(value)
125
+ elsif type.equal?(DateTime)
126
+ render_date(value)
127
+ else
128
+ raise Error, "Don't know how to render value '#{value}' of type '#{type}'"
129
+ end
113
130
  end
114
131
 
115
132
  #
@@ -123,6 +140,84 @@ module OvirtSDK4
123
140
  writer.write_element(name, Writer.render_date(value))
124
141
  end
125
142
 
126
- end
143
+ #
144
+ # This hash stores for each known type a reference to the method that writes the XML document corresponding for that
145
+ # type. For example, for the `Vm` type it will contain a reference to the `VmWriter.write_one` method.
146
+ #
147
+ @writers = {}
148
+
149
+ #
150
+ # Registers a write method.
151
+ #
152
+ # @param type [Class] The type.
153
+ # @param writer [Method] The reference to the method that writes the XML document corresponding to the type.
154
+ #
155
+ def self.register(type, writer)
156
+ @writers[type] = writer
157
+ end
127
158
 
159
+ #
160
+ # Writes one object, determining the writer method to use based on the type. For example if the type of the object
161
+ # is `Vm` then it will create write the `vm` tag, with its contents.
162
+ #
163
+ # @param object [Struct] The object to write.
164
+ #
165
+ # @param opts [Hash] Options to alter the behaviour of the method.
166
+ #
167
+ # @option opts [XmlWriter] :target The XML writer where the output will be written. If it this option
168
+ # isn't given, or if the value is `nil` the method will return a string contain the XML document.
169
+ #
170
+ # @option opts [String] :root The name of the root tag of the generated XML document. This isn't needed
171
+ # when writing single objects, as the tag is calculated from the type of the object, for example, if
172
+ # the object is a virtual machine then the tag will be `vm`. But when writing arrays of objects the tag
173
+ # is needed, because the list may be empty, or have different types of objects. In this case, for arrays,
174
+ # if the name isn't provided an exception will be raised.
175
+ #
176
+ # @option opts [Boolean] :indent (false) Indicates if the output should be indented, for easier reading by humans.
177
+ #
178
+ def self.write(object, opts = {})
179
+ # Get the options:
180
+ target = opts[:target]
181
+ root = opts[:root]
182
+ indent = opts[:indent] || false
183
+
184
+ # If the target is `nil` then create a temporary XML writer to write the output:
185
+ cursor = nil
186
+ if target.nil?
187
+ cursor = XmlWriter.new(nil, indent)
188
+ elsif target.is_a?(XmlWriter)
189
+ cursor = target
190
+ else
191
+ raise ArgumentError, "Expected an 'XmlWriter', but got '#{target.class}'"
192
+ end
193
+
194
+ # Do the actual write, and make sure to always close the XML writer if we created it:
195
+ begin
196
+ if object.is_a?(Array)
197
+ # For arrays we can't decide which tag to use, so the 'root' parameter is mandatory in this case:
198
+ raise Error, "The 'root' option is mandatory when writing arrays" if root.nil?
199
+
200
+ # Write the root tag, and then recursively call the method to write each of the items of the array:
201
+ cursor.write_start(root)
202
+ object.each do |item|
203
+ write(item, target: cursor)
204
+ end
205
+ cursor.write_end
206
+ else
207
+ # Select the specific writer according to the type:
208
+ type = object.class
209
+ writer = @writers[type]
210
+ raise Error, "Can't find a writer for type '#{type}'" if writer.nil?
211
+
212
+ # Write the object using the specific method:
213
+ writer.call(object, cursor, root)
214
+ end
215
+
216
+ # If no XML cursor was explicitly given, and we created it, then we need to return the generated XML text:
217
+ cursor.string if target.nil?
218
+ ensure
219
+ cursor.close if !cursor.nil? && !cursor.equal?(target)
220
+ end
221
+ end
222
+ end
128
223
  end