How to write scripts with plug-in support [long]

Hi all,
( I’m not a great ruby programer, I’m not a great
programmer at all, but )

I need to write a sw in ruby that would allow people
adding plug-in/modules
to handle some event driven functions.
I can think of two ways of doing this:

#1 way

-create a Plugin class, with methods that won’t do
nothing
(actually, I suppose this could be a mix-in)
-create the plugins as MyPlug<Plugin and redefine the
necessary methods
-register MyPlug in an array
-define onEventXY as “proxy-methods” that would call
$PluginArray.each onEventXY
(maybe defining them as Plugin class methods)

I suppose this should work, but it seems that the last
step is too long and complex, and
anyway this approach would end calling a lot of null
methods in any Plugin.

I even thought about having a check , something like
$PluginArray.each do |x|
x.method if x.has_the_method(method)
end

but this way I would add a check that actually would
be computationally worst than calling
a null method, and I would even add complexity to the
script, I suppose.

#2 way

-work on Method or create a myMethod class
-create a plugin (MyPlugMethod<myMethod or
myMethod.new) for each added method
-register the method in $MethodXYArray
-define onEventXY as “proxy-methods” that would call
$MethodXYArray.each …

in this way I would lose a huge time loading the
plug-in files, writing them,
and I don’t even know how they could interact.

(I mean , eventX still could set a state that eventY
would unset,
and such things, but I don’t know if could be other
ways of interaction)

After all this raving I come to the question:
is there some Rubish Right Way ™ to write SW with
plug-in support?

PS
(sorry about my english)

···

=====
icq #69488917


Yahoo! Musica: notizie, recensioni, classifiche, speciali multimediali
http://it.yahoo.com/mail_it/foot/?http://it.music.yahoo.com/

gabriele renzi wrote:

I need to write a sw in ruby that would allow people
adding plug-in/modules
to handle some event driven functions.
I can think of two ways of doing this:

-create a Plugin class, with methods that won’t do
nothing

This is not necessary in Ruby

(actually, I suppose this could be a mix-in)
No, you don’t even need a mix-in for that.

I suppose this should work, but it seems that the last
step is too long and complex, and
anyway this approach would end calling a lot of null
methods in any Plugin.

This is true : suboptimal.

After all this raving I come to the question:
is there some Rubish Right Way ™ to write SW with
plug-in support?

I think I have an extremely simple but useful callback-mechanism,
I send it in an attachment.

Usage :

You define a callback stack (instance of of CallbackStack)
for each of your event.

Your plugins can register a callback by calling the register
method with some block (The block will be executed if the
corresponding event occurs.)

Your application calls the call method of its callback stack
if the specified event occures.

That’s all.

callbacks.rb (785 Bytes)

I think I have an extremely simple but useful callback-mechanism,
I send it in an attachment.

I did not recieve this attachment. Is this posted somewhere?

Looks interesting,

~ Patrick

> I think I have an extremely simple but useful > callback-mechanism, > I send it in an attachment.

Well, I knew I’d have some good suggestion :slight_smile:
tnx, this is quite ideal, just a last thing:
could you even suggest some way to automagically
build up the instances for the events?
(just for fun, I can write 'em all in little time,
just playing a little with this idea,
and could’nt get a good answer)

thanks again

···

=====
icq #69488917


Yahoo! Musica: notizie, recensioni, classifiche, speciali multimediali
http://it.yahoo.com/mail_it/foot/?http://it.music.yahoo.com/

I think I have an extremely simple but useful callback-mechanism,
I send it in an attachment.

I did not recieve this attachment. Is this posted somewhere?

Looks interesting,

~ Patrick

Here it is… pasted below.

Hal

#!/usr/bin/env ruby

class CallbackStack
def initialize
@table = {}
@id = 0
end

You should give your callback a unique name (id)

if you want to delete or overwrite it!

The stack returns a unique id if you don’t specify any (nil).

def add(proc=nil,id=nil)
if id
if proc
@table[id] = proc
id
else
@table.delete(id)
end
else
if !proc
@table.clear
nil
else
@id+=1 while @table[@id]
@table[@id] = proc
@id
end
end
end

def call(*x)
for id,proc in @table
proc.call(*x)
end
end

def register(&block)
add(block)
end

call it now + register

def always(&block)
block.call(self)
add(block)
end
end

···

----- Original Message -----
From: “Patrick May” patrick-may@monmouth.com
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Friday, August 30, 2002 6:36 PM
Subject: Re: How to write scripts with plug-in support [long]

gabriele renzi wrote:

Well, I knew I’d have some good suggestion :slight_smile:
tnx, this is quite ideal, just a last thing:
could you even suggest some way to automagically
build up the instances for the events?

It depends on your programm structure…