Class: CIMI::Model::Resource
Constant Summary
collapse
- CMWG_NAMESPACE =
"http://schemas.dmtf.org/cimi/1"
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
array, collection, hash_map, href, ref, scalar, struct, text
#filter_by, #parse_filter_opts
#select_by, #select_by_arr_index, #select_by_arr_range
Constructor Details
#initialize(values = {}) ⇒ Resource
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] || []
@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
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
|
.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
101
102
103
104
105
106
|
# File 'lib/cimi/models/resource.rb', line 101
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
93
94
95
96
97
98
|
# File 'lib/cimi/models/resource.rb', line 93
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/cimi/models/resource.rb', line 108
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 ⇒ Object
151
152
153
|
# File 'lib/cimi/models/resource.rb', line 151
def required_attributes
@schema.required_attributes
end
|
.resource_uri ⇒ Object
134
135
136
|
# File 'lib/cimi/models/resource.rb', line 134
def resource_uri
CMWG_NAMESPACE + "/" + self.name.split("::").last
end
|
.to_json(model) ⇒ Object
138
139
140
141
142
|
# File 'lib/cimi/models/resource.rb', line 138
def to_json(model)
json = @schema.to_json(model)
json[:resourceURI] = resource_uri
JSON::unparse(json)
end
|
.to_xml(model) ⇒ Object
144
145
146
147
148
149
|
# File 'lib/cimi/models/resource.rb', line 144
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
130
131
132
|
# File 'lib/cimi/models/resource.rb', line 130
def xml_tag_name
self.name.split("::").last
end
|
Instance Method Details
#[](a) ⇒ Object
158
159
160
|
# File 'lib/cimi/models/resource.rb', line 158
def [](a)
@attribute_values[a]
end
|
#[]=(a, v) ⇒ Object
162
163
164
165
|
# File 'lib/cimi/models/resource.rb', line 162
def []=(a, v)
return @attribute_values.delete(a) if v.nil?
@attribute_values[a] = self.class.schema.convert(a, v)
end
|
#base_id ⇒ Object
188
189
190
|
# File 'lib/cimi/models/resource.rb', line 188
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.
170
171
172
173
174
175
176
177
178
179
|
# File 'lib/cimi/models/resource.rb', line 170
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
192
193
194
|
# File 'lib/cimi/models/resource.rb', line 192
def to_json
self.class.to_json(self)
end
|
#to_xml ⇒ Object
196
197
198
|
# File 'lib/cimi/models/resource.rb', line 196
def to_xml
self.class.to_xml(self)
end
|
#validate!(format = :xml) ⇒ Object
181
182
183
184
185
186
|
# File 'lib/cimi/models/resource.rb', line 181
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
|