We need to reopen Base and add some stuff to avoid circular dependencies
We keep the values of the attributes in a hash
# File lib/cimi/models/base.rb, line 103 def <<(model) clone_base_schema unless base_schema_cloned? member_name = model.name.split("::").last if ::Struct.const_defined?("CIMI_#{member_name}") puts "Removing struct" ::Struct.send(:remove_const, "CIMI_#{member_name}") end member_symbol = member_name.underscore.pluralize.to_sym members = CIMI::Model::Schema::Array.new(member_symbol) members.struct.schema.attributes = model.schema.attributes base_schema.attributes << members end
# File lib/cimi/models/base.rb, line 233 def self.acts_as_root_entity(name=nil) if name name = name.to_s.camelize.pluralize else name = xml_tag_name.pluralize.uncapitalize end CIMI::Model.register_as_root_entity! name end
# File lib/cimi/models/base.rb, line 139 def add_attributes!(names, attr_klass, &block) if self.respond_to? :schema schema.add_attributes!(names, attr_klass, &block) else base_schema.add_attributes!(names, attr_klass, &block) end names.each do |name| define_method(name) { @attribute_values[name] } define_method(:"#{name}=") { |newval| @attribute_values[name] = newval } end end
# File lib/cimi/models/base.rb, line 242 def self.all(_self); find(:all, _self); end
# File lib/cimi/models/base.rb, line 116 def base_schema @schema ||= CIMI::Model::Schema.new end
Construct a new object
# File lib/cimi/models/base.rb, line 183 def self.from_json(text) json = JSON::parse(text) model = self.new @schema.from_json(json, model) model end
Construct a new object from the XML representation xml
# File lib/cimi/models/base.rb, line 175 def self.from_xml(text) xml = XmlSimple.xml_in(text, :force_content => true) model = self.new @schema.from_xml(xml, model) model end
# File lib/cimi/models/base.rb, line 131 def inherited(child) child.instance_eval do def schema base_schema_cloned? ? @schema : clone_base_schema end end end
Return a collection of entities
# File lib/cimi/models/collection.rb, line 86 def list(context) entries = find(:all, context) desc = "#{self.name.split("::").last} Collection for the #{context.driver.name.capitalize} driver" id = context.send("#{collection_class.entry_name}_url") collection_class.new(:id => id, :name => 'default', :count => entries.size, :entries => entries, :description => desc) end
Factory methods
# File lib/cimi/models/base.rb, line 170 def initialize(values = {}) @attribute_values = values end
# File lib/cimi/models/base.rb, line 190 def self.parse(text, content_type) if content_type == "application/xml" from_xml(text) elsif content_type == "application/json" from_json(text) else raise "Can not parse content type #{content_type}" end end
# File lib/cimi/models/base.rb, line 133 def schema base_schema_cloned? ? @schema : clone_base_schema end
# File lib/cimi/models/base.rb, line 208 def self.to_json(model) JSON::unparse(@schema.to_json(model)) end
# File lib/cimi/models/base.rb, line 212 def self.to_xml(model) xml = @schema.to_xml(model) xml["xmlns"] = "http://schemas.dmtf.org/cimi/1" XmlSimple.xml_out(xml, :root_name => xml_tag_name) end
Serialize
# File lib/cimi/models/base.rb, line 204 def self.xml_tag_name self.name.split("::").last end
# File lib/cimi/models/base.rb, line 159 def [](a) @attribute_values[a] end
# File lib/cimi/models/base.rb, line 163 def []=(a, v) @attribute_values[a] = v end
# File lib/cimi/models/base.rb, line 244 def filter_by(filter_opts) return self if filter_opts.nil? return filter_attributes(filter_opts.split(',').map{ |a| a.intern }) if filter_opts.include? ',' case filter_opts when %r^([\w\_]+)$/ then filter_attributes([$1.intern]) when %r^([\w\_]+)\[(\d+\-\d+)\]$/ then filter_by_arr_range($1.intern, $2) when %r^([\w\_]+)\[(\d+)\]$/ then filter_by_arr_index($1.intern, $2) else self end end
# File lib/cimi/models/base.rb, line 218 def to_json self.class.to_json(self) end
# File lib/cimi/models/base.rb, line 222 def to_xml self.class.to_xml(self) end