Module Madness

John Sheets fisheye at metacasa.net
Tue May 8 13:50:15 EDT 2007


On May 7, 2007, at 1:31 PM, Joshua Schairbaum wrote:

> I'm wanted to make a "global" module that I could include in my
> application for some shared code between them.  I've been making
> several camping apps and wanted them all to share a helper/partial
> that contained the navigation.

I think the root of the "problem" is the way the app modules are  
loaded into the Markaby rendering context:

   module Views; include Controllers, Helpers end
   class Mab < Markaby::Builder
       include Views
       def tag!(*g,&b)
           h=g[-1]
           [:href,:action,:src].each{|a|(h[a]=self/h[a])rescue 0}
           super
       end
   end

Here's a slightly unnerving hack which seems to solve the problem.   
You pass it the base module (or symbol of) your Camping app and one  
or more shared helper modules, and it'll inject them into your  
Camping app.  (Obviously the symbol parameters won't work if you have  
nested modules...you'll have to pass in the module constant  
instead...the symbols are just syntactic sugar to echo Camping.goes().)

class TentSteak
   def self.bootstrap(mod_or_sym, *appmods)
     @@rootmod = mod_or_sym.instance_of?(Module) ? mod_or_sym :  
const_get(mod_or_sym.to_s)
     inject appmods
   end
   def self.inject(*mod_or_syms)
     @@rootmod.module_eval "class Mab < Markaby::Builder; include # 
{mod_or_syms.join(', ')}; end"
   end
   def self.view_method_defined?(meth)
     @@rootmod::Mab.method_defined?(meth)
   end
end

Usage is like this:

   Camping.goes :MyApp
   TentSteak.bootstrap :MyApp, :MyHelperModule, :MyOtherHelpers

I'd love to hear if there's a better way to do this injection.  Seems  
pretty hacky.  Incidentally, TentSteak is a Camping helper library  
I've been working on recently.  I'll upload it to rubyforge as soon  
as I get the ugly corners chiseled off and properly tested/ 
documented....

John

-- 
"The Internet is not something you just dump something
on.  It's not a big truck.  It's a series of tubes."
  --Senator Ted Stevens, R-AK

John R. Sheets
http://bark.metacasa.net



-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/camping-list/attachments/20070508/3067553e/attachment.html 


More information about the Camping-list mailing list