Module | CIMI::Model |
In: |
lib/cimi/model/errors.rb
lib/cimi/model/base.rb lib/cimi/model.rb |
The base class for any CIMI object that we either read from a request or write as a response. This class handles serializing/deserializing XML and JSON into a common form.
The conversion of XML and JSON into internal objects is based on a schema that is defined through a DSL:
class Machine < CIMI::Model::Base text :status href :meter array :volumes do scalar :href, :attachment_point, :protocol end end
The DSL automatically takes care of converting identifiers from their underscored form to the camel-cased form used by CIMI. The above class can be used in the following way:
machine = Machine.from_xml(some_xml) if machine.status == "UP" ... end sda = machine.volumes.find { |v| v.attachment_point == "/dev/sda" } handle_meter(machine.meter.href)
The keywords for the DSL are
[scalar(names, ...)] Define a scalar attribute; in JSON, this is represented as a string property. In XML, this can be represented in a number of ways, depending on whether the option :text is set: * :text not set: attribute on the enclosing element * :text == :direct: the text content of the enclosing element * :text == :nested: the text content of an element +<name>...</name>+ [text(names)] A shorthand for +scalar(names, :text => :nested)+, i.e., for attributes that in XML are represented by their own tags [href(name)] A shorthand for +struct name { scalar :href }+; in JSON, this is represented as +{ name: { "href": string } }+, and in XML as +<name href="..."/>+ [struct(name, opts, &block)] A structured subobject; the block defines the schema of the subobject. The +:content+ option can be used to specify the attribute that should receive the content of hte corresponding XML element [array(name, opts, &block)] An array of structured subobjects; the block defines the schema of the subobjects.