Class: CIMI::Model::Schema::Hash
Instance Attribute Summary
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) ⇒ Hash
Returns a new instance of Hash.
268
269
270
271
|
# File 'lib/cimi/models/schema.rb', line 268
def initialize(name, opts = {}, &block)
opts[:json_name] = name.to_s.pluralize unless opts[:json_name]
super(name, opts)
end
|
Instance Method Details
#from_json(json, model) ⇒ Object
280
281
282
|
# File 'lib/cimi/models/schema.rb', line 280
def from_json(json, model)
model[name] = json[json_name] || {}
end
|
#from_xml(xml, model) ⇒ Object
273
274
275
276
277
278
|
# File 'lib/cimi/models/schema.rb', line 273
def from_xml(xml, model)
model[name] = (xml[xml_name] || []).inject({}) do |result, item|
result[item["key"]] = item["content"]
result
end
end
|
#to_json(model, json) ⇒ Object
289
290
291
292
293
|
# File 'lib/cimi/models/schema.rb', line 289
def to_json(model, json)
if model[name] && ! model[name].empty?
json[json_name] = model[name]
end
end
|
#to_xml(model, xml) ⇒ Object
284
285
286
287
|
# File 'lib/cimi/models/schema.rb', line 284
def to_xml(model, xml)
ary = (model[name] || {}).map { |k, v| { "key" => k, "content" => v } }
xml[xml_name] = ary unless ary.empty?
end
|