[opensuse-ruby] HelpNeeded: ActiveResource and transitive inheritance
Hi, I create own wrapper over ActiveResource and found quite interesting problem which blocking my work. If I create ancestor of ActiveResource::Base then everything works, but when I create ancestor of ancestor then it stop working (exactly it return nil on method which should not return nil, because it is accessor with default and I assign value so it should return assigned value or default but not nil). I attach example code which demonstrate this problem. thanks for help Josef -- Josef Reidinger YaST team maintainer of perl-Bootloader, YaST2-Repair, webyast (language,time,basesystem,ntp)
On 2009-12-21 08:40:58 +0100, Josef Reidinger wrote:
Hi, I create own wrapper over ActiveResource and found quite interesting problem which blocking my work. If I create ancestor of ActiveResource::Base then everything works, but when I create ancestor of ancestor then it stop working (exactly it return nil on method which should not return nil, because it is accessor with default and I assign value so it should return assigned value or default but not nil). I attach example code which demonstrate this problem.
your example and the active_resource code are not the same [[[ require 'rubygems' require 'active_resource' class A class << self attr_accessor_with_default(:a) { ActiveSupport::Inflector.pluralize(element_name) } end end class B < A end class C < B end A.a = 'test' puts A.a puts B.a puts C.a ]]] from my understanding attr_accessor_with_default sets a instance variable on the class object. those are not inherited. darix -- openSUSE - SUSE Linux is my linux openSUSE is good for you www.opensuse.org -- To unsubscribe, e-mail: opensuse-ruby+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-ruby+help@opensuse.org
Marcus Rueckert write:
On 2009-12-21 08:40:58 +0100, Josef Reidinger wrote:
Hi, I create own wrapper over ActiveResource and found quite interesting problem which blocking my work. If I create ancestor of ActiveResource::Base then everything works, but when I create ancestor of ancestor then it stop working (exactly it return nil on method which should not return nil, because it is accessor with default and I assign value so it should return assigned value or default but not nil). I attach example code which demonstrate this problem.
your example and the active_resource code are not the same
[[[ require 'rubygems' require 'active_resource'
class A class << self attr_accessor_with_default(:a) { ActiveSupport::Inflector.pluralize(element_name) } end end
class B < A end class C < B end
A.a = 'test'
puts A.a puts B.a puts C.a ]]]
from my understanding attr_accessor_with_default sets a instance variable on the class object. those are not inherited.
darix
Thanks, so it sets only to instance. I found that I cannot move all models which is based on out rest-service to ancestor, but use module, which sets it to class instance. Thanks for explanation. I add it to documentation why module instead of ancestor must be used. Josef -- Josef Reidinger YaST team maintainer of perl-Bootloader, YaST2-Repair, webyast (language,time,basesystem,ntp) -- To unsubscribe, e-mail: opensuse-ruby+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-ruby+help@opensuse.org
On Monday 21 December 2009 15:01:09 Josef Reidinger wrote:
from my understanding attr_accessor_with_default sets a instance variable on the class object. those are not inherited.
class TestA class << self def test_a "test_a" end end end class TestB < TestA end class TestC class << self attr_accessor_with_default(:test_c) { "test_c" } end end class TestD < TestC end class TestF < TestD end puts TestA.test_a puts TestB.test_a puts TestC.test_c puts TestD.test_c puts TestF.test_c output: test_a test_a test_c test_c test_c -- Duncan Mac-Vicar P. - Engineering Manager, YaST SUSE LINUX Products GmbH, GF: Markus Rex, HRB 16746 (AG Nuernberg) -- To unsubscribe, e-mail: opensuse-ruby+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-ruby+help@opensuse.org
On 2009-12-21 16:33:43 +0100, Duncan Mac-Vicar Prett wrote:
Date: Mon, 21 Dec 2009 16:33:43 +0100 From: Duncan Mac-Vicar Prett
Subject: Re: [opensuse-ruby] HelpNeeded: ActiveResource and transitive inheritance To: opensuse-ruby@opensuse.org Cc: Josef Reidinger On Monday 21 December 2009 15:01:09 Josef Reidinger wrote:
from my understanding attr_accessor_with_default sets a instance variable on the class object. those are not inherited.
class TestA class << self def test_a "test_a" end end end
class TestB < TestA end
class TestC class << self attr_accessor_with_default(:test_c) { "test_c" } end end
class TestD < TestC end
class TestF < TestD end
puts TestA.test_a puts TestB.test_a puts TestC.test_c puts TestD.test_c puts TestF.test_c
output: test_a test_a test_c test_c test_c
well i believe you that printing out the default value works, as it should. but setting the value for test_c saves the value in an *instance* variable of the Class object. :) darix -- openSUSE - SUSE Linux is my linux openSUSE is good for you www.opensuse.org -- To unsubscribe, e-mail: opensuse-ruby+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-ruby+help@opensuse.org
On Monday 21 December 2009 16:48:02 Marcus Rueckert wrote:
well i believe you that printing out the default value works, as it should. but setting the value for test_c saves the value in an *instance* variable of the Class object. :)
darix
Right, actually: TestD.test_c = 2 puts TestD.test_c puts TestF.test_c output: 2 nil same effect. I warned you time ago! http://twitter.com/dmacvicar/status/6443921461 :-P -- Duncan Mac-Vicar P. - Engineering Manager, YaST SUSE LINUX Products GmbH, GF: Markus Rex, HRB 16746 (AG Nuernberg) -- To unsubscribe, e-mail: opensuse-ruby+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-ruby+help@opensuse.org
On 2009-12-21 17:49:46 +0100, Duncan Mac-Vicar Prett wrote:
same effect. I warned you time ago! http://twitter.com/dmacvicar/status/6443921461 :-P
well... attr_accessor behaves exactly the same and it matches the documentation imho. -- openSUSE - SUSE Linux is my linux openSUSE is good for you www.opensuse.org -- To unsubscribe, e-mail: opensuse-ruby+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-ruby+help@opensuse.org
participants (3)
-
Duncan Mac-Vicar Prett
-
Josef Reidinger
-
Marcus Rueckert