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.



194
195
196
197
# File 'lib/cimi/models/schema.rb', line 194

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



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

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

#from_xml(xml, model) ⇒ Object



199
200
201
202
203
204
# File 'lib/cimi/models/schema.rb', line 199

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



215
216
217
218
219
# File 'lib/cimi/models/schema.rb', line 215

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

#to_xml(model, xml) ⇒ Object



210
211
212
213
# File 'lib/cimi/models/schema.rb', line 210

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