deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michal Fojtik <mfoj...@redhat.com>
Subject Re: [PATCH core 1/2] CIMI: Prevent recycling old Structs
Date Thu, 06 Sep 2012 12:36:21 GMT

On Sep 6, 2012, at 1:30 AM, David Lutterkort <lutter@redhat.com> wrote:

> On Wed, 2012-09-05 at 15:15 +0200, mfojtik@redhat.com wrote:
>> From: Michal Fojtik <mfojtik@redhat.com>
>> 
>> * This should fix DTACLOUD-311
>> 
>> Signed-off-by: Michal fojtik <mfojtik@redhat.com>
>> ---
>> server/lib/cimi/models/schema.rb |    6 ++----
>> 1 file changed, 2 insertions(+), 4 deletions(-)
>> 
>> diff --git a/server/lib/cimi/models/schema.rb b/server/lib/cimi/models/schema.rb
>> index 088e111..a7dc0f2 100644
>> --- a/server/lib/cimi/models/schema.rb
>> +++ b/server/lib/cimi/models/schema.rb
>> @@ -140,11 +140,9 @@ class CIMI::Model::Schema
>>     def struct
>>       cname = "CIMI_#{json_name.upcase_first}"
>>       if ::Struct.const_defined?(cname)
>> -        ::Struct.const_get(cname)
>> -      else
>> -        ::Struct.new("CIMI_#{json_name.upcase_first}",
>> -                     *@schema.attribute_names)
>> +        ::Struct.send(:remove_const, "CIMI_#{json_name.upcase_first}")
>>       end
>> +      ::Struct.new("CIMI_#{json_name.upcase_first}", *@schema.attribute_names)
> 
> Nice catch - instead of defining a constant Struct::Foo and then
> undefining it, and continually defining new classes, we should just save
> it in an instance variable, i.e. do the following
> 
>        def struct
>          @struct_class ||= Struct.new(nil, *@schema.attribute_names)
>        end
> 

I tried this but got a lot of:

................F/Users/mfojtik/code/core/server/lib/cimi/models/schema.rb:146: warning: redefining
constant Struct::CIMI_Operations
./Users/mfojtik/code/core/server/lib/cimi/models/schema.rb:146: warning: redefining constant
Struct::CIMI_Disks
/Users/mfojtik/code/core/server/lib/cimi/models/schema.rb:146: warning: redefining constant
Struct::CIMI_Operations
F/Users/mfojtik/code/core/server/lib/cimi/models/schema.rb:146: warning: redefining constant
Struct::CIMI_Operations
./Users/mfojtik/code/core/server/lib/cimi/models/schema.rb:146: warning: redefining constant
Struct::CIMI_Volumes
/Users/mfojtik/code/core/server/lib/cimi/models/schema.rb:146: warning: redefining constant
Struct::CIMI_NetworkInterfaces
/Users/mfojtik/code/core/server/lib/cimi/models/schema.rb:146: warning: redefining constant
Struct::CIMI_Operations
...../Users/mfojtik/code/core/server/lib/cimi/models/schema.rb:146: warning: redefining constant
Struct::CIMI_Structs
../Users/mfojtik/code/core/server/lib/cimi/models/schema.rb:146: warning: redefining constant
Struct::CIMI_Meter
...../Users/mfojtik/code/core/server/lib/cimi/models/schema.rb:146: warning: redefining constant
Struct::CIMI_Struct


But no errors. I think we can combine both to:

    def struct
      cname = "CIMI_#{json_name.upcase_first}"
      ::Struct.send(:remove_const, cname) if ::Struct.const_defined?(cname)
      @struct_class ||= ::Struct.new(cname, *@schema.attribute_names)
    end

This will remove the warnings and does not define new class every time :)

  -- Michal


Michal Fojtik
http://deltacloud.org
mfojtik@redhat.com




Mime
View raw message