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.
299
300
301
302
303
304
305
306
307
308
309
310
311
|
# File 'lib/cimi/models/schema.rb', line 299
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
unless opts[:class].collection_class
opts[:class].collection_class = CIMI::Model::Collection.generate(opts[:class], params)
end
@collection_class = opts[:class].collection_class
end
|
Instance Method Details
#convert(value) ⇒ Object
Convert a Hash or Array to an instance of the collection class
346
347
348
349
350
351
352
|
# File 'lib/cimi/models/schema.rb', line 346
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
319
320
321
322
323
|
# File 'lib/cimi/models/schema.rb', line 319
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
313
314
315
316
317
|
# File 'lib/cimi/models/schema.rb', line 313
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
335
336
337
338
339
340
341
342
343
|
# File 'lib/cimi/models/schema.rb', line 335
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
325
326
327
328
329
330
331
332
333
|
# File 'lib/cimi/models/schema.rb', line 325
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
|