My First Ruby Gem: Hashed_Attributes

I just wrote and released my first Ruby Gem, Hashed Attributes https://rubygems.org/gems/hashed_attributes. It is a very simple ActiveRecord extension for saving variables through a serialized hash. The Gem will let you declare getter and setter methods that use a hash to store assigned data. For instance, instead of doing something like

@user.preferences[:favorite_color] = 'orange'

you can do

@user.favorite_color = 'orange'

The value for favorite color will be kept in a serialized hash.

  person = Person.new(:favorite_color=>"orange")
  {
               :id => nil,
      :preferences => {
          :favorite_color => "orange",
      },
       :created_at => nil,
       :updated_at => nil
  }

Setup in the model is very straight forward. The first argument is the column name you want to use followed by a list of methods used as keys on the hash.

# Example model
class Person < ActiveRecord::Base
  hashed_attributes :preferences, :favorite_color, :theme, :plan # etc...
end

ActiveRecord is required to use this gem. Additionally, you need to add a column in your database.

# Sample migration
create_table :people do |t|
   t.text :preferences
end

To install

gem install hashed_attributes

Or place in your Rails Gemfile

gem 'hashed_attributes'

The project code is hosted on Github: https://github.com/bseanvt/hashed_attributes and https://rubygems.org/gems/hashed_attributes

Releasing this gem was a lot of fun and I’m looking forward to writing more gems!

Ruby Enterprise Edition and Passenger ./script/console production fails and instead returns Loading production environment (Rails 2.3.5) Rails requires RubyGems >= 1.3.2. Please install RubyGems and try again: http://rubygems.rubyforge.org

After installing Ruby Enterprise Edition, REE, and Passenger on Ubuntu you may see this error message when you run script/console for the first time

./script/console production
# =>
Loading production environment (Rails 2.3.5)
Rails requires RubyGems >= 1.3.2. Please install RubyGems and try again: http://rubygems.rubyforge.org

You then scratch your head and run

which gem
which ruby
which rails

to find that all appears to be in order. You have rubygems installed , ruby is installed and so is rails. You also find that each are pointing to the correct location, which is something like /usr/bin/gem -> /opt/ruby-enterprise-x.x.x.x/bin/gem, where x.x.x.x is the version of REE.

The problem isn’t however, with any of the above. The issue is with the location of irb. If you installed (like me) irb with apt-get install irb, then irb isn’t aware of your shiny new REE and ruby gems. It’s a simple fix however, unlink irb and symlink the /usr/bin/irb to REE’s irb like so…

rm /usr/bin/irb

And symlink it to the irb that REE has in bin

ln -s /opt/ruby-enterprise-x.x.x.x/bin/irb /usr/bin/irb

Now cd into your rails app and run

./script/console production

Installing Monk on Ubuntu with Ruby Gems

Installing monk like this will fail

gem install monk

You’ll need to install the wycats-thor gem first with this command

gem install wycats-thor -s http://gems.github.com