Class: CIMI::Model::Resource

Inherits:
Object
  • Object
show all
Extended by:
Schema::DSL
Defined in:
lib/cimi/models/base.rb

Direct Known Subclasses

Base, Collection

Constant Summary collapse

CMWG_NAMESPACE =
"http://schemas.dmtf.org/cimi/1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Schema::DSL

array, collection, hash, href, resource_attr, scalar, struct, text

Constructor Details

#initialize(values = {}) ⇒ Resource

Factory methods



181
182
183
184
185
186
187
# File 'lib/cimi/models/base.rb', line 181

def initialize(values = {})
  names = self.class.schema.attribute_names
  @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_valuesObject (readonly)

We keep the values of the attributes in a hash



98
99
100
# File 'lib/cimi/models/base.rb', line 98

def attribute_values
  @attribute_values
end

Class Method Details

.<<(model) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/cimi/models/base.rb', line 107

def <<(model)
  clone_base_schema unless base_schema_cloned?
  member_name = model.name.split("::").last
  if ::Struct.const_defined?("CIMI_#{member_name}")
    ::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) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/cimi/models/base.rb', line 142

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



155
156
157
# File 'lib/cimi/models/base.rb', line 155

def all_uri(context)
  self.all(context).map { |e| { :href => e.id } }
end

.base_schemaObject



119
120
121
# File 'lib/cimi/models/base.rb', line 119

def base_schema
  @schema ||= CIMI::Model::Schema.new
end

.base_schema_cloned?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/cimi/models/base.rb', line 128

def base_schema_cloned?
  @schema_duped
end

.clone_base_schemaObject



123
124
125
126
# File 'lib/cimi/models/base.rb', line 123

def clone_base_schema
  @schema_duped = true
  @schema = Marshal::load(Marshal.dump(superclass.base_schema))
end

.from_json(text) ⇒ Object

Construct a new object



198
199
200
201
202
203
# File 'lib/cimi/models/base.rb', line 198

def self.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



190
191
192
193
194
195
# File 'lib/cimi/models/base.rb', line 190

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) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/cimi/models/base.rb', line 134

def inherited(child)
  child.instance_eval do
    def schema
      base_schema_cloned? ? @schema : clone_base_schema
    end
  end
end

.parse(text, content_type) ⇒ Object



205
206
207
208
209
210
211
212
213
# File 'lib/cimi/models/base.rb', line 205

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_uriObject



223
224
225
# File 'lib/cimi/models/base.rb', line 223

def self.resource_uri
  CMWG_NAMESPACE + "/" + self.name.split("::").last
end

.to_json(model) ⇒ Object



227
228
229
230
231
# File 'lib/cimi/models/base.rb', line 227

def self.to_json(model)
  json = @schema.to_json(model)
  json[:resourceURI] = resource_uri
  JSON::unparse(json)
end

.to_xml(model) ⇒ Object



233
234
235
236
237
238
# File 'lib/cimi/models/base.rb', line 233

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_nameObject

Serialize



219
220
221
# File 'lib/cimi/models/base.rb', line 219

def self.xml_tag_name
  self.name.split("::").last
end

Instance Method Details

#[](a) ⇒ Object



162
163
164
# File 'lib/cimi/models/base.rb', line 162

def [](a)
  @attribute_values[a]
end

#[]=(a, v) ⇒ Object



166
167
168
# File 'lib/cimi/models/base.rb', line 166

def []=(a, v)
  @attribute_values[a] = self.class.schema.convert(a, v)
end

#filter_by(filter_opts) ⇒ Object



248
249
250
251
252
253
254
255
256
257
# File 'lib/cimi/models/base.rb', line 248

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 /^([\w\_]+)$/ then filter_attributes([$1.intern])
    when /^([\w\_]+)\[(\d+\-\d+)\]$/ then filter_by_arr_range($1.intern, $2)
    when /^([\w\_]+)\[(\d+)\]$/ then filter_by_arr_index($1.intern, $2)
    else self
  end
end

#filter_by_arr_index(attr, filter) ⇒ Object



259
260
261
262
# File 'lib/cimi/models/base.rb', line 259

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) ⇒ Object



264
265
266
267
268
# File 'lib/cimi/models/base.rb', line 264

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

#prepareObject

Prepare to serialize



171
172
173
174
175
176
# File 'lib/cimi/models/base.rb', line 171

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_jsonObject



240
241
242
# File 'lib/cimi/models/base.rb', line 240

def to_json
  self.class.to_json(self)
end

#to_xmlObject



244
245
246
# File 'lib/cimi/models/base.rb', line 244

def to_xml
  self.class.to_xml(self)
end