Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
# Demonstrating the use of const_get() and caller() to build a smarter delegation pattern. # Let's pretend this is a lot more complicated and in some other file. class ImapProfile def self.config {:username => "todb", :password => "Shadowfax" # Unguessable! } end end # This too. class ImapSendUserNameCmd def self.data username = ImapProfile.config[:username] [username].pack("m*") end end module ActionHandler def my_protocol self.class.name.to_s.split("::").last end def caller_action_to_constant caller[1] =~ /`([^']+?)'/ $1 =~ /^do_(client|server)_(.*)/ $2.split("_").map {|s| s.capitalize}.join end def action_executor(args={}) Kernel.const_get(my_protocol + caller_action_to_constant + "Cmd").send :data end end module AppManager class Imap include ActionHandler def get_profile_params Kernel.const_get(my_protocol + "Profile").send :config end def do_client_send_user_name(args={}) action_executor(args) end end end @app = AppManager::Imap.new
This paste will be private.
From the Design Piracy series on my blog: