Saturday, May 30, 2009

Returning the keys of all documents in CouchDb

There's a bit of a learning curve when trying to use CouchDb's mapreduce. One of the harder parts is to write the reduce function, which can have two separate cases: called from the map functions, and called again from reduce functions.

When you emit data from map, the examples show you emitting the document, but you can emit any data structure you care to dream up in the key and value portion of the emit. I needed a mapreduce view that returned all the keys that were present in the all the documents. So if I had documents in the db in the form:
{"year": 2008, "birth_rate": 20.0 }
{"year": 2009, "birth_rate": 21.0 }
{"year": 2008, "death_rate": 20.0 }
{"year": 2009, "death_rate": 20.0 }

I wanted something that returned: ["year", "birth_rate", "death_rate"]

Here's one way to do it:


Wednesday, May 27, 2009

How to add helpers, controllers, models, and views of your plugin into the Rails loadpath

Sometimes, when you're writing a plugin, you end up writing models,
helpers, and controllers that the main app can use.  However, you
don't want to copy it into the main app all the time.  You'd like to
keep things separate between the plugin, but you'd like to be able to
include it in the path of the main app.

To do this, put the following in your init.rb file in the root
of your plugin.  To add a new view path in your plugin that's at
PLUGIN_ROOT/lib/views (where PLUGIN_ROOT is the root directory of your

ActionController::Base.append_view_path(File.join(PLUGIN_ROOT, "lib", "views"))

Any template files (like html.erb) that you put in that path will be
seen in your app.

To add new helper, model, or controller directories in the rails load path:

%w{ helpers model controller }.each do |dir|
path = File.join(PLUGIN_ROOT, 'lib', dir)
$LOAD_PATH << path
Dependencies.load_paths << path

And now, any models you put in lib/model, lib/controller, and
lib/helpers will be in the rails load path.

Of course, this might all be moot with the reintroduction of Rails
engines in 2.3.  I haven't gotten around to using them or figuring it
out yet, but for now, this is how you do it with plugins.  tip!

Posted via web from The Web and all that Jazz

Friday, May 22, 2009

Never mix package managers

Today, I decided to dive into Python and explore some web frameworks.
Instead, I've spent the better part of the day messing around with
package problems on Mac OsX.
The problem? Mixing package managers. I had installed py-setuptools
from Mac Ports. Turns out it's still far behind, so it barfs when
easy_install uses it to install something like mysql-python.egg.
I should know better. On Ubuntu, I only use apt to get the language,
and the rest is managed in rubygems.

Posted via email from The Web and all that Jazz