incubator-deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@redhat.com
Subject [PATCH core 3/3] CIMI: Added rename_key_fields! method to change names for given properties in JSON format
Date Fri, 21 Oct 2011 14:05:41 GMT
From: Michal Fojtik <mfojtik@redhat.com>


Signed-off-by: Michal fojtik <mfojtik@redhat.com>
---
 server/lib/cimi/helpers/cmwg_helper.rb |   27 +++++++++++++++++++++++++++
 1 files changed, 27 insertions(+), 0 deletions(-)

diff --git a/server/lib/cimi/helpers/cmwg_helper.rb b/server/lib/cimi/helpers/cmwg_helper.rb
index 0a9b1e9..b7d5b81 100644
--- a/server/lib/cimi/helpers/cmwg_helper.rb
+++ b/server/lib/cimi/helpers/cmwg_helper.rb
@@ -72,8 +72,35 @@ module CMWG
       engine = Tilt::HamlTemplate.new(File.join(settings.views, resource_name.to_s.pluralize,
'show.xml.haml'))
       hash_response = XmlSimple.xml_in(engine.render(self, :'@object' => object))
       hash_response.delete('xmlns')
+      rename_key_fields!(hash_response, replacement_keys_for(resource_name))
       hash_response.to_json
     end
 
+    private
+
+    def replacement_keys_for(resource)
+      case resource
+        when :machine_image then {"property" => "properties", "operation" => "operations"}
+        when :machine then {"property" => "properties", "disk" => "disks", "operation"
=> "operations"}
+        when :colume then {"property" => "properties", "operation" => "operations"}
+      end
+    end
+
+    def rename_key_fields!(an_object, key_maps = nil)
+      if an_object.kind_of?(Hash)
+        key_maps.each do |key, value|
+          an_object[value] = an_object.delete(key) if an_object.key?(key)
+        end
+        an_object.each do |key, value|
+          rename_key_fields!(value, key_maps)
+        end
+      elsif an_object.kind_of?(Array)
+        an_object.each do |value|
+          rename_key_fields!(value, key_maps)
+        end
+      end
+    end
+
+
   end
 end
-- 
1.7.4.4


Mime
View raw message