Truly modular

  • Thread starter Jesse van den Kieboom
  • Start date
J

Jesse van den Kieboom

Hi,

As Ruby claims to be very modular I'd like to do the following (I tried
some things but I think I'm going in the wrong directions).

I have my main Ruby application. The applications need to be extendible.
My idea is to write extra .rb files and put them in one directory. The
main app should somehow load those files to extend functionality. on
runtime the files can changes and the main app should be able to reload
the extensions. Also new files can be added and these should be reloaded
too.

I don't know the right approach to this problem. The external .rb files
somehow need to have access to parts of the main application and the
main application obviously needs to call functions from the external
files.

Users can communicatie with the main app telling it there are new
external files to be loaded, the name of the files and maybe function
names to be called or whatever. The idea is that I don't want to restart
the main application but do want to extend it on the flow.

Somebody with some great ideas how to achieve this?


Jesse van den Kieboom
 
R

Robert Klemme

Jesse van den Kieboom said:
Hi,

As Ruby claims to be very modular I'd like to do the following (I tried
some things but I think I'm going in the wrong directions).

I have my main Ruby application. The applications need to be extendible.
My idea is to write extra .rb files and put them in one directory. The
main app should somehow load those files to extend functionality. on
runtime the files can changes and the main app should be able to reload
the extensions. Also new files can be added and these should be reloaded
too.

I don't know the right approach to this problem. The external .rb files
somehow need to have access to parts of the main application and the
main application obviously needs to call functions from the external
files.

Users can communicatie with the main app telling it there are new
external files to be loaded, the name of the files and maybe function
names to be called or whatever. The idea is that I don't want to restart
the main application but do want to extend it on the flow.

Somebody with some great ideas how to achieve this?

Great? Dunno... As far as I can see here are two problems:

i) file loading with respect to changes

ii) communication between the code in the files and your app

I can suggest something for i) (see below) but for ii) I'd have to know
more about the nature of the app.

Kind regards

robert



require 'singleton'

class ExtensionManager
include Singleton

def initialize
@files = {}
end

def refresh
@files.dup.each do |file, time|
load_file(file) if File.mtime(file) > time
end

self
end

def load_file(file)
load(file)
@files[file] = Time.now
self
end

def maybe_load(file)
load_time = @files[file]
load_file(file) if load_time.nil? || File.mtime(file) > load_time
self
end
end


ExtensionManager.instance.maybe_load( "test.rb" )
ExtensionManager.instance.maybe_load( "test.rb" )

sleep 2
File.open("test.rb", "a") {|io| io.puts("")}

ExtensionManager.instance.refresh
# ExtensionManager.instance.maybe_load( "test.rb" )
# ExtensionManager.instance.maybe_load( "test.rb" )
 
J

Jesse van den Kieboom

Op wo 15-09-2004, om 18:19 schreef Robert Klemme:
Great? Dunno... As far as I can see here are two problems:

i) file loading with respect to changes

This is not the main issue though. If I have to I can manually tell the
application to reload a certain file. Or use GnomeVFS for instance.
ii) communication between the code in the files and your app
I can suggest something for i) (see below) but for ii) I'd have to know
more about the nature of the app.

My application gets data through a TCPSocket. All the data for example
is prefixed by a certain command. I would like to have some sort of
modular support for these commands which means that I can dynamically
add a certain action for a new command. My guess was that this could
best be done by loading external files, let the files register some
command/actionfunction and have some sort of communication between the
files code and the main app code. The communication is the problem.


Jesse van den Kieboom
 
H

Henrik Horneber

My application gets data through a TCPSocket. All the data for example
is prefixed by a certain command. I would like to have some sort of
modular support for these commands which means that I can dynamically
add a certain action for a new command. My guess was that this could
best be done by loading external files, let the files register some
command/actionfunction and have some sort of communication between the
files code and the main app code. The communication is the problem.

Hi!

This reminds me a little of rbot ( http://linuxbrit.co.uk/rbot/ ), a
ruby irc bot that provides a plugin framework. You can drop files
('plugins') into a plugin directory, and have this plugins register
themselves for some commands. Then chatters use these commands when they
message the bot. For example, the google.rb plugin registers itself
under 'search', so you can ask in a channel 'rbot search what i want to
find' and the bot queries google. Of course you still have the IRC
protocol in between you and the TCPSocket, but the general layout may
give you some hints.

Hope that helps and was understandable ;)

Henrik
 
J

Jesse van den Kieboom

Op wo 15-09-2004, om 20:22 schreef Henrik Horneber:
Hi!

This reminds me a little of rbot ( http://linuxbrit.co.uk/rbot/ ), a
ruby irc bot that provides a plugin framework. You can drop files
('plugins') into a plugin directory, and have this plugins register
themselves for some commands. Then chatters use these commands when they
message the bot. For example, the google.rb plugin registers itself
under 'search', so you can ask in a channel 'rbot search what i want to
find' and the bot queries google. Of course you still have the IRC
protocol in between you and the TCPSocket, but the general layout may
give you some hints.

Hope that helps and was understandable ;)

Henrik

Thanks, this is just what I needed!

Jesse
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,413
Latest member
ReeceDorri

Latest Threads

Top