3 Replies - 1207 Views - Last Post: 08 October 2009 - 11:04 AM Rate Topic: -----

#1 tuntis  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 10-November 08

Loading class from folder based on user input

Posted 08 October 2009 - 09:05 AM

This is something quite simple that I just don't know how to implement in the best way.

Let me explain better - I'm working on a project called risg: it's a program that parses IRC logfiles and generates fancy statistics based on them.

Now, it's obvious that it needs to support many kinds of logfile formats and output formats: you can see that I've implemented a system for both logfile formats and output formats (see lib/risg/format and lib/risg/output).

But there's one problem - I don't know how to allow the user to decide which format to use for logfiles and output. If I add a command line switch like "--format" to the main executable, how should I load and instiate the right class from the format directory based on user input?

And while I'm at it, I've been wondering if I should package the application as a Rubygem: is it an optimal way of deployment/distribution?

Is This A Good Question/Topic? 0
  • +

Replies To: Loading class from folder based on user input

#2 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 916
  • View blog
  • Posts: 3,206
  • Joined: 12-May 09

Re: Loading class from folder based on user input

Posted 08 October 2009 - 09:44 AM

Gems are nice.

As for selecting the correct format:
You could have a variable that will hold the log somehow.
Have some sort of conditional
if format1
	require "format1"
	logger = format1.new(args)
else
	require "format2"
	logger = format2.new(args)
end #endif



As long as each format conforms to the same interface, i.e. it has some sort of method that you can call throughout the program (something like write, log, etc), you should be good to go.
Was This Post Helpful? 0
  • +
  • -

#3 tuntis  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 10-November 08

Re: Loading class from folder based on user input

Posted 08 October 2009 - 10:18 AM

I did obviously think of doing that, but it occurred to me that somebody might want to write their own log/output format, and that they could simply put it into the right directory and it would be usable without any other source hacks.
Was This Post Helpful? 0
  • +
  • -

#4 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 916
  • View blog
  • Posts: 3,206
  • Joined: 12-May 09

Re: Loading class from folder based on user input

Posted 08 October 2009 - 11:04 AM

I mean I don't see any issue with using the command line argument, and then a
require "#{ARGV[0])" and maybe having a default value if they don't specify one.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1