The smarts of converting from XML and JSON into internal objects
The actual Schema class
# File lib/cimi/models/schema.rb, line 221 def initialize @attributes = [] end
# File lib/cimi/models/schema.rb, line 306 def add_attributes!(args, attr_klass, &block) raise "The schema has already been used to convert objects" if @attributes.frozen? opts = args.extract_opts! args.each { |arg| @attributes << attr_klass.new(arg, opts, &block) } end
For MachineCollection, copy over the schema of Machine to hold each member of the collection - avoid duplicating the schemas
# File lib/cimi/models/schema.rb, line 246 def add_collection_member_array(model) member_symbol = model.name.split("::").last.underscore.pluralize.to_sym members = CIMI::Model::Schema::Array.new(member_symbol) members.struct.schema.attributes = model.schema.attributes self.attributes << members end
# File lib/cimi/models/schema.rb, line 259 def attribute_names @attributes.map { |a| a.name } end
# File lib/cimi/models/schema.rb, line 231 def from_json(json, model = {}) @attributes.freeze @attributes.each { |attr| attr.from_json(json, model) } model end
# File lib/cimi/models/schema.rb, line 225 def from_xml(xml, model = {}) @attributes.freeze @attributes.each { |attr| attr.from_xml(xml, model) } model end
# File lib/cimi/models/schema.rb, line 253 def to_json(model, json = {}) @attributes.freeze @attributes.each { |attr| attr.to_json(model, json) } json end
# File lib/cimi/models/schema.rb, line 237 def to_xml(model, xml = nil) xml ||= OrderedHash.new @attributes.freeze @attributes.each { |attr| attr.to_xml(model, xml) } xml end