Class: CIMI::Model::Schema::Collection
Instance Attribute Summary
Attributes inherited from Attribute
#json_name, #name, #required, #xml_name
Instance Method Summary
collapse
Methods inherited from Attribute
#required?, #valid?
Constructor Details
#initialize(name, opts = {}) ⇒ Collection
Returns a new instance of Collection.
297
298
299
300
301
302
303
304
305
306
|
# File 'lib/cimi/models/schema.rb', line 297
def initialize(name, opts = {})
params = {}
params[:scope] = opts.delete(:scope)
super(name, opts)
unless opts[:class]
raise "Specify the class of collection entries using :class"
end
params[:embedded] = true
@collection_class = CIMI::Model::Collection.generate(opts[:class], params)
end
|
Instance Method Details
#convert(value) ⇒ Object
Convert a Hash or Array to an instance of the collection class
341
342
343
344
345
346
347
|
# File 'lib/cimi/models/schema.rb', line 341
def convert(value)
if value.is_a?(::Array)
@collection_class.new(:entries => value)
else
@collection_class.new(value || {})
end
end
|
#from_json(json, model) ⇒ Object
314
315
316
317
318
|
# File 'lib/cimi/models/schema.rb', line 314
def from_json(json, model)
if json[json_name]
model[name] = @collection_class.schema.from_json(json[json_name], {})
end
end
|
#from_xml(xml, model) ⇒ Object
308
309
310
311
312
|
# File 'lib/cimi/models/schema.rb', line 308
def from_xml(xml, model)
if xml[xml_name]
model[name] = @collection_class.schema.from_xml(xml[xml_name].first, {})
end
end
|
#to_json(model, json) ⇒ Object
330
331
332
333
334
335
336
337
338
|
# File 'lib/cimi/models/schema.rb', line 330
def to_json(model, json)
return if model[name].nil?
model[name].prepare
if model[name].entries.empty?
json[json_name] = { "href" => model[name].href }
else
json[json_name] = @collection_class.schema.to_json(model[name])
end
end
|
#to_xml(model, xml) ⇒ Object
320
321
322
323
324
325
326
327
328
|
# File 'lib/cimi/models/schema.rb', line 320
def to_xml(model, xml)
return if model[name].nil?
model[name].prepare
if model[name].entries.empty?
xml[xml_name] = { "href" => model[name].href }
else
xml[xml_name] = @collection_class.schema.to_xml(model[name])
end
end
|