You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@deltacloud.apache.org by mf...@redhat.com on 2011/11/14 16:09:27 UTC

[PATCH core 1/5] CIMI: Replaced elsif by case, switched block syntax

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


Signed-off-by: Michal fojtik <mf...@redhat.com>
---
 server/lib/cimi/model.rb        |    2 +
 server/lib/cimi/model/base.rb   |    4 +-
 server/lib/cimi/model/schema.rb |   42 +++++++++++++--------------------------
 3 files changed, 18 insertions(+), 30 deletions(-)

diff --git a/server/lib/cimi/model.rb b/server/lib/cimi/model.rb
index c1d7f02..fcc6e17 100644
--- a/server/lib/cimi/model.rb
+++ b/server/lib/cimi/model.rb
@@ -14,6 +14,8 @@
 # under the License.
 #
 
+# Declare namespace for CIMI model
+#
 module CIMI
   module Model; end
 end
diff --git a/server/lib/cimi/model/base.rb b/server/lib/cimi/model/base.rb
index 0ef2b27..de6529b 100644
--- a/server/lib/cimi/model/base.rb
+++ b/server/lib/cimi/model/base.rb
@@ -163,7 +163,7 @@ class CIMI::Model::Base
   text :uri, :name, :description, :created
 
   # FIXME: this doesn't match with JSON
-  array :properties, :content => :value do
-    scalar :key
+  array :property, :content => :value do
+    scalar :name
   end
 end
diff --git a/server/lib/cimi/model/schema.rb b/server/lib/cimi/model/schema.rb
index e470121..d8520d5 100644
--- a/server/lib/cimi/model/schema.rb
+++ b/server/lib/cimi/model/schema.rb
@@ -60,23 +60,19 @@ class CIMI::Model::Schema
     def nested_text?; @text == :nested; end
 
     def from_xml(xml, model)
-      if @text == :nested
-        model[@name] = xml[@xml_name].first["content"] if xml[@xml_name]
-      elsif @text == :direct
-        model[@name] = xml["content"]
-      else
-        model[@name] = xml[@xml_name]
+      case @text
+        when :nested : model[@name] = xml[@xml_name].first["content"] if xml[@xml_name]
+        when :direct : model[@name] = xml["content"]
+        else model[@name] = xml[@xml_name]
       end
     end
 
     def to_xml(model, xml)
       return unless model[@name]
-      if @text == :nested
-        xml[@xml_name] = [{ "content" => model[@name] }]
-      elsif @text == :direct
-        xml["content"] = model[@name]
-      else
-        xml[@xml_name] = model[@name]
+      case @text
+        when :nested : xml[@xml_name] = [{ "content" => model[@name] }]
+        when :direct : xml["content"] = model[@name]
+        else xml[@xml_name] = model[@name]
       end
     end
   end
@@ -84,7 +80,7 @@ class CIMI::Model::Schema
   class Struct < Attribute
     def initialize(name, opts, &block)
       content = opts[:content]
-      super(name)
+      super(name, opts)
       @schema = CIMI::Model::Schema.new
       @schema.instance_eval(&block) if block_given?
       @schema.scalar(content, :text => :direct) if content
@@ -156,15 +152,11 @@ class CIMI::Model::Schema
     end
 
     def from_xml(xml, model)
-      model[name] = (xml[xml_name] || []).map do |elt|
-        @struct.convert_from_xml(elt)
-      end
+      model[name] = (xml[xml_name] || []).map { |elt| @struct.convert_from_xml(elt) }
     end
 
     def from_json(json, model)
-      model[name] = (json[json_name] || []).map do |elt|
-        @struct.convert_from_json(elt)
-      end
+      model[name] = (json[json_name] || []).map { |elt| @struct.convert_from_json(elt) }
     end
 
     def to_xml(model, xml)
@@ -220,9 +212,7 @@ class CIMI::Model::Schema
   # +add_attributes!+ method
   module DSL
     def href(*args)
-      args.each do |arg|
-        struct(arg) { scalar :href }
-      end
+      args.each { |arg| struct(arg) { scalar :href } }
     end
 
     def text(*args)
@@ -246,12 +236,8 @@ class CIMI::Model::Schema
   include DSL
 
   def add_attributes!(args, attr_klass, &block)
-    if @attributes.frozen?
-      raise "The schema has already been used to convert objects"
-    end
+    raise "The schema has already been used to convert objects" if @attributes.frozen?
     opts = args.extract_opts!
-    args.each do |arg|
-      @attributes << attr_klass.new(arg, opts, &block)
-    end
+    args.each { |arg| @attributes << attr_klass.new(arg, opts, &block) }
   end
 end
-- 
1.7.4.4


Re: [PATCH core 1/5] CIMI: Replaced elsif by case, switched block syntax

Posted by David Lutterkort <lu...@redhat.com>.
On Wed, 2011-11-16 at 13:21 +0200, marios@redhat.com wrote:
> minor comment inline:
> 
> On 14/11/11 17:09, mfojtik@redhat.com wrote:
> > From: Michal Fojtik <mf...@redhat.com>
> > diff --git a/server/lib/cimi/model/base.rb b/server/lib/cimi/model/base.rb
> > index 0ef2b27..de6529b 100644
> > --- a/server/lib/cimi/model/base.rb
> > +++ b/server/lib/cimi/model/base.rb
> > @@ -163,7 +163,7 @@ class CIMI::Model::Base
> >    text :uri, :name, :description, :created
> >  
> >    # FIXME: this doesn't match with JSON
> > -  array :properties, :content => :value do
> > -    scalar :key
> > +  array :property, :content => :value do
> > +    scalar :name
> 
> This was changed to :key and I think it makes more sense for it to be
> :properties since it's meant to be a 'set of properties'.
> 
> JSON:  "properties": { "key": string, + }, ?
> XML: <property key="xs:string"> xs:string </property> *

The DSL doesn't have a way to deal with properties right now; we'll need
to add something that handles what is a hash in JSON, and the encoding
of a hash as a sequence of tags in XML. Just put a big FIXME next to
properties for now - they are not critical.

David




Re: [PATCH core 1/5] CIMI: Replaced elsif by case, switched block syntax

Posted by "marios@redhat.com" <ma...@redhat.com>.
minor comment inline:

On 14/11/11 17:09, mfojtik@redhat.com wrote:
> From: Michal Fojtik <mf...@redhat.com>
> diff --git a/server/lib/cimi/model/base.rb b/server/lib/cimi/model/base.rb
> index 0ef2b27..de6529b 100644
> --- a/server/lib/cimi/model/base.rb
> +++ b/server/lib/cimi/model/base.rb
> @@ -163,7 +163,7 @@ class CIMI::Model::Base
>    text :uri, :name, :description, :created
>  
>    # FIXME: this doesn't match with JSON
> -  array :properties, :content => :value do
> -    scalar :key
> +  array :property, :content => :value do
> +    scalar :name

This was changed to :key and I think it makes more sense for it to be
:properties since it's meant to be a 'set of properties'.

JSON:  "properties": { "key": string, + }, ?
XML: <property key="xs:string"> xs:string </property> *

otherwise ACK