Class: CIMI::Model::Schema

Inherits:
Object
  • Object
show all
Includes:
DSL
Defined in:
lib/cimi/models/schema.rb

Overview

The smarts of converting from XML and JSON into internal objects

Defined Under Namespace

Modules: DSL Classes: Array, Attribute, Collection, Hash, Scalar, Struct

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DSL

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

Constructor Details

#initializeSchema

Returns a new instance of Schema.



282
283
284
# File 'lib/cimi/models/schema.rb', line 282

def initialize
  @attributes = []
end

Instance Attribute Details

#attributesObject

The actual Schema class



280
281
282
# File 'lib/cimi/models/schema.rb', line 280

def attributes
  @attributes
end

Instance Method Details

#add_attributes!(args, attr_klass, &block) ⇒ Object



390
391
392
393
394
# File 'lib/cimi/models/schema.rb', line 390

def add_attributes!(args, attr_klass, &block)
  raise "The schema has already been used to convert objects" if @attributes.frozen?
  opts = args.extract_opts!
  args.each { |arg| @attributes << attr_klass.new(arg, opts, &block) }
end

#add_collection_member_array(model) ⇒ Object

For MachineCollection, copy over the schema of Machine to hold each member of the collection - avoid duplicating the schemas



322
323
324
325
326
327
# File 'lib/cimi/models/schema.rb', line 322

def add_collection_member_array(model)
  member_symbol = model.name.split("::").last.underscore.pluralize.to_sym
  members = CIMI::Model::Schema::Array.new(member_symbol)
  members.struct.schema.attributes = model.schema.attributes
  self.attributes << members
end

#attribute_namesObject



336
337
338
# File 'lib/cimi/models/schema.rb', line 336

def attribute_names
  @attributes.map { |a| a.name }
end

#collectionsObject



290
291
292
# File 'lib/cimi/models/schema.rb', line 290

def collections
  @attributes.select { |a| a.is_a?(Collection) }
end

#convert(name, value) ⇒ Object



294
295
296
297
298
# File 'lib/cimi/models/schema.rb', line 294

def convert(name, value)
  attr = @attributes.find { |a| a.name == name }
  raise "Unknown attribute #{name}" unless attr
  attr.convert(value)
end

#deep_cloneObject



286
287
288
# File 'lib/cimi/models/schema.rb', line 286

def deep_clone
  Marshal::load(Marshal.dump(self))
end

#from_json(json, model = {}) ⇒ Object



306
307
308
309
310
# File 'lib/cimi/models/schema.rb', line 306

def from_json(json, model = {})
  @attributes.freeze
  @attributes.each { |attr| attr.from_json(json, model) }
  model
end

#from_xml(xml, model = {}) ⇒ Object



300
301
302
303
304
# File 'lib/cimi/models/schema.rb', line 300

def from_xml(xml, model = {})
  @attributes.freeze
  @attributes.each { |attr| attr.from_xml(xml, model) }
  model
end

#to_json(model, json = {}) ⇒ Object



329
330
331
332
333
334
# File 'lib/cimi/models/schema.rb', line 329

def to_json(model, json = {})
  @attributes.freeze
  model.prepare if model.respond_to?(:prepare)
  @attributes.each { |attr| attr.to_json(model, json) }
  json
end

#to_xml(model, xml = nil) ⇒ Object



312
313
314
315
316
317
318
# File 'lib/cimi/models/schema.rb', line 312

def to_xml(model, xml = nil)
  xml ||= OrderedHash.new
  @attributes.freeze
  model.prepare if model.respond_to?(:prepare)
  @attributes.each { |attr| attr.to_xml(model, xml) }
  xml
end