hash_extension

Get Version

0.0.4

What

hash_extension is a Ruby on Rails plug-in that does two things. First, it provides ActiveRecord finder methods that return hashes. Second, it makes hashes more object-like. Why? When you request a lot of objects with an ActiveRecord query, there’s a lot of overhead in creating the objects that come back. If you’re not modifying the records, but just displaying them or performing local calculations before display, you don’t need to suffer the overhead of creating heavy ActiveRecord objects. It turns out it’s much faster to get back hash objects. This plug-in makes it a snap to replace a query that returns ActiveRecord objects with one that returns hashes.

Installing

sudo gem install hash_extension

The basics

The hash_extension plug-in allows you to treat hashes like objects, and to get back hashes from (almost) standard ActiveRecord queries. E.g,. instead of:

hash['foo']

you can say:

hash.foo

Instead of:

hash['foo'] = 'bar'

you can say:

hash.foo = 'bar'

Two variants of basic ActiveRecord query types are supported—find and find_by_sql, inventively recast as find_as_hashes and find_by_sql_as_hashes. E.g.,

Foo.find_as_hashes(:all)
Foo.find_as_hashes(:first)
Foo.find_as_hashes(:all, :conditions => "bar = 'baz'")
Foo.find_by_sql_as_hashes("select * where fubar = 'some_complex_stuff'")

The trunk repository is svn://rubyforge.org/var/svn/hash_extension/trunk for anonymous access.

From Rails

First, add the following to the bottom of your environment.rb file:

require 'hash_extension'

Anywhere you have syntax like this:

MyObject.find(:all)

You can replace it with this:

MyObject.find_as_hashes(:all)

The same data is returned, but without the overhead of creating large, complex ActiveRecord objects. The rest of your code will continue to behave as expected, because the object-like access will now work correctly with the hashes returned by these methods.

License

This code is free to use under the terms of the MIT license.

Credits.

The initial release only provided the extension to hash that made them act more like objects. I posed the challenge: “If someone would like to contribute extensions to ActiveRecord that would create finders that automatically return hashes, e.g., “find_as_hashes(:all)”, I’d be thrilled to include it as part of this plug-in.” Elliot Laster rose to the challenge and provided the ActiveRecord extension available in the 0.0.2 release.

The new call to action is to provide some unit tests! Ideally, they should use migrations to set up dummy tables, test against them, then tear down those tables. Anyone up for it? :-)

Contact

Comments are welcome. Send an email to Dan Chak.

FIXME full name, 10th February 2008
Theme extended from Paul Battley