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.
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_attributesOr 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!
Just finishing up brewing up some fresh ground comments...