Joshua Muheim
2007-Nov-20 17:59 UTC
Plugin: create instance methods dynamically in ClassMethods?
Hi all I have the following plugin code: module IncenseCrud def self.included(base_class) base_class.extend(ClassMethods) end module ClassMethods def performs_incense_crud def index list return render(:action => ''list'') end end end end In init.rb I have the following: require File.dirname(__FILE__) + ''/lib/incense_crud'' ActionController::Base.send(:include, IncenseCrud) So far so good. In any controller I can put now class AnyController < ApplicationController performs_incense_crud end and the performs_incense_crud method is invoked. Cool - the class method has been added to the controller through the plugin. But strangely the instance method index does not seem to have been added! It''s just disappeared when trying to reach www.xxx.yyy/any_controller/index What''s wrong here? Can''t I add these methods dynamically this way? Thanks Josh -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2007-Nov-20 19:14 UTC
Re: Plugin: create instance methods dynamically in ClassMethods?
define_method usually does the trick when trying to define methods dynamically :-) Fred --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Joshua Muheim
2007-Nov-21 13:04 UTC
Re: Plugin: create instance methods dynamically in ClassMeth
Frederick Cheung wrote:> define_method usually does the trick when trying to define methods > dynamically :-) > > FredThank you, this works well. Now I''ve got another question... In fact, my method performs_incense_crud accepts an option parameter: def performs_incense_crud(options = {}) ... end Now I''d like to use this options parameter in another method called list: module IncenseCrud def self.included(base_class) base_class.extend(ClassMethods) end module ClassMethods def performs_incense_crud(options = {}) define_method(:index) do list return render(:action => ''list'') end define_method(:list) do @pages, @objects = paginate :sessions, :per_page => (options[:items_per_page] || 10) end end end end When using... performs_incense_crud :items_per_page => 5 ...it works great. But when omitting this option... performs_incense_crud ...it returns the following error: NameError in XxxController#index undefined local variable or method ` 10'' for #<LinksController:0x32a896c> The same happens when using ? : ... :per_page => (options[:items_per_page] ? options[:items_per_page] : 10) instead of ||. Can anybody tell why this doesn''t work here? Thanks a lot, Josh -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---