class CIMI::Model::Schema

The smarts of converting from XML and JSON into internal objects

Attributes

attributes[RW]

The actual Schema class

Public Class Methods

new() click to toggle source
# File lib/cimi/models/schema.rb, line 356
def initialize
  @attributes = []
end

Public Instance Methods

add_attributes!(args, attr_klass, &block) click to toggle source
# File lib/cimi/models/schema.rb, line 476
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) click to toggle source

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

# File lib/cimi/models/schema.rb, line 396
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() click to toggle source
# File lib/cimi/models/schema.rb, line 410
def attribute_names
  @attributes.map { |a| a.name }
end
collections() click to toggle source
# File lib/cimi/models/schema.rb, line 364
def collections
  @attributes.select { |a| a.is_a?(Collection) }
end
convert(name, value) click to toggle source
# File lib/cimi/models/schema.rb, line 368
def convert(name, value)
  attr = @attributes.find { |a| a.name == name }
  raise "Unknown attribute #{name}" unless attr
  attr.convert(value)
end
deep_clone() click to toggle source
# File lib/cimi/models/schema.rb, line 360
def deep_clone
  Marshal::load(Marshal.dump(self))
end
from_json(json, model = {}) click to toggle source
# File lib/cimi/models/schema.rb, line 380
def from_json(json, model = {})
  @attributes.freeze
  @attributes.each { |attr| attr.from_json(json, model) }
  model
end
from_xml(xml, model = {}) click to toggle source
# File lib/cimi/models/schema.rb, line 374
def from_xml(xml, model = {})
  @attributes.freeze
  @attributes.each { |attr| attr.from_xml(xml, model) }
  model
end
required_attributes() click to toggle source
# File lib/cimi/models/schema.rb, line 414
def required_attributes
  @attributes.select { |a| a.required? }
end
to_json(model, json = {}) click to toggle source
# File lib/cimi/models/schema.rb, line 403
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) click to toggle source
# File lib/cimi/models/schema.rb, line 386
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