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, href, ref, resource_attr, 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
|
.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
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
123
124
125
126
127
128
129
130
|
# 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 ⇒ Object
157
158
159
|
# File 'lib/cimi/models/resource.rb', line 157
def required_attributes
@schema.required_attributes
end
|
.resource_uri ⇒ Object
140
141
142
|
# File 'lib/cimi/models/resource.rb', line 140
def resource_uri
CMWG_NAMESPACE + "/" + self.name.split("::").last
end
|
.to_json(model) ⇒ Object
144
145
146
147
148
|
# 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) ⇒ Object
150
151
152
153
154
155
|
# 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 ⇒ Object
136
137
138
|
# File 'lib/cimi/models/resource.rb', line 136
def xml_tag_name
self.name.split("::").last
end
|
Instance Method Details
#[](a) ⇒ Object
164
165
166
|
# File 'lib/cimi/models/resource.rb', line 164
def [](a)
@attribute_values[a]
end
|
#[]=(a, v) ⇒ Object
168
169
170
171
|
# 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 ⇒ Object
194
195
196
|
# File 'lib/cimi/models/resource.rb', line 194
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.
176
177
178
179
180
181
182
183
184
185
|
# 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 ⇒ Object
198
199
200
|
# File 'lib/cimi/models/resource.rb', line 198
def to_json
self.class.to_json(self)
end
|
#to_xml ⇒ Object
202
203
204
|
# File 'lib/cimi/models/resource.rb', line 202
def to_xml
self.class.to_xml(self)
end
|
#validate!(format = :xml) ⇒ Object
187
188
189
190
191
192
|
# 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
|