You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@deltacloud.apache.org by tc...@redhat.com on 2010/12/01 23:27:47 UTC

[PATCH client] Namespace dynamically generated classes by type.

From: Tobias Crawley <tc...@redhat.com>

This resolves an issue where based on the results of one request,
the client will create an Instance class as a subclass of StateFullObject
then, based on the results of another request try to create an Instance
as a subclass of ActionObject, causing a superclass mismatch error.

This patch groups the dynamic classes inside nested containing classes
inside DeltaCloud::API, preventing collisions.
---
 client/lib/base_object.rb |   27 +++++++++++++++------------
 1 files changed, 15 insertions(+), 12 deletions(-)

diff --git a/client/lib/base_object.rb b/client/lib/base_object.rb
index 295968c..93c35cc 100644
--- a/client/lib/base_object.rb
+++ b/client/lib/base_object.rb
@@ -192,7 +192,7 @@ module DeltaCloud
 
     end
 
-    class StateFullObject < ActionObject
+    class StatefulObject < ActionObject
       attr_reader :state
 
       def initialize(opts={}, &block)
@@ -263,25 +263,28 @@ module DeltaCloud
     end
 
     def self.add_class(name, parent=:base)
-      parent_class = case parent
-        when :base then 'BaseObject'
-        when :action then 'ActionObject'
-        when :state then 'StateFullObject'
-      end
+      parent = parent.to_s
+      parent_class = "#{parent.classify}Object"
       @defined_classes ||= []
-      if @defined_classes.include?(name)
-        DeltaCloud::API.class_eval("#{name.classify}")
-      else
-        DeltaCloud::API.class_eval("class #{name.classify} < DeltaCloud::#{parent_class}; end")
-        DeltaCloud::API.const_get("#{name.classify}")
+      class_name = "#{parent.classify}::#{name.classify}"
+      unless @defined_classes.include?(class_name)
+        DeltaCloud::API.class_eval("class #{class_name} < DeltaCloud::#{parent_class}; end")
+        @defined_classes << class_name
       end
+      
+      DeltaCloud::API.const_get(parent.classify).const_get(name.classify)
     end
 
     def self.guess_model_type(response)
       response = Nokogiri::XML(response.to_s)
       return :action if ((response/'//actions').length == 1) and ((response/'//state').length == 0)
-      return :state if ((response/'//actions').length == 1) and ((response/'//state').length == 1)
+      return :stateful if ((response/'//actions').length == 1) and ((response/'//state').length == 1)
       return :base
     end
 
+    class API
+      class Action; end
+      class Base; end
+      class Stateful; end
+    end
 end
-- 
1.7.3.2


Re: [PATCH client] Namespace dynamically generated classes by type.

Posted by Michal Fojtik <mf...@redhat.com>.
On 02/12/10 10:29 +0100, Michal Fojtik wrote:
>On 01/12/10 17:27 -0500, tcrawley@redhat.com wrote:
>>From: Tobias Crawley <tc...@redhat.com>
>>
>>This resolves an issue where based on the results of one request,
>>the client will create an Instance class as a subclass of StateFullObject
>>then, based on the results of another request try to create an Instance
>>as a subclass of ActionObject, causing a superclass mismatch error.
>
>ACK. Nice catch Tobi.

Pushed into master.

   -- Michal

