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, 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
#initialize ⇒ Schema
Returns a new instance of Schema.
277
278
279
|
# File 'lib/cimi/models/schema.rb', line 277
def initialize
@attributes = []
end
|
Instance Attribute Details
#attributes ⇒ Object
275
276
277
|
# File 'lib/cimi/models/schema.rb', line 275
def attributes
@attributes
end
|
Instance Method Details
#add_attributes!(args, attr_klass, &block) ⇒ Object
381
382
383
384
385
|
# File 'lib/cimi/models/schema.rb', line 381
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
313
314
315
316
317
318
|
# File 'lib/cimi/models/schema.rb', line 313
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
327
328
329
|
# File 'lib/cimi/models/schema.rb', line 327
def attribute_names
@attributes.map { |a| a.name }
end
|
#collections ⇒ Object
281
282
283
|
# File 'lib/cimi/models/schema.rb', line 281
def collections
@attributes.select { |a| a.is_a?(Collection) }
end
|
#convert(name, value) ⇒ Object
285
286
287
288
289
|
# File 'lib/cimi/models/schema.rb', line 285
def convert(name, value)
attr = @attributes.find { |a| a.name == name }
raise "Unknown attribute #{name}" unless attr
attr.convert(value)
end
|
#from_json(json, model = {}) ⇒ Object
297
298
299
300
301
|
# File 'lib/cimi/models/schema.rb', line 297
def from_json(json, model = {})
@attributes.freeze
@attributes.each { |attr| attr.from_json(json, model) }
model
end
|
#from_xml(xml, model = {}) ⇒ Object
291
292
293
294
295
|
# File 'lib/cimi/models/schema.rb', line 291
def from_xml(xml, model = {})
@attributes.freeze
@attributes.each { |attr| attr.from_xml(xml, model) }
model
end
|
#to_json(model, json = {}) ⇒ Object
320
321
322
323
324
325
|
# File 'lib/cimi/models/schema.rb', line 320
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
303
304
305
306
307
308
309
|
# File 'lib/cimi/models/schema.rb', line 303
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
|