You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@deltacloud.apache.org by mi...@mifo.sk on 2011/11/19 18:56:45 UTC
[PATCH core 2/2] CIMI: Switched to OpenStruct instead of Struct in 'hash' properties
From: Michal Fojtik <mf...@redhat.com>
---
server/lib/cimi/model/base.rb | 4 +---
server/lib/cimi/model/schema.rb | 25 ++++++++++++++++---------
2 files changed, 17 insertions(+), 12 deletions(-)
diff --git a/server/lib/cimi/model/base.rb b/server/lib/cimi/model/base.rb
index 7cc3f49..acc6499 100644
--- a/server/lib/cimi/model/base.rb
+++ b/server/lib/cimi/model/base.rb
@@ -163,7 +163,5 @@ class CIMI::Model::Base
text :uri, :name, :description, :created
# FIXME: this doesn't match with JSON
- hash :property, :content => :value do
- scalar :name
- end
+ hash :properties, :content => :value
end
diff --git a/server/lib/cimi/model/schema.rb b/server/lib/cimi/model/schema.rb
index 46d537b..334a6e4 100644
--- a/server/lib/cimi/model/schema.rb
+++ b/server/lib/cimi/model/schema.rb
@@ -14,6 +14,8 @@
# under the License.
#
+require 'ostruct'
+
# The smarts of converting from XML and JSON into internal objects
class CIMI::Model::Schema
@@ -173,29 +175,34 @@ class CIMI::Model::Schema
class Hash < Attribute
def initialize(name, opts = {}, &block)
- opts[:json_name] = name.to_s.pluralize unless opts[:json_name]
+ opts[:xml_name] = name.to_s.singularize unless opts[:xml_name]
super(name, opts)
- @struct = Struct.new(name, opts, &block)
end
def from_xml(xml, model)
- model[name] = (xml[xml_name] || []).map { |elt| @struct.convert_from_xml(elt) }
+ hsh = (xml[xml_name] || []).inject({}) do |result, item|
+ result[item['name']] = item['content']; result
+ end
+ model[name] = ::OpenStruct.new(hsh)
end
def from_json(json, model)
- model[name] = (json[json_name] || {}).inject([]) do |result,item|
- result << @struct.convert_from_json({ 'name' => item[0], 'value' => item[1] })
- end
+ model[name] = ::OpenStruct.new(json[json_name])
end
def to_xml(model, xml)
- ary = model[name].map { |elt| @struct.convert_to_xml(elt) }
+ ary = []
+ model[name].marshal_dump.each do |key, value|
+ ary << { 'name' => key.to_s, 'content' => value }
+ end
xml[xml_name] = ary unless ary.empty?
end
def to_json(model, json)
- ary = model[name].map { |elt| @struct.convert_to_json(elt) }
- json[json_name] = ary.inject({}) { |result, item| result[item['name']] = item['value']; result } unless ary.empty?
+ unless model[name].marshal_dump.keys.empty?
+ # We need to convert Symbol in keys to String
+ json[json_name] = model[name].marshal_dump.inject({}) { |result, item| result[item[0].to_s] = item[1]; result}
+ end
end
end
--
1.7.7.3