>
>>
>>This patch groups the dynamic classes inside nested containing classes
>>inside DeltaCloud::API, preventing collisions.
>>---
>>client/lib/base_object.rb |   27 +++++++++++++++------------
>>1 files changed, 15 insertions(+), 12 deletions(-)
>>
>>diff --git a/client/lib/base_object.rb b/client/lib/base_object.rb
>>index 295968c..93c35cc 100644
>>--- a/client/lib/base_object.rb
>>+++ b/client/lib/base_object.rb
>>@@ -192,7 +192,7 @@ module DeltaCloud
>>
>>    end
>>
>>-    class StateFullObject < ActionObject
>>+    class StatefulObject < ActionObject
>>      attr_reader :state
>>
>>      def initialize(opts={}, &block)
>>@@ -263,25 +263,28 @@ module DeltaCloud
>>    end
>>
>>    def self.add_class(name, parent=:base)
>>-      parent_class = case parent
>>-        when :base then 'BaseObject'
>>-        when :action then 'ActionObject'
>>-        when :state then 'StateFullObject'
>>-      end
>>+      parent = parent.to_s
>>+      parent_class = "#{parent.classify}Object"
>>      @defined_classes ||= []
>>-      if @defined_classes.include?(name)
>>-        DeltaCloud::API.class_eval("#{name.classify}")
>>-      else
>>-        DeltaCloud::API.class_eval("class #{name.classify} < DeltaCloud::#{parent_class}; end")
>>-        DeltaCloud::API.const_get("#{name.classify}")
>>+      class_name = "#{parent.classify}::#{name.classify}"
>>+      unless @defined_classes.include?(class_name)
>>+        DeltaCloud::API.class_eval("class #{class_name} < DeltaCloud::#{parent_class}; end")
>>+        @defined_classes << class_name
>>      end
>>+
>>+      DeltaCloud::API.const_get(parent.classify).const_get(name.classify)
>>    end
>>
>>    def self.guess_model_type(response)
>>      response = Nokogiri::XML(response.to_s)
>>      return :action if ((response/'//actions').length == 1) and ((response/'//state').length == 0)
>>-      return :state if ((response/'//actions').length == 1) and ((response/'//state').length == 1)
>>+      return :stateful if ((response/'//actions').length == 1) and ((response/'//state').length == 1)
>>      return :base
>>    end
>>
>>+    class API
>>+      class Action; end
>>+      class Base; end
>>+      class Stateful; end
>>+    end
>>end
>>--
>>1.7.3.2
>>
>
>-- 
>--------------------------------------------------------
>Michal Fojtik, mfojtik@redhat.com
>Deltacloud API: http://deltacloud.org
>--------------------------------------------------------

-- 
--------------------------------------------------------
Michal Fojtik, mfojtik@redhat.com
Deltacloud API: http://deltacloud.org
--------------------------------------------------------

Re: [PATCH client] Namespace dynamically generated classes by type.

Posted by Michal Fojtik <mf...@redhat.com>.
On 01/12/10 17:27 -0500, tcrawley@redhat.com wrote:
>From: Tobias Crawley <tc...@redhat.com>
>
>This resolves an issue where based on the results of one request,
>the client will create an Instance class as a subclass of StateFullObject
>then, based on the results of another request try to create an Instance
>as a subclass of ActionObject, causing a superclass mismatch error.

ACK. Nice catch Tobi.

>
>This patch groups the dynamic classes inside nested containing classes
>inside DeltaCloud::API, preventing collisions.
>---
> client/lib/base_object.rb |   27 +++++++++++++++------------
> 1 files changed, 15 insertions(+), 12 deletions(-)
>
>diff --git a/client/lib/base_object.rb b/client/lib/base_object.rb
>index 295968c..93c35cc 100644
>--- a/client/lib/base_object.rb
>+++ b/client/lib/base_object.rb
>@@ -192,7 +192,7 @@ module DeltaCloud
>
>     end
>
>-    class StateFullObject < ActionObject
>+    class StatefulObject < ActionObject
>       attr_reader :state
>
>       def initialize(opts={}, &block)
>@@ -263,25 +263,28 @@ module DeltaCloud
>     end
>
>     def self.add_class(name, parent=:base)
>-      parent_class = case parent
>-        when :base then 'BaseObject'
>-        when :action then 'ActionObject'
>-        when :state then 'StateFullObject'
>-      end
>+      parent = parent.to_s
>+      parent_class = "#{parent.classify}Object"
>       @defined_classes ||= []
>-      if @defined_classes.include?(name)
>-        DeltaCloud::API.class_eval("#{name.classify}")
>-      else
>-        DeltaCloud::API.class_eval("class #{name.classify} < DeltaCloud::#{parent_class}; end")
>-        DeltaCloud::API.const_get("#{name.classify}")
>+      class_name = "#{parent.classify}::#{name.classify}"
>+      unless @defined_classes.include?(class_name)
>+        DeltaCloud::API.class_eval("class #{class_name} < DeltaCloud::#{parent_class}; end")
>+        @defined_classes << class_name
>       end
>+
>+      DeltaCloud::API.const_get(parent.classify).const_get(name.classify)
>     end
>
>     def self.guess_model_type(response)
>       response = Nokogiri::XML(response.to_s)
>       return :action if ((response/'//actions').length == 1) and ((response/'//state').length == 0)
>-      return :state if ((response/'//actions').length == 1) and ((response/'//state').length == 1)
>+      return :stateful if ((response/'//actions').length == 1) and ((response/'//state').length == 1)
>       return :base
>     end
>
>+    class API
>+      class Action; end
>+      class Base; end
>+      class Stateful; end
>+    end
> end
>--
>1.7.3.2
>

-- 
--------------------------------------------------------
Michal Fojtik, mfojtik@redhat.com
Deltacloud API: http://deltacloud.org
--------------------------------------------------------