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:44 UTC

[PATCH core 1/2] Core: Fixed String core_ext to handle singularization of 'properties' kind of strings

From: Michal Fojtik <mf...@redhat.com>

---
 server/lib/deltacloud/core_ext/string.rb |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)

diff --git a/server/lib/deltacloud/core_ext/string.rb b/server/lib/deltacloud/core_ext/string.rb
index de56c99..7d7ae17 100644
--- a/server/lib/deltacloud/core_ext/string.rb
+++ b/server/lib/deltacloud/core_ext/string.rb
@@ -40,6 +40,7 @@ class String
 
   def singularize
     return self.gsub(/es$/, '') if self =~ /sses$/
+    return self.gsub(/ties$/, 'ty') if self =~ /ties$/
     self.gsub(/s$/, '')
   end
 
-- 
1.7.7.3


[PATCH core 2/2] CIMI: Switched to OpenStruct instead of Struct in 'hash' properties

Posted by mi...@mifo.sk.
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