Class: CIMI::Model::Resource
- Inherits:
-
Object
- Object
- CIMI::Model::Resource
- Extended by:
- Schema::DSL
- Defined in:
- lib/cimi/models/resource.rb
Direct Known Subclasses
Constant Summary collapse
- CMWG_NAMESPACE =
"http://schemas.dmtf.org/cimi/1"
Instance Attribute Summary collapse
-
#attribute_values ⇒ Object
readonly
We keep the values of the attributes in a hash.
Class Method Summary collapse
- .add_attributes!(names, attr_klass, &block) ⇒ Object
-
.all_uri(context) ⇒ Object
Return Array of links to current CIMI object.
- .base_schema ⇒ Object
- .base_schema_cloned? ⇒ Boolean
- .clone_base_schema ⇒ Object
-
.from_json(text) ⇒ Object
Construct a new object.
-
.from_xml(text) ⇒ Object
Construct a new object from the XML representation
xml
. -
.inherited(child) ⇒ Object
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.
- .parse(text, content_type) ⇒ Object
- .resource_uri ⇒ Object
- .to_json(model) ⇒ Object
- .to_xml(model) ⇒ Object
-
.xml_tag_name ⇒ Object
Serialize.
Instance Method Summary collapse
-
#[](a) ⇒ Object
END of class methods.
- #[]=(a, v) ⇒ Object
- #base_id ⇒ Object
-
#initialize(values = {}) ⇒ Resource
constructor
Factory methods.
-
#prepare ⇒ Object
Apply the $select options to all sub-collections and prepare then to serialize by setting correct :href and :id attributes.
- #to_json ⇒ Object
- #to_xml ⇒ Object
Methods included from Schema::DSL
array, collection, hash, href, resource_attr, scalar, struct, text
Methods included from Helpers::FilterResourceMethods
#filter_by, #parse_filter_opts
Methods included from Helpers::SelectResourceMethods
#select_by, #select_by_arr_index, #select_by_arr_range
Constructor Details
#initialize(values = {}) ⇒ Resource
Factory methods
37 38 39 40 41 42 43 44 45 46 47 48 |
# 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 |
Instance Attribute Details
#attribute_values ⇒ Object (readonly)
We keep the values of the attributes in a hash
30 31 32 |
# File 'lib/cimi/models/resource.rb', line 30 def attribute_values @attribute_values end |
Class Method Details
.add_attributes!(names, attr_klass, &block) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 |
# 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) ⇒ Object
Return Array of links to current CIMI object
94 95 96 |
# File 'lib/cimi/models/resource.rb', line 94 def all_uri(context) self.all(context).map { |e| { :href => e.id } } end |
.base_schema ⇒ Object
53 54 55 |
# File 'lib/cimi/models/resource.rb', line 53 def base_schema @schema ||= CIMI::Model::Schema.new end |
.base_schema_cloned? ⇒ Boolean
62 63 64 |
# File 'lib/cimi/models/resource.rb', line 62 def base_schema_cloned? @schema_duped end |
.clone_base_schema ⇒ Object
57 58 59 60 |
# File 'lib/cimi/models/resource.rb', line 57 def clone_base_schema @schema_duped = true @schema = superclass.base_schema.deep_clone end |
.from_json(text) ⇒ Object
Construct a new object
107 108 109 110 111 112 |
# 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) ⇒ Object
Construct a new object from the XML representation xml
99 100 101 102 103 104 |
# 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) ⇒ Object
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
72 73 74 75 76 77 78 |
# 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 |
.parse(text, content_type) ⇒ Object
114 115 116 117 118 119 120 121 122 |
# File 'lib/cimi/models/resource.rb', line 114 def 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 ⇒ Object
132 133 134 |
# File 'lib/cimi/models/resource.rb', line 132 def resource_uri CMWG_NAMESPACE + "/" + self.name.split("::").last end |
.to_json(model) ⇒ Object
136 137 138 139 140 |
# File 'lib/cimi/models/resource.rb', line 136 def to_json(model) json = @schema.to_json(model) json[:resourceURI] = resource_uri JSON::unparse(json) end |
.to_xml(model) ⇒ Object
142 143 144 145 146 147 |
# File 'lib/cimi/models/resource.rb', line 142 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 ⇒ Object
Serialize
128 129 130 |
# File 'lib/cimi/models/resource.rb', line 128 def xml_tag_name self.name.split("::").last end |
Instance Method Details
#[](a) ⇒ Object
END of class methods
152 153 154 |
# File 'lib/cimi/models/resource.rb', line 152 def [](a) @attribute_values[a] end |
#[]=(a, v) ⇒ Object
156 157 158 159 |
# File 'lib/cimi/models/resource.rb', line 156 def []=(a, v) return @attribute_values.delete(a) if v.nil? @attribute_values[a] = self.class.schema.convert(a, v) end |
#base_id ⇒ Object
175 176 177 |
# File 'lib/cimi/models/resource.rb', line 175 def base_id self.id || @base_id end |
#prepare ⇒ Object
Apply the $select options to all sub-collections and prepare then to serialize by setting correct :href and :id attributes.
164 165 166 167 168 169 170 171 172 173 |
# File 'lib/cimi/models/resource.rb', line 164 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 ⇒ Object
179 180 181 |
# File 'lib/cimi/models/resource.rb', line 179 def to_json self.class.to_json(self) end |
#to_xml ⇒ Object
183 184 185 |
# File 'lib/cimi/models/resource.rb', line 183 def to_xml self.class.to_xml(self) end |