class CIMI::Model::Resource

Constants

CMWG_NAMESPACE

Attributes

attribute_values[R]

We keep the values of the attributes in a hash

Public Class Methods

<<(model) click to toggle source
# File lib/cimi/models/base.rb, line 108
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
add_attributes!(names, attr_klass, &block) click to toggle source
# File lib/cimi/models/base.rb, line 144
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/base.rb, line 157
def all_uri(context)
  self.all(context).map { |e| { :href => e.id } }
end
base_schema() click to toggle source
# File lib/cimi/models/base.rb, line 121
def base_schema
  @schema ||= CIMI::Model::Schema.new
end
from_json(text) click to toggle source

Construct a new object

# File lib/cimi/models/base.rb, line 200
def self.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/base.rb, line 192
def self.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
# File lib/cimi/models/base.rb, line 136
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/base.rb, line 183
def initialize(values = {})
  names = self.class.schema.attribute_names
  @attribute_values = names.inject({}) 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/base.rb, line 207
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
resource_uri() click to toggle source
# File lib/cimi/models/base.rb, line 225
def self.resource_uri
  CMWG_NAMESPACE + "/" + self.name.split("::").last
end
schema() click to toggle source
# File lib/cimi/models/base.rb, line 138
def schema
  base_schema_cloned? ? @schema : clone_base_schema
end
to_json(model) click to toggle source
# File lib/cimi/models/base.rb, line 229
def self.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/base.rb, line 235
def self.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/base.rb, line 221
def self.xml_tag_name
  self.name.split("::").last
end

Public Instance Methods

[](a) click to toggle source
# File lib/cimi/models/base.rb, line 164
def [](a)
  @attribute_values[a]
end
[]=(a, v) click to toggle source
# File lib/cimi/models/base.rb, line 168
def []=(a, v)
  @attribute_values[a] = self.class.schema.convert(a, v)
end
filter_by(filter_opts) click to toggle source
# File lib/cimi/models/base.rb, line 250
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
filter_by_arr_index(attr, filter) click to toggle source
# File lib/cimi/models/base.rb, line 261
def filter_by_arr_index(attr, filter)
  return self unless self.respond_to?(attr)
  self.class.new(attr => [self.send(attr)[filter.to_i]])
end
filter_by_arr_range(attr, filter) click to toggle source
# File lib/cimi/models/base.rb, line 266
def filter_by_arr_range(attr, filter)
  return self unless self.respond_to?(attr)
  filter = filter.split('-').inject { |s,e| s.to_i..e.to_i }
  self.class.new(attr => self.send(attr)[filter])
end
prepare() click to toggle source

Prepare to serialize

# File lib/cimi/models/base.rb, line 173
def prepare
  self.class.schema.collections.map { |coll| coll.name }.each do |n|
    self[n].href = "#{self.id}/#{n}" unless self[n].href
    self[n].id = "#{self.id}/#{n}" if !self[n].entries.empty?
  end
end
to_json() click to toggle source
# File lib/cimi/models/base.rb, line 242
def to_json
  self.class.to_json(self)
end
to_xml() click to toggle source
# File lib/cimi/models/base.rb, line 246
def to_xml
  self.class.to_xml(self)
end