From: Josh Cheek Date: 2010-03-21T14:23:58+09:00 Subject: Re: A plugin system using extend --000325564dfa89211a048248c9e7 Content-Type: text/plain; charset=ISO-8859-1 On Sat, Mar 20, 2010 at 9:46 PM, Jean-denis Vauguet wrote: > Hi. > > I tried to design a simple plugin system using :extend called upon > instance (http://ruby-doc.org/core/classes/Object.html#M000335 usual > documented behavior), so as to inject some methods redefinitions while > keeping the ability to call super in order to fall back on the default > behavior if needed. > > It looks like this: http://gist.github.com/339025 > It works as expected, as one can see by reading the commented output at > the end of the file: the inheritance chain is altered, with the plugin > "subclassing" the original class of the plugin receiver. > > Yet, I tried to introduce some modularity in the system, so I went for: > http://gist.github.com/339028 > It fails but I don't understand why. The inheritance chain is not > altered. > I tried to use :include instead of :extend, which gives merely the same > behavior (overwriting instance methods), but this time the plugin module > is added as the parent of the original class within the inheritance > chain, so it is useless for the purpose ;) > > I'll be glad if someone could give me a hint on this :) > Thank you! > -- > Posted via http://www.ruby-forum.com/. > > Hi, I got it to do what I think you are looking for by having plugins pass the object to extend. http://gist.github.com/339101 To test it, I had the redefined object say things in reverse. I also added another module to show that you can activate different behaviours in different objects. Then I played around with it a little bit more, trying to make it more modular, and behave similar to ActiveRecord's scopes http://gist.github.com/339114 I wanted to try to make the plugin agnostic to the method it was activating so that you could, for example, make a plugin that could then be applied to any method of any object. But I just can never seem to get define_method to work right -.- every time I try to do that, I seem to struggle a lot with it, and usually end up using eval with a string, because dynamically adding a method is so difficult. Oh well, I'm kind of happy with it. Guess I should pick up PragProg's Metaprogramming Ruby book... or try out Lisp >:D --000325564dfa89211a048248c9e7--