Class: CIMI::Model::Schema::Collection

Inherits:
Attribute
  • Object
show all
Defined in:
lib/cimi/models/schema.rb

Instance Attribute Summary

Attributes inherited from Attribute

#json_name, #name, #xml_name

Instance Method Summary collapse

Constructor Details

#initialize(name, opts = {}) ⇒ Collection

Returns a new instance of Collection.



223
224
225
226
227
228
229
230
231
232
# File 'lib/cimi/models/schema.rb', line 223

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



267
268
269
270
271
272
273
# File 'lib/cimi/models/schema.rb', line 267

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



240
241
242
243
244
# File 'lib/cimi/models/schema.rb', line 240

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



234
235
236
237
238
# File 'lib/cimi/models/schema.rb', line 234

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



256
257
258
259
260
261
262
263
264
# File 'lib/cimi/models/schema.rb', line 256

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



246
247
248
249
250
251
252
253
254
# File 'lib/cimi/models/schema.rb', line 246

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