Hello, I''m overriding the initialize method of a base class and I''m trying to set a Base class variable in the overridden method and have it register in the Base class (hope that makes sense). Here''s my code: [code] in Rails::Generator::Base: class Base include Options class_inheritable_accessor :spec def initialize(runtime_args, runtime_options = {}) @args = runtime_args parse!(@args, runtime_options) # Derive source and destination paths. @source_root = options[:source] || File.join(spec.path, ''templates'') end end class MediaModuleGenerator < Rails::Generator::Base def initialize(runtime_args, runtime_options = {}) self.spec.path = File.dirname(__FILE__) + ''/generators/media_module'' super end end [/code] I should mention that spec is a module w/in the Rails framework IE: Rails::Generator::Spec When I run my generator, I get this error: You have a nil object when you didn''t expect it! (NoMethodError) The error occurred while evaluating nil.path So when I set the self.spec.path var to File.dirname(__FILE__) + ''/generators/media_module'', it''s not registering in the base class. Any ideas? -- Posted via http://www.ruby-forum.com/.
Conrad Taylor
2009-Jun-04 02:58 UTC
Re: Can you set Base class attributes from a subclass?
On Wed, Jun 3, 2009 at 7:39 PM, Clem Rock <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>wrote:> > Hello, > I''m overriding the initialize method of a base class and I''m trying > to set a Base class variable in the overridden method and have it > register in the Base class (hope that makes sense). > > Here''s my code: > > [code] > > in Rails::Generator::Base: > > > class Base > include Options > class_inheritable_accessor :spec > > def initialize(runtime_args, runtime_options = {}) > @args = runtime_args > parse!(@args, runtime_options) > > # Derive source and destination paths. > @source_root = options[:source] || File.join(spec.path, ''templates'') > end > end > > class MediaModuleGenerator < Rails::Generator::Base > def initialize(runtime_args, runtime_options = {}) > self.spec.path = File.dirname(__FILE__) + > ''/generators/media_module'' > super > end > end >If you''re overriding the initialize method, then you should be calling super before any other expression. This isn''t necessary in regards to subclasses who parent is Object. -Conrad> > [/code] > > I should mention that spec is a module w/in the Rails framework IE: > Rails::Generator::Spec > > When I run my generator, I get this error: > > You have a nil object when you didn''t expect it! (NoMethodError) The > error occurred while evaluating nil.path > > So when I set the self.spec.path var to File.dirname(__FILE__) + > ''/generators/media_module'', it''s not registering in the base class. > > Any ideas? > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Conrad Taylor
2009-Jun-04 03:03 UTC
Re: Can you set Base class attributes from a subclass?
On Wed, Jun 3, 2009 at 7:58 PM, Conrad Taylor <conradwt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Wed, Jun 3, 2009 at 7:39 PM, Clem Rock < > rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > >> >> Hello, >> I''m overriding the initialize method of a base class and I''m trying >> to set a Base class variable in the overridden method and have it >> register in the Base class (hope that makes sense). >> >> Here''s my code: >> >> [code] >> >> in Rails::Generator::Base: >> >> >> class Base >> include Options >> class_inheritable_accessor :spec >> >> def initialize(runtime_args, runtime_options = {}) >> @args = runtime_args >> parse!(@args, runtime_options) >> >> # Derive source and destination paths. >> @source_root = options[:source] || File.join(spec.path, ''templates'') >> end >> end >> >> class MediaModuleGenerator < Rails::Generator::Base >> def initialize(runtime_args, runtime_options = {}) >> self.spec.path = File.dirname(__FILE__) + >> ''/generators/media_module'' >> super >> end >> end >> > > If you''re overriding the initialize method, then you should be > calling super before any other expression. This isn''t necessary > in regards to subclasses who parent is Object. >You should have an expression like this in your subclass: super( runtime_args, runtime_options ) If you need further details, please consult the PixAxe book. -Conrad> > -Conrad > > >> >> [/code] >> >> I should mention that spec is a module w/in the Rails framework IE: >> Rails::Generator::Spec >> >> When I run my generator, I get this error: >> >> You have a nil object when you didn''t expect it! (NoMethodError) The >> error occurred while evaluating nil.path >> >> So when I set the self.spec.path var to File.dirname(__FILE__) + >> ''/generators/media_module'', it''s not registering in the base class. >> >> Any ideas? >> -- >> Posted via http://www.ruby-forum.com/. >> >> >> >> >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I tried adding super( runtime_args, runtime_options ) and still got this error: rails_generator/base.rb:107:in `initialize'': You have a nil object when you didn''t expect it! my overidden initialize method now looks like this: def initialize(runtime_args, runtime_options = {}) super( runtime_args, runtime_options ) end Conrad Taylor wrote:> On Wed, Jun 3, 2009 at 7:58 PM, Conrad Taylor <conradwt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > >>> >>> @args = runtime_args >>> ''/generators/media_module'' >>> super >>> end >>> end >>> >> >> If you''re overriding the initialize method, then you should be >> calling super before any other expression. This isn''t necessary >> in regards to subclasses who parent is Object. >> > > You should have an expression like this in your subclass: > > super( runtime_args, runtime_options ) > > If you need further details, please consult the PixAxe book. > > -Conrad-- Posted via http://www.ruby-forum.com/.
Conrad Taylor
2009-Jun-04 04:44 UTC
Re: Can you set Base class attributes from a subclass?
On Wed, Jun 3, 2009 at 9:08 PM, Clem Rock <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>wrote:> > I tried adding > > super( runtime_args, runtime_options ) > > and still got this error: > > rails_generator/base.rb:107:in `initialize'': You have a nil object when > you didn''t expect it! > > my overidden initialize method now looks like this: > > > def initialize(runtime_args, runtime_options = {}) > super( runtime_args, runtime_options ) > end > > > > Conrad Taylor wrote: > > On Wed, Jun 3, 2009 at 7:58 PM, Conrad Taylor <conradwt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > wrote: > > > >>> > >>> @args = runtime_args > >>> ''/generators/media_module'' > >>> super > >>> end > >>> end > >>> > >> > >> If you''re overriding the initialize method, then you should be > >> calling super before any other expression. This isn''t necessary > >> in regards to subclasses who parent is Object. > >> > > > > You should have an expression like this in your subclass: > > > > super( runtime_args, runtime_options ) > > > > If you need further details, please consult the PixAxe book. > > > > -Conrad >What''s on line 107 that''s generating the error message? -Conrad --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
this is line 107: @source_root = options[:source] || File.join(spec.path, ''templates'') Conrad Taylor wrote:> On Wed, Jun 3, 2009 at 9:08 PM, Clem Rock > <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>wrote: > >> my overidden initialize method now looks like this: >> > wrote: >> >> calling super before any other expression. This isn''t necessary >> > What''s on line 107 that''s generating the error message? > > -Conrad-- Posted via http://www.ruby-forum.com/.
Conrad Taylor
2009-Jun-04 06:23 UTC
Re: Can you set Base class attributes from a subclass?
What''s the value of spec.path? On Wed, Jun 3, 2009 at 10:38 PM, Clem Rock <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > this is line 107: > > @source_root = options[:source] || File.join(spec.path, ''templates'') > > > > > > Conrad Taylor wrote: > > On Wed, Jun 3, 2009 at 9:08 PM, Clem Rock > > <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>wrote: > > > >> my overidden initialize method now looks like this: > >> > wrote: > >> >> calling super before any other expression. This isn''t necessary > >> > > What''s on line 107 that''s generating the error message? > > > > -Conrad > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
spec.path is nil.path - that''s the whole problem - I''m trying to set the spec.path = File.dirname(__FILE__) + ''/generators/media_module'' in my overidden initialize method and I can''t seem to send that value to the Base class. Conrad Taylor wrote:> What''s the value of spec.path? > > On Wed, Jun 3, 2009 at 10:38 PM, Clem Rock > <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org-- Posted via http://www.ruby-forum.com/.