We keep the values of the attributes in a hash
# File lib/cimi/models/resource.rb, line 80 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) { self[name] } define_method(:"#{name}=") { |newval| self[name] = newval } end end
# File lib/cimi/models/resource.rb, line 53 def base_schema @schema ||= CIMI::Model::Schema.new end
Construct a new object
# File lib/cimi/models/resource.rb, line 107 def 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/resource.rb, line 99 def from_xml(text) xml = XmlSimple.xml_in(text, :force_content => true) model = self.new @schema.from_xml(xml, model) model end
If the model is inherited by another model, we want to clone the base schema instead of using the parent model schema, which might be modified
# File lib/cimi/models/resource.rb, line 72 def inherited(child) child.instance_eval do def schema base_schema_cloned? ? @schema : clone_base_schema end end end
Factory methods
# File lib/cimi/models/resource.rb, line 37 def initialize(values = {}) names = self.class.schema.attribute_names @select_attrs = values[:select_attr_list] || [] # Make sure we always have the :id of the entity even # the $select parameter is used and :id is filtered out # @base_id = values[:base_id] || values[:id] @attribute_values = names.inject(OrderedHash.new) do |hash, name| hash[name] = self.class.schema.convert(name, values[name]) hash end end
# File lib/cimi/models/resource.rb, line 114 def parse(text, content_type) if ["application/xml", "text/xml"].include? content_type entity = from_xml(text) entity.validate!(:xml) entity elsif content_type == "application/json" if text.kind_of? StringIO entity = from_json(text.read) else entity = from_json(text) end entity.validate!(:json) entity else raise "Can not parse content type #{content_type}" end end
# File lib/cimi/models/resource.rb, line 157 def required_attributes @schema.required_attributes end
# File lib/cimi/models/resource.rb, line 140 def resource_uri CMWG_NAMESPACE + "/" + self.name.split("::").last end
# File lib/cimi/models/resource.rb, line 74 def schema base_schema_cloned? ? @schema : clone_base_schema end
# File lib/cimi/models/resource.rb, line 144 def to_json(model) json = @schema.to_json(model) json[:resourceURI] = resource_uri JSON::unparse(json) end
# File lib/cimi/models/resource.rb, line 150 def to_xml(model) xml = @schema.to_xml(model) xml["xmlns"] = CMWG_NAMESPACE xml["resourceURI"] = resource_uri XmlSimple.xml_out(xml, :root_name => xml_tag_name) end
Serialize
# File lib/cimi/models/resource.rb, line 136 def xml_tag_name self.name.split("::").last end
END of class methods
# File lib/cimi/models/resource.rb, line 164 def [](a) @attribute_values[a] end
# File lib/cimi/models/resource.rb, line 168 def []=(a, v) return @attribute_values.delete(a) if v.nil? @attribute_values[a] = self.class.schema.convert(a, v) end
# File lib/cimi/models/resource.rb, line 194 def base_id self.id || @base_id end
Apply the $select options to all sub-collections and prepare then to serialize by setting correct :href and :id attributes.
# File lib/cimi/models/resource.rb, line 176 def prepare self.class.schema.collections.map { |coll| coll.name }.each do |n| if @select_attrs.empty? or @select_attrs.include?(n) self[n].href = "#{self.base_id}/#{n}" if !self[n].href self[n].id = "#{self.base_id}/#{n}" if !self[n].entries.empty? else self[n] = nil end end end
# File lib/cimi/models/resource.rb, line 198 def to_json self.class.to_json(self) end
# File lib/cimi/models/resource.rb, line 202 def to_xml self.class.to_xml(self) end
# File lib/cimi/models/resource.rb, line 187 def validate!(format=:xml) failed_attrs = self.class.required_attributes.map do |attr| attr.send("#{format}_name") unless attr.valid?(send(attr.name)) end.compact raise CIMI::Model::ValidationError.new(failed_attrs, format) unless failed_attrs.empty? end