Class: CIMI::Model::Schema
- Inherits:
-
Object
- Object
- CIMI::Model::Schema
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, Ref, Scalar, Struct
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from DSL
#array, #collection, #hash_map, #href, #ref, #scalar, #struct, #text
Constructor Details
#initialize ⇒ Schema
Returns a new instance of Schema.
361
362
363
|
# File 'lib/cimi/models/schema.rb', line 361
def initialize
@attributes = []
end
|
Instance Attribute Details
#attributes ⇒ Object
359
360
361
|
# File 'lib/cimi/models/schema.rb', line 359
def attributes
@attributes
end
|
Instance Method Details
#add_attributes!(args, attr_klass, &block) ⇒ Object
472
473
474
475
476
|
# File 'lib/cimi/models/schema.rb', line 472
def add_attributes!(args, attr_klass, &block)
raise "The schema has already been used to convert objects" if @attributes.frozen?
opts = args.
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
401
402
403
404
405
406
|
# File 'lib/cimi/models/schema.rb', line 401
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_names ⇒ Object
415
416
417
|
# File 'lib/cimi/models/schema.rb', line 415
def attribute_names
@attributes.map { |a| a.name }
end
|
#collections ⇒ Object
369
370
371
|
# File 'lib/cimi/models/schema.rb', line 369
def collections
@attributes.select { |a| a.is_a?(Collection) }
end
|
#convert(name, value) ⇒ Object
373
374
375
376
377
|
# File 'lib/cimi/models/schema.rb', line 373
def convert(name, value)
attr = @attributes.find { |a| a.name == name }
raise "Unknown attribute #{name}" unless attr
attr.convert(value)
end
|
#deep_clone ⇒ Object
365
366
367
|
# File 'lib/cimi/models/schema.rb', line 365
def deep_clone
Marshal::load(Marshal.dump(self))
end
|
#from_json(json, model = {}) ⇒ Object
385
386
387
388
389
|
# File 'lib/cimi/models/schema.rb', line 385
def from_json(json, model = {})
@attributes.freeze
@attributes.each { |attr| attr.from_json(json, model) }
model
end
|
#from_xml(xml, model = {}) ⇒ Object
379
380
381
382
383
|
# File 'lib/cimi/models/schema.rb', line 379
def from_xml(xml, model = {})
@attributes.freeze
@attributes.each { |attr| attr.from_xml(xml, model) }
model
end
|
#required_attributes ⇒ Object
419
420
421
|
# File 'lib/cimi/models/schema.rb', line 419
def required_attributes
@attributes.select { |a| a.required? }
end
|
#to_json(model, json = {}) ⇒ Object
408
409
410
411
412
413
|
# File 'lib/cimi/models/schema.rb', line 408
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
391
392
393
394
395
396
397
|
# File 'lib/cimi/models/schema.rb', line 391
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
|