hardaur-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Oct-12 18:29 UTC
code layout question
I''m trying to figure out where to put objects that aren''t strictly "model" objects (ie, not database/business objects, these are actually objects holding information regarding the ui). I''ve seen a couple answers which is to create the classes in a module file in /lib, or to create the objects as non-ActiveRecord class files. Both of these solutions work, of course. However, when working in the rails development environment they aren''t reloaded on the fly in webrick/mongrel when they''re modified. Restarting the server everytime I modify one is a bit of a pain. The one fix I have found is to just throw the class definitions in one of the Helper files and instantiate it with the full path, with an eye to moving it to lib when finished. It works, but it''s really nasty and I would prefer to come up with a "right" solution. The concept would be, for instance, that I am creating a web application that has multiple windows. Each window is an instantiation of a Frame object, it''s the Frame objects I need to know where to put. Thanks for any help! H --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Put them in /lib. To make the classes reload automatically: class YourLibClass include Reloadable #pretty sure that''s the name ... end Jason On 10/12/06, hardaur-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <hardaur-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > I''m trying to figure out where to put objects that aren''t strictly > "model" objects (ie, not database/business objects, these are actually > objects holding information regarding the ui). I''ve seen a couple > answers which is to create the classes in a module file in /lib, or to > create the objects as non-ActiveRecord class files. > > Both of these solutions work, of course. However, when working in the > rails development environment they aren''t reloaded on the fly in > webrick/mongrel when they''re modified. Restarting the server everytime > I modify one is a bit of a pain. > > The one fix I have found is to just throw the class definitions in one > of the Helper files and instantiate it with the full path, with an eye > to moving it to lib when finished. It works, but it''s really nasty and > I would prefer to come up with a "right" solution. > > The concept would be, for instance, that I am creating a web > application that has multiple windows. Each window is an instantiation > of a Frame object, it''s the Frame objects I need to know where to put. > > Thanks for any help! > > H > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Leave them in /lib and put this at the top of your class... class MyClass include Reloadable That should solve the reloading issue in development. hardaur-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> I''m trying to figure out where to put objects that aren''t strictly > "model" objects (ie, not database/business objects, these are actually > objects holding information regarding the ui). I''ve seen a couple > answers which is to create the classes in a module file in /lib, or to > create the objects as non-ActiveRecord class files. > > Both of these solutions work, of course. However, when working in the > rails development environment they aren''t reloaded on the fly in > webrick/mongrel when they''re modified. Restarting the server everytime > I modify one is a bit of a pain. > > The one fix I have found is to just throw the class definitions in one > of the Helper files and instantiate it with the full path, with an eye > to moving it to lib when finished. It works, but it''s really nasty and > I would prefer to come up with a "right" solution. > > The concept would be, for instance, that I am creating a web > application that has multiple windows. Each window is an instantiation > of a Frame object, it''s the Frame objects I need to know where to put. > > Thanks for any help! > > H > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> include Reloadable #pretty sure that''s the nameThat''s it, however sometimes Rails is weird about when it''ll reload and when it won''t. I still have to restart webrick/mongrel in a few cases (which I can''t remember at the moment). Anyone care to enumerate them here? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hrm, I try that, and restart the server. First request is fine, hit reload (change or no change to class) returns: NameError in TestController#index uninitialized constant MyClass Here''s the module file currently in /lib module MyLib class MyClass include Reloadable def doit puts "This work?" end end end --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
probably seeing what eden li was talking about. I forgot to include the code in the controller that calls it. . . x = MyLib::MyClass.new x.doit Hardy Hardy wrote:> Hrm, I try that, and restart the server. First request is fine, hit > reload (change or no change to class) returns: > > NameError in TestController#index > > uninitialized constant MyClass > > Here''s the module file currently in /lib > > module MyLib > class MyClass > include Reloadable > > def doit > puts "This work?" > end > end > end--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Can you post the code to TestController#index? Jason On 10/12/06, Hardy <hardaur-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > Hrm, I try that, and restart the server. First request is fine, hit > reload (change or no change to class) returns: > > NameError in TestController#index > > uninitialized constant MyClass > > Here''s the module file currently in /lib > > module MyLib > class MyClass > include Reloadable > > def doit > puts "This work?" > end > end > end > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
class TestController < ApplicationController def index puts "in index" x = MyLib::Duh.new x.doit end end See anything? *hopes* Jason Roelofs wrote:> Can you post the code to TestController#index? > > Jason > > On 10/12/06, Hardy <hardaur-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > Hrm, I try that, and restart the server. First request is fine, hit > > reload (change or no change to class) returns: > > > > NameError in TestController#index > > > > uninitialized constant MyClass > > > > Here''s the module file currently in /lib > > > > module MyLib > > class MyClass > > include Reloadable > > > > def doit > > puts "This work?" > > end > > end > > end > > > > > > > > > > > ------=_Part_20121_4316017.1160679281408 > Content-Type: text/html; charset=ISO-8859-1 > > Can you post the code to TestController#index?<br><br>Jason<br><br><div><span class="gmail_quote">On 10/12/06, <b class="gmail_sendername">Hardy</b> <<a href="mailto:hardaur-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org">hardaur-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org</a>> wrote:</span> > <blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br>Hrm, I try that, and restart the server. First request is fine, hit<br>reload (change or no change to class) returns: > <br><br> NameError in TestController#index<br><br>uninitialized constant MyClass<br><br>Here''s the module file currently in /lib<br><br>module MyLib<br> class MyClass<br> include Reloadable<br><br> def doit<br> puts "This work?" > <br> end<br> end<br>end<br><br><br></div><br> > > ------=_Part_20121_4316017.1160679281408----~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Umm, please change "Duh" to "MyClass" ; ) Cleaning up obnoxious code for public presentation. Hardy wrote:> class TestController < ApplicationController > def index > puts "in index" > x = MyLib::Duh.new > x.doit > end > end > > See anything? *hopes* > > Jason Roelofs wrote: > > Can you post the code to TestController#index? > > > > Jason > > > > On 10/12/06, Hardy <hardaur-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > > > Hrm, I try that, and restart the server. First request is fine, hit > > > reload (change or no change to class) returns: > > > > > > NameError in TestController#index > > > > > > uninitialized constant MyClass > > > > > > Here''s the module file currently in /lib > > > > > > module MyLib > > > class MyClass > > > include Reloadable > > > > > > def doit > > > puts "This work?" > > > end > > > end > > > end > > > > > > > > > > > > > > > > > ------=_Part_20121_4316017.1160679281408 > > Content-Type: text/html; charset=ISO-8859-1 > > > > Can you post the code to TestController#index?<br><br>Jason<br><br><div><span class="gmail_quote">On 10/12/06, <b class="gmail_sendername">Hardy</b> <<a href="mailto:hardaur-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org">hardaur-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org</a>> wrote:</span> > > <blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br>Hrm, I try that, and restart the server. First request is fine, hit<br>reload (change or no change to class) returns: > > <br><br> NameError in TestController#index<br><br>uninitialized constant MyClass<br><br>Here''s the module file currently in /lib<br><br>module MyLib<br> class MyClass<br> include Reloadable<br><br> def doit<br> puts "This work?" > > <br> end<br> end<br>end<br><br><br></div><br> > > > > ------=_Part_20121_4316017.1160679281408----~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I think I see what''s going on here. The lib/ system is very dependent on the filename <-> classname relationship. Thus, you have lib/my_lib.rb, Rails is going to look for class MyLib. Otherwise, you have to manually require the file when you want to use it. So in your case, try moving MyClass out of the module, and make sure that the file is named lib/my_class.rb. That should take care of this problem. jason On 10/12/06, Hardy <hardaur-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > probably seeing what eden li was talking about. I forgot to include > the code in the controller that calls it. . . > > x = MyLib::MyClass.new > x.doit > > Hardy > > > > Hardy wrote: > > Hrm, I try that, and restart the server. First request is fine, hit > > reload (change or no change to class) returns: > > > > NameError in TestController#index > > > > uninitialized constant MyClass > > > > Here''s the module file currently in /lib > > > > module MyLib > > class MyClass > > include Reloadable > > > > def doit > > puts "This work?" > > end > > end > > end > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
That did it!! Kind of strange it didn''t like it from the module, but it''s not a problem for the application to just define a class file. Thank you all very much!! H --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> The lib/ system is very dependent on the filename <-> classname relationship.Aha! That solves my problem too. Is this documented somewhere? Things outside of app/, config/ (except routes.rb) and public/ all seem magical to me. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---