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, #xml_name

Instance Method Summary collapse

Methods inherited from Attribute

#convert

Constructor Details

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

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



165
166
167
168
169
170
171
# File 'lib/cimi/models/schema.rb', line 165

def initialize(name, opts = {}, &block)
  unless opts[:xml_name]
    opts[:xml_name] = name.to_s.singularize.camelize.uncapitalize
  end
  super(name, opts)
  @struct = Struct.new(name, opts, &block)
end

Instance Attribute Details

#structObject

Returns the value of attribute struct.



161
162
163
# File 'lib/cimi/models/schema.rb', line 161

def struct
  @struct
end

Instance Method Details

#from_json(json, model) ⇒ Object



177
178
179
# File 'lib/cimi/models/schema.rb', line 177

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

#from_xml(xml, model) ⇒ Object



173
174
175
# File 'lib/cimi/models/schema.rb', line 173

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

#to_json(model, json) ⇒ Object



186
187
188
189
# File 'lib/cimi/models/schema.rb', line 186

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



181
182
183
184
# File 'lib/cimi/models/schema.rb', line 181

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