Where to put algorithm class


#1

I have a large class of algorithms (50-100 methods) used in calculating
some results for a model class. I have created a separate algorithm.rb
file which contains the class. Where should this file go ? Should it
even be a module instead of a class (though I do need it to hold class
variables during the calculations)? Finally, how do I call it from the
model active record class ?


#2

john wrote:

I have a large class of algorithms (50-100 methods) used in calculating
some results for a model class. I have created a separate algorithm.rb
file which contains the class. Where should this file go ?

If it doesn’t use ActiveView or ActiveRecord, it goes in the lib/
folder.

Should it
even be a module instead of a class (though I do need it to hold class
variables during the calculations)?

We use Modules when we want to isolate the name of a class, and/or when
we
want the Module to mix-in with other classes. If you don’t need those,
then
do what’s simplest. Plop a Class into the lib/ folder, and worry about
name
collisions when they happen.

(If you publish this class, however, that formula no longer applies, and
you
should put the class in a module with a name that others are not likely
to
use.:wink:

Finally, how do I call it from the
model active record class ?

Given lib/algorithm.rb, write:

require 'algorithm'

up at the top of the Model that uses it.

And toss your test cases into test/unit . (You do have test cases,
don’t
you?:wink:


Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!


#3

If you are using your algorithms in places besides your webapp, why
not make yourself an algorithms gem and install it on your system?


#4

Phlip wrote:

If it doesn’t use ActiveView or ActiveRecord, it goes in the lib/ folder.

Given lib/algorithm.rb, write:

require 'algorithm'

okay

And toss your test cases into test/unit . (You do have test cases, don’t
you?:wink:

How do I set up a test stub for the separate class file. To get code
converted to ruby, I had made it a separate controller and yes, have
been testing every method.