Class: CIMI::Model::Schema::Hash

Inherits:
Attribute 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

Methods inherited from Attribute

#convert

Constructor Details

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

Returns a new instance of Hash.



191
192
193
194
# File 'lib/cimi/models/schema.rb', line 191

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



203
204
205
# File 'lib/cimi/models/schema.rb', line 203

def from_json(json, model)
  model[name] = json[json_name] || {}
end

#from_xml(xml, model) ⇒ Object



196
197
198
199
200
201
# File 'lib/cimi/models/schema.rb', line 196

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



212
213
214
215
216
# File 'lib/cimi/models/schema.rb', line 212

def to_json(model, json)
  if model[name] && ! model[name].empty?
    json[json_name] = model[name]
  end
end

#to_xml(model, xml) ⇒ Object



207
208
209
210
# File 'lib/cimi/models/schema.rb', line 207

def to_xml(model, xml)
  ary = (model[name] || {}).map { |k, v| { "key" => k, "content" => v } }
  xml[xml_name] = ary unless ary.empty?
end