Class: IbmPowerHmc::AbstractNonRest Abstract
- Inherits:
-
Object
- Object
- IbmPowerHmc::AbstractNonRest
- Defined in:
- lib/ibm_power_hmc/schema/parser.rb
Overview
HMC generic K2 non-REST object.
Direct Known Subclasses
AbstractRest, ExtendedFileData, FieldReplaceableUnit, IOAdapter, IORDevice, IOSlot, IPInterface, ManagedSystemPcmPreference, Node, PhysicalFibreChannelPort, SharedEthernetAdapter, VirtualFibreChannelMapping, VirtualFibreChannelNPortLoginStatus, VirtualMediaRepository, VirtualSCSIMapping, VirtualSCSIStorage, VirtualTargetDevice
Constant Summary collapse
- ATTRS =
{}.freeze
Instance Attribute Summary collapse
-
#xml ⇒ REXML::Document
readonly
The XML document representing this object.
Class Method Summary collapse
-
.marshal(attrs = {}, namespace = UOM_XMLNS, version = "V1_1_0") ⇒ Object
XML marshaling of object.
Instance Method Summary collapse
- #collection_of(name, type) ⇒ Object
-
#create_element(xpath) ⇒ Object
Create a new XML element.
-
#initialize(xml) ⇒ AbstractNonRest
constructor
A new instance of AbstractNonRest.
-
#marshal(attrs = {}, namespace = UOM_XMLNS, version = "V1_1_0") ⇒ Object
XML marshaling of object.
-
#singleton(xpath, attr = nil) ⇒ String?
Get the text (or the value of a specified attribute) of an XML element.
- #timestamp(xpath) ⇒ Object
- #to_s ⇒ Object
- #uuid_from_href(href, index = -1)) ⇒ Object
- #uuids_from_links(elem, index = -1)) ⇒ Object
Constructor Details
#initialize(xml) ⇒ AbstractNonRest
Returns a new instance of AbstractNonRest.
85 86 87 88 |
# File 'lib/ibm_power_hmc/schema/parser.rb', line 85 def initialize(xml) @xml = xml self.class::ATTRS.each { |varname, xpath| define_attr(varname, xpath) } end |
Instance Attribute Details
#xml ⇒ REXML::Document (readonly)
The XML document representing this object.
81 82 83 |
# File 'lib/ibm_power_hmc/schema/parser.rb', line 81 def xml @xml end |
Class Method Details
.marshal(attrs = {}, namespace = UOM_XMLNS, version = "V1_1_0") ⇒ Object
XML marshaling of object.
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/ibm_power_hmc/schema/parser.rb', line 96 def self.marshal(attrs = {}, namespace = UOM_XMLNS, version = "V1_1_0") doc = REXML::Document.new("") doc.add_element(name.split("::").last, "schemaVersion" => version) doc.root.add_namespace(namespace) obj = new(doc.root) attrs.each do |varname, value| obj.send("#{varname}=", value) end obj end |
Instance Method Details
#collection_of(name, type) ⇒ Object
183 184 185 186 187 188 189 |
# File 'lib/ibm_power_hmc/schema/parser.rb', line 183 def collection_of(name, type) xml.get_elements([name, type].compact.join("/")).map do |elem| Module.const_get("IbmPowerHmc::#{elem.name}").new(elem) rescue NameError nil end.compact end |
#create_element(xpath) ⇒ Object
Create a new XML element.
132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/ibm_power_hmc/schema/parser.rb', line 132 def create_element(xpath) cur = xml xpath.split("/").each do |el| p = cur.elements[el] if p.nil? cur = cur.add_element(el) else cur = p end end end |
#marshal(attrs = {}, namespace = UOM_XMLNS, version = "V1_1_0") ⇒ Object
XML marshaling of object.
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/ibm_power_hmc/schema/parser.rb', line 96 def self.marshal(attrs = {}, namespace = UOM_XMLNS, version = "V1_1_0") doc = REXML::Document.new("") doc.add_element(name.split("::").last, "schemaVersion" => version) doc.root.add_namespace(namespace) obj = new(doc.root) attrs.each do |varname, value| obj.send("#{varname}=", value) end obj end |
#singleton(xpath, attr = nil) ⇒ String?
Get the text (or the value of a specified attribute) of an XML element.
151 152 153 154 155 156 |
# File 'lib/ibm_power_hmc/schema/parser.rb', line 151 def singleton(xpath, attr = nil) elem = xml.elements[xpath] return if elem.nil? attr.nil? ? elem.text&.strip : elem.attributes[attr] end |
#timestamp(xpath) ⇒ Object
178 179 180 181 |
# File 'lib/ibm_power_hmc/schema/parser.rb', line 178 def (xpath) # XML element containing a number of milliseconds since the Epoch. Time.at(0, singleton(xpath).to_i, :millisecond).utc end |
#to_s ⇒ Object
158 159 160 161 162 163 164 165 166 |
# File 'lib/ibm_power_hmc/schema/parser.rb', line 158 def to_s str = +"#{self.class.name}:\n" self.class::ATTRS.each do |varname, _| value = instance_variable_get("@#{varname}") value = value.nil? ? "null" : "'#{value}'" str << " #{varname}: #{value}\n" end str end |
#uuid_from_href(href, index = -1)) ⇒ Object
168 169 170 |
# File 'lib/ibm_power_hmc/schema/parser.rb', line 168 def uuid_from_href(href, index = -1) URI(href).path.split('/')[index] end |
#uuids_from_links(elem, index = -1)) ⇒ Object
172 173 174 175 176 |
# File 'lib/ibm_power_hmc/schema/parser.rb', line 172 def uuids_from_links(elem, index = -1) xml.get_elements("#{elem}/link[@href]").map do |link| uuid_from_href(link.attributes["href"], index) end.compact end |