Plug-in support for Camping Apps

ronald.evangelista ironald at gmail.com
Wed May 28 12:18:19 EDT 2008


# camping_plugin.rb
# plug-in support for Camping Apps
# require 'lib/camping_plugin'
# override R helper method to your liking :-)

module Camping
   module PluginHelpers
      def R_with_module(c,*g)
         p,h=/\(.+?\)/,g.grep(Hash)
         g-=h
         raise "bad route" unless u = c.urls.find{|x|
            break x if x.scan(p).size == g.size &&
            /^#{x}\/?$/ =~ (x=g.inject(x){|x,a|
            x.sub p,C.escape((a[a.class.primary_key]rescue a))})
         }
         if h.any?
            l = proc{|_,o,n|o.u(n,&m)rescue([*o]<<n)}
            h[0].values.grep(Hash).each{ |a|
               k, v = h[0].index(a), a
               h[0].u( h[0].delete(k).inject(H[]){|m, a|
               m.u( H[[k,a[0]].join("[")<<"]", a[1]], &l)}, &l)
            }
            h[0].rehash

            u+"?"+h[0].map{|x|
               k, v = x
               Array===v ? v.map{|v2| [k, v2].map{|z| C.escape z}*"="} * 
"&" : x.map{|z| C.escape z}*"="
            }*"&"
         else
            u
         end
      end
   end
end
#init plug-in
Camping::Helpers.send(:include, Camping::PluginHelpers)
Camping::Helpers.class_eval { alias_method_chain :R, :module }



More information about the Camping-list mailing list