views:

38

answers:

3

I've been developing Rails apps now for about a year and am starting on a new project (that's actually 3 projects). I'd like to write some gems that can be shared amongst these projects to maintain a common code-base. They'll likely eventually evolve into Rails engines at some point, but to start, just common libraries that can be included into each app.

I've never authored a rubygem before so I'm looking for tips and tricks as to where/how I should start building this gem:

  • Is it easiest start programming it nested within a rails app, say, as a git submodule to take advantage of existing rails environment etc...?
  • Should I start programming it just as a standalone library? (my non-rails experience is fairly rudimentary so i'm not sure about structure/setup/testing)

I'd like to stick within the simple Rails conventions as much as possible with proper testing environment etc... Probably won't be Rails3 adopters yet but will obviously head in that direction.

Any tips, tricks, suggestions or recommended resources are all greatly appreciated!!

A: 

What i usually do is first write the functionality inline where i need it - like model level etc. when i notice that it can be reused, it extract it into a separate module under lib/ext/modulename. And when its clear that this piece of functionality could be used as a separate gem, i just make a new project, port the code into proper structure, handle any possible initialization, add documentation and commit to github.

Later on when you need to make modifications to the gem, i just do my stuff in the separate project, build the gem, install it with gem install --local project.gem. You could also install it from git as a plugin, edit it straight in the vendor dir and later backport the changes to the gem project.

Tanel Suurhans
A: 

rails plugin might fit the bill

rogerdpack
A: 

Whether you make it a stand-alone or pull it apart after the fact probably depends on how well you see the boundary between your gem and the rest of the code. If the responsibilities are very clear I'd start out apart. If its not clear exactly where things start/end you probably want to make it work before you make it right to stop yourself from doing a bunch of busy-work multiple times.

However you manage to pull the reusable portions of your code out, once you have the code ready to go (or before if that's the way you roll) you may want to take a look at jeweler, http://github.com/technicalpickles/jeweler, particularly if you're going to share the gem with the community.

There's also a book: Practical Ruby Gems, it's a bit dated, but you can find a used copy for less than $1.

Paul Rubel
Thanks for pointing out Jeweler, looks like a good tool for getting me started with a base structure. Also I Won't be sharing this code as it's only relevant for our internal apps, but Github hosting will be used so I think it still works perfectly!
brad