deltacloud-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@apache.org
Subject svn commit: r1203695 - in /incubator/deltacloud/trunk/server/lib/cimi/model: base.rb schema.rb
Date Fri, 18 Nov 2011 15:28:41 GMT
Author: mfojtik
Date: Fri Nov 18 15:28:41 2011
New Revision: 1203695

URL: http://svn.apache.org/viewvc?rev=1203695&view=rev
Log:
CIMI: Added new type 'hash' to deal with properties

Modified:
    incubator/deltacloud/trunk/server/lib/cimi/model/base.rb
    incubator/deltacloud/trunk/server/lib/cimi/model/schema.rb

Modified: incubator/deltacloud/trunk/server/lib/cimi/model/base.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/lib/cimi/model/base.rb?rev=1203695&r1=1203694&r2=1203695&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/lib/cimi/model/base.rb (original)
+++ incubator/deltacloud/trunk/server/lib/cimi/model/base.rb Fri Nov 18 15:28:41 2011
@@ -163,7 +163,7 @@ class CIMI::Model::Base
   text :uri, :name, :description, :created
 
   # FIXME: this doesn't match with JSON
-  array :property, :content => :value do
+  hash :property, :content => :value do
     scalar :name
   end
 end

Modified: incubator/deltacloud/trunk/server/lib/cimi/model/schema.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/lib/cimi/model/schema.rb?rev=1203695&r1=1203694&r2=1203695&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/lib/cimi/model/schema.rb (original)
+++ incubator/deltacloud/trunk/server/lib/cimi/model/schema.rb Fri Nov 18 15:28:41 2011
@@ -170,6 +170,35 @@ class CIMI::Model::Schema
     end
   end
 
+  class Hash < Attribute
+
+    def initialize(name, opts = {}, &block)
+      opts[:json_name] = name.to_s.pluralize unless opts[:json_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) }
+    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
+    end
+
+    def to_xml(model, xml)
+      ary = model[name].map { |elt| @struct.convert_to_xml(elt) }
+      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?
+    end
+  end
+
   #
   # The actual Schema class
   #
@@ -231,6 +260,10 @@ class CIMI::Model::Schema
     def struct(name, opts={}, &block)
       add_attributes!([name, opts], Struct, &block)
     end
+
+    def hash(name, opts={}, &block)
+      add_attributes!([name, opts], Hash, &block)
+    end
   end
 
   include DSL



Mime
View raw message