Code organisation "template" interference(?) with RUBYLIB envar

P

Paul van Delst

(Warning: this post is waaaaay longer than I intended)

Hello,

Apologies if the subject line is a bit cryptic, but it's not easy to describe in a single
line. Anyway...

A while back an astute fellow on comp.lang.ruby (thanks RobertK!) provided me with some
advice on how to lay out ruby classes and code that ended up being so useful that I've
applied the same organisational template for several projects. I may have butchered the
advice, but basically I have the following sort of setup (apologies for the lengthy ASCII
art too),

---project1 (module Proj1)
| |--lib
| | |---project1.rb (class Proj1::Runner)
| | |---config
| | | |
| | | `--config.rb (class Proj1::Config)
| | |---base
| | | |
| | | `--base.rb (class Proj1::Base)
| | |---runner1 (module Proj1::Runner1)
| | | |
| | | |--base.rb (class Proj1::Runner1::Base < Proj1::Base)
| | | |--runner1.rb (class Proj1::Runner1::Runner < Proj1::Runner1::Base)
| | | |--class1.rb (class Proj1::Runner1::Class1 < Proj1::Runner1::Base)
| | | |--class2.rb (class Proj1::Runner1::Class2 < Proj1::Runner1::Base)
| | | ...etc
| | `---runner2 (== module Runner2)
| | |
| | |--base.rb (class Proj1::Runner2::Base < Proj::Base)
| | |--runner2.rb (class Proj1::Runner2::Runner < Proj1::Runner2::Base)
| | |--class1.rb (class Proj1::Runner2::Class1 < Proj1::Runner2::Base)
| | |--class2.rb (class Proj1::Runner2::Class2 < Proj1::Runner2::Base)
| | ...etc
| |--test
| | |
| ...etc
|
|-project2 (module Proj2)
| |--lib
| | |---project2.rb (class Proj2::Runner)
| | |---config
| | | |
| | | `--config.rb (class Proj2::Config)
| | |---base
| | | |
| | | `--base.rb (class Proj2::Base)
| | |---runner1 (module Proj2::Runner1)
| | | |
| | | |--base.rb (class Proj2::Runner1::Base < Proj2::Base)
| | | |--runner1.rb (class Proj2::Runner1::Runner < Proj2::Runner1::Base)
| | | |--class1.rb (class Proj2::Runner1::Class1 < Proj2::Runner1::Base)
| | | |--class2.rb (class Proj2::Runner1::Class2 < Proj2::Runner1::Base)
| | | ...etc
| | `---runner2 (== module Runner2)
| | |
| | |--base.rb (class Proj2::Runner2::Base < Proj2::Base)
| | |--runner2.rb (class Proj2::Runner2::Runner < Proj2::Runner2::Base)
| | |--class1.rb (class Proj2::Runner2::Class1 < Proj2::Runner2::Base)
| | |--class2.rb (class Proj2::Runner2::Class2 < Proj2::Runner2::Base)
| | ...etc
| |--test
| | |
...etc

etc.

For any project, call it module ProjectX:
- the lib/projectX.rb file (class Project2::Runner) is the interface to a
user. It's the "main" runner class if you like.

- the ProjectX::Config class holds, surprise, all the configuration info
(usually read in from a text file)

- the ProjectX::Base clase holds all the constants and methods that are
shared throughout any particular ProjectX module

- the ProjectX::RunnerY::Base class holds all the constants and methods
that are shared throughout any particular ProjectX::RunnerY module

- the ProjectX::RunnerY::Runner class is the controller that iterates over
all the current module classes (the Class1, Class2,...ClassZ etc) and
invokes their methods

- the ProjectX::RunnerY::ClassZ class actually does something. :eek:)


I like the above setup because it scales quite well. For some projects each "runnerY"
contains another layer. It's made it really easy to isolate functionality and test it.

Anyway, the problem I'm having now is I decided to consolidate all my code on a single
machine and tack on the various locations of everything onto the RUBYLIB envar, i.e.

export RUBYLIB=$HOME/ruby/project1/lib:$HOME/ruby/project2/lib: etc...

So now when I do something like the following in project2/lib/runner1/base.rb,

require 'base/base'
module Proj2
module Runner1
class Base < Proj2::Base
# This is how the config gets passed along in
# Runner1 module and holds shared Runner1 code
end
end
end

and in project2/lib/runner1/runner.rb,

require 'runner1/base'
module Proj2
module Runner1
class Runner < Proj2::Runner1::Base
RUNNERS=[Class1,Class2,....,ClassZ]
def run
RUNNERS.each do |run_class|
r = run_class.new
r.config = self.config
r.run
end
end
end
end
end

and invoke the main script, ruby searches the RUBYLIB paths and finds the "base/base.rb"
for project*1* first (since it's listed first) and loads that file instead of the
"base/base.rb" for project*2*. Thus, I get errors like:

project2/lib/runner1/base.rb:4: uninitialized constant Project2::Base (NameError)

If I swap the listing in my RUBYLIB envar so that the project2 directory is listed first,
everything is honky dory and runs fine.

Phewph! If you've made it this far, the beer is on me if we ever meet.

My questions are:

1) can I keep my current directory structure (which, believe it or not, seems quite
logical to me) and avoid these file loading order problems?

2) IF not, how to fix this? I like the idea of generic file names and classes (config,
base) across projects so that those that follow can grok multiple projects after studying
the docs for just one.

Thanks for any insight.

cheers,

paulv
 
B

Brian Candler

---project1 (module Proj1)
| |--lib
| | |---project1.rb (class Proj1::Runner)
| | |---config
| | | |
| | | `--config.rb (class Proj1::Config) ...
require 'base/base'
module Proj2
module Runner1

I'd suggest putting a single entry in your load path, for the very top level
of your directory structure, and then use

require 'project1/lib/base/base'

instead of

require 'base/base'

This is the simplest way of easily distinguishing project1 from project2 if
the subdirectories and files contained by both have the same names
(especially if project1 needs to use files within project2)

If all projects are independent, then clearly when you run an application
from project1 you only need to put project1's lib directory into the RUBYLIB
environment.

If you want to automate this, then create a bunch of files at the top level
of your lib tree

[project1.rb]
$:.unshift "/path/to/project1/lib"

[project2.rb]
$:.unshift "/path/to/project2/lib"

Then your programs can say:

require 'project1'
require 'base/base'

or whatever. (That means you only need to put one extra line at the top of
each source file)

Just a couple of ideas.

B.
 
P

Paul van Delst

Brian said:
I'd suggest putting a single entry in your load path, for the very top level
of your directory structure, and then use

require 'project1/lib/base/base'

instead of

require 'base/base'

This is the simplest way of easily distinguishing project1 from project2 if
the subdirectories and files contained by both have the same names
(especially if project1 needs to use files within project2)

Hi Brian,

Your suggestion is how I first thought about "fixing" my problem. But when other folks
check out my code from the repository, they may not have the same directory structure as I
do. Then I get calls about files not found etc.
If all projects are independent, then clearly when you run an application
from project1 you only need to put project1's lib directory into the RUBYLIB
environment.

If you want to automate this, then create a bunch of files at the top level
of your lib tree

[project1.rb]
$:.unshift "/path/to/project1/lib"

[project2.rb]
$:.unshift "/path/to/project2/lib"

Then your programs can say:

require 'project1'
require 'base/base'

or whatever. (That means you only need to put one extra line at the top of
each source file)

Thanks for the tip. I think I'll look into this... I do something like this for my test cases.

cheers,

paulv
 
P

Paul van Delst

Brian said:
If you want to automate this, then create a bunch of files at the top level
of your lib tree

[project1.rb]
$:.unshift "/path/to/project1/lib"

[project2.rb]
$:.unshift "/path/to/project2/lib"

Then your programs can say:

require 'project1'
require 'base/base'

or whatever. (That means you only need to put one extra line at the top of
each source file)

Hi again,

I just tried adding

$:.unshift File.join(File.dirname(__FILE__))

to the "parent" file and it works a treat!

I should've figured it out on my own, but thanks muchly for the
hint/nudge-in-right-direction. :eek:)

cheers,

paulv
 

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
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top