class CIMI::Model::Resource

Constants

CMWG_NAMESPACE

Attributes

attribute_values[R]

We keep the values of the attributes in a hash

Public Class Methods

add_attributes!(names, attr_klass, &block) click to toggle source
# 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
all_uri(context) click to toggle source

Return Array of links to current CIMI object

# File lib/cimi/models/resource.rb, line 94
def all_uri(context)
  self.all(context).map { |e| { :href => e.id } }
end
base_schema() click to toggle source
# File lib/cimi/models/resource.rb, line 53
def base_schema
  @schema ||= CIMI::Model::Schema.new
end
from_json(text) click to toggle source

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
from_xml(text) click to toggle source

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
inherited(child) click to toggle source

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
new(values = {}) click to toggle source

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
parse(text, content_type) click to toggle source
# 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
required_attributes() click to toggle source
# File lib/cimi/models/resource.rb, line 157
def required_attributes
  @schema.required_attributes
end
resource_uri() click to toggle source
# File lib/cimi/models/resource.rb, line 140
def resource_uri
  CMWG_NAMESPACE + "/" + self.name.split("::").last
end
schema() click to toggle source
# File lib/cimi/models/resource.rb, line 74
def schema
  base_schema_cloned? ? @schema : clone_base_schema
end
to_json(model) click to toggle source
# 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
to_xml(model) click to toggle source
# 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
xml_tag_name() click to toggle source

Serialize

# File lib/cimi/models/resource.rb, line 136
def xml_tag_name
  self.name.split("::").last
end

Public Instance Methods

[](a) click to toggle source

END of class methods

# File lib/cimi/models/resource.rb, line 164
def [](a)
  @attribute_values[a]
end
[]=(a, v) click to toggle source
# 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
base_id() click to toggle source
# File lib/cimi/models/resource.rb, line 194
def base_id
  self.id || @base_id
end
prepare() click to toggle source

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
to_json() click to toggle source
# File lib/cimi/models/resource.rb, line 198
def to_json
  self.class.to_json(self)
end
to_xml() click to toggle source
# File lib/cimi/models/resource.rb, line 202
def to_xml
  self.class.to_xml(self)
end
validate!(format=:xml) click to toggle source
# 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