in theory it should be as easy as that, ...  ala-rails.
would be nice to have genuine support for plug-ins .
will peruse zimbatm's 'equipment' to get more inspiration
or maybe we can set a standard on how to add Camping plug-ins.

anyway, this is how i've used it:   'quick n dirty '

# helpers_plugin.rb
# it would be nice to have a genuine support for plugins
# plug-in support for Camping Apps to override original Camping::Helper methods # otherwise you can only override methods if you code directly into your App::Helpers
# like so:
#
# module App::Helpers
#  def R(c, *g)
#     ":-)"
#  end
# end
#
# why? check out http://rubyforge.org/pipermail/camping-list/2007-November/000545.html by zimbatm
#
#  pseudo-flattens a hash
# {"nested_params"=>{"a"=>"1", "b"=>["1", "2", "3"]}, "param1"=>"10", "param2"=>"1..10"}.flatten # >> {"nested_params[b]"=>[1, 2, 3], "nested_params[a]"=>1, "param1"=>10, "param2"=>1..10}
class Hash
  def flatten
     m = proc{|_,o,n|o.u(n,&m)rescue([*o]+[n].flatten)}
     values.grep(Hash).each do |a|
        k, v = index(a), a
        u(delete(k).inject(Hash[]){|h, a|
        h.u(Hash[[k,a[0]].join("[")<<"]", a[1]], &m)},
        &m)
     end
     rehash
  end
  alias_method :u, :merge!
end
# required/loaded after the camping library to work
module Camping
  module PluginHelpers
     # override R helper method to your liking :-)
     def R_with_plugin(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))})
        }
        h.any? ? u+"?"+h[0].flatten.map{|x|
              k, v = x
Array===v ? v.map{|v2| [k, v2].map{|z| C.escape z}*"="} * "&" : x.map{|z| C.escape z}*"=" }*"&" : u
     end
  end
end

# in your app:
# %w[camping camping/session lib/plugins/helpers_plugin pp].each{|l| require l}
#  use Camping.makes method instead of goes
#
# Camping.makes :MyApp, :plugins=>[:plugin_helpers]
#
#    R in your views
#    you can now use nested hash params
# a 'view', :href=>R(ViewPost, post, 'param1'=>10, 'param2'=>1..10, 'nested_params'=>{'a'=>1, 'b'=>[1, 2, 3]})
#    ...
#
#    check values of @input
#
#    class ViewPost < R '/post/(\d+)', '/post/(\d+)/(\w+)'
#      include PostHelper
#      def get(id, action=nil)
#         @post = model_class.find(id)
# pp input # >> {"nested_params"=>{"a"=>"1", "b"=>["1", "2", "3"]}, "param1"=>"10", "param2"=>"1..10"} # # original R implementation returns {"nested_params"=>"a1b123", "param1"=>"10", "param2"=>"1..10"}
#                  #
# @params = @input # then you can pass em around like form data perhaps changing values, ie 'page'=>999
#         case action
#         when 'edit'
#            render :update_post
#         else
#            render :view_post
#         end
#      end
#   end

module Camping
  def self.makes(*a)
     h = a.grep(Hash)
     a-=h
     app = a.shift
     Camping.goes app
     @App=const_get( app.to_s)
     h.pop[:plugins].each{|s| self.load_plugin s} if h.any?
  end
  def self.load_plugin(symbol)
     plugin_module = "Camping::#{symbol.to_s.camelize}".constantize
     @App::Helpers.send(:include, plugin_module)
     @App::Helpers.module_eval {
        plugin_module.instance_methods.map{|m|
           target_method = m.gsub(/_with_plugin/,'')
alias_method_chain(target_method, :plugin) if instance_methods.include?(target_method)
        }
     }
  end
end

_______________________________________________
Camping-list mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/camping-list

Reply via email to