# 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 }
What does this do? Can you give an example of how someone might use this? -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/camping-list/attachments/20080528/4c184cd4/attachment.html>
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
Why not just do something like this?
  require ''camping''
  require ''awesome_plugin
  Camping.goes :Example
  Example.needs AwesomePlugin
  # needs is defined as:
  module Camping
    def self.needs(m)
      include(m)
    end
  end
  # The plugin:
  module AwesomePlugin
    def service(*a)
      # Do some stuff
      super(*a)
    end
  end
On Thu, May 29, 2008 at 5:48 PM, ronald.evangelista <ironald at gmail.com>
wrote:> 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
> Camping-list at rubyforge.org
> http://rubyforge.org/mailman/listinfo/camping-list
>
-- 
Magnus Holm
Maybe Matching Threads
- multiplying values in data frame by corresponding value in the first column
- Passing extra parameters to functions called from Observers
- accessing extra request parameters
- XML parameters to Column Headers for importing into a dataset
- link_to and GET parameters