Gmane
Favicon Gravatar
From: Jamis Buck <jamis@...>
Subject: Re: Plugin conventions
Newsgroups: gmane.comp.lang.ruby.rails
Date: 2005-10-26 17:06:46 GMT (3 years, 35 weeks, 6 days, 6 hours and 43 minutes ago)
On Oct 26, 2005, at 10:46 AM, Trevor Squires wrote:

> Hi Jamis,
>
> Well, this is what I get for posting to my blog before opening my  
> inbox...
>
> http://tinyurl.com/8rant
>
> (and I love that I have a tinyurl with 'rant' in it)
>
> Anyhow, the only thing that I can add is that using your namespace  
> in your /lib directory to avoid 'require' conflicts seems 'right'  
> to me.

Well said, Trevor (nice article). I completely agree.

Note, however, that in order for Rails to be able to easily find your  
namespaced classes and modules, you'll need to add some intermediate  
files at each level of your namespace hierarchy, like so:

   lib/
   lib/jamis.rb
   lib/jamis/
   lib/jamis/buck.rb
   lib/jamis/buck/
   lib/jamis/buck/my_module.rb

jamis.rb looks like this:

   require 'jamis/buck'

buck.rb looks like this:

   require 'jamis/buck/my_module'

and my_module looks like this:

   module Jamis
   module Buck
     module MyModule
       ...
     end
   end
   end

Then, you can simply do Jamis::Buck::MyModule in your code and not  
have to do any explicit requires anywhere. (Without those  
intermediate files, you'd have to do 'require "jamis/buck/my_module"'  
somewhere in your code before you could access the module.)

- Jamis

>
> Regards,
> Trevor
>
>
> On 26-Oct-05, at 7:38 AM, Jamis Buck wrote:
>
>
>>
>> This reduces the risk of namespace conflicts between two or more  
>> plugins, if two people implement the same functionality  
>> independently.
>>
>>
> --
> Trevor Squires
> http://somethinglearned.com
>
> _______________________________________________
> Rails mailing list
> Rails@...
> http://lists.rubyonrails.org/mailman/listinfo/rails
>