Class: CIMI::Model::Schema::Array

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

Instance Attribute Summary collapse

Attributes inherited from Attribute

#json_name, #name, #required, #xml_name

Instance Method Summary collapse

Methods inherited from Attribute

#convert, #required?, #valid?

Constructor Details

#initialize(name, opts = {}, &block) ⇒ Array

For an array :funThings, we collect all <funThing/> elements (XmlSimple actually does the collecting)



232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/cimi/models/schema.rb', line 232

def initialize(name, opts = {}, &block)
  unless opts[:xml_name]
    opts[:xml_name] = name.to_s.singularize.camelize.uncapitalize
  end
  if opts[:ref] && block_given?
    raise "Provide only one of :ref or a block"
  end
  super(name, opts)
  if opts[:ref]
    @struct = Ref.new(name, :class=> opts[:ref])
  else
    @struct = Struct.new(name, opts, &block)
  end
end

Instance Attribute Details

#structObject

Returns the value of attribute struct.



228
229
230
# File 'lib/cimi/models/schema.rb', line 228

def struct
  @struct
end

Instance Method Details

#from_json(json, model) ⇒ Object



251
252
253
# File 'lib/cimi/models/schema.rb', line 251

def from_json(json, model)
  model[name] = (json[json_name] || []).map { |elt| @struct.convert_from_json(elt) }
end

#from_xml(xml, model) ⇒ Object



247
248
249
# File 'lib/cimi/models/schema.rb', line 247

def from_xml(xml, model)
  model[name] = (xml[xml_name] || []).map { |elt| @struct.convert_from_xml(elt) }
end

#to_json(model, json) ⇒ Object



260
261
262
263
# File 'lib/cimi/models/schema.rb', line 260

def to_json(model, json)
  ary = (model[name] || []).map { |elt| @struct.convert_to_json(elt) }
  json[json_name] = ary unless ary.empty?
end

#to_xml(model, xml) ⇒ Object



255
256
257
258
# File 'lib/cimi/models/schema.rb', line 255

def to_xml(model, xml)
  ary = (model[name] || []).map { |elt| @struct.convert_to_xml(elt) }
  xml[xml_name] = ary unless ary.empty?
end