Ruby Gems: gems github hashed_attributes open source rubgems.org rubygems
by bseanvt
leave a comment
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!
Posts: configuration dreamhost freeze gems MySQL port Rails unpack
by bseanvt
leave a comment
Deploying to Dreamhost
Remember to include the host declaration in the database.yml file when you deploy to Dreamhost. Dreamhost does not use “localhost” which is typically the default setting when using the mysql adapter and developing locally or even on a small site.
At least for me, when I ported a Rails app to Dreamhost, this was the only “Gotcha”, because my log files were not reporting any errors and were instead serving the 500 something went wrong file.
A sample config/database.yml file
production: adapter: mysql username: youruser password: yourpasswd database: ror_production_db host: mysql.yourdomain.com
To port, I unpack my gems, if I haven’t already
rake gems:unpack
Then I freeze and package rails w/ my app just in case versions aren’t exact
rake rails:freeze:gems
Then I upload to Dreamhost!
Send Mail in Ruby with a Pony
Great little gem that let’s you quickly and easily send out mail from your ruby scripts.
gem install pony require 'rubygems' require 'pony' Pony.mail :from=>"me@example.com", :to=>"you@example.com", :subject=>"hello", :body=>"world"
Databases: config datamapper do_mysql gems MySQL Sinatra
by bseanvt
leave a comment
Install do_mysql Ruby Gem on Mac OS X
I ran into the same problem when installing mysql gem for Rails development. This fix worked for me http://seanbehan.com/programming/fixing-mysql-for-rails-2-2-development-on-mac-os-x/
The same thing works with the data objects gem. Just specify the path the mysql config that it’s using and the gem should install just fine.
gem install do_mysql -- --with-mysql-config=/opt/local/lib/mysql5/bin/mysql_config
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
Fixing MySQL for Rails 2.2 Development on Mac OS X
Oh what trouble Rails 2.2 and MySQL (on Mac OS X) can be. Rails, as of version >= 2.2, no longer comes bundled with the MySQL adapter. This means you’ll need to install it yourself, but it appears that the gem for installing it is also broken.
This will fail
gem install mysql
What you need to do is tell the gem which MySQL to use. I installed MySQL with mac ports, http://macports.org , so I need to specify this when I run the installation command.
gem install mysql -- --with-mysql-config=/opt/local/lib/mysql5/bin/mysql_config
Check out an earlier post explaining how to install MySQL with mac ports http://seanbehan.com/programming/installing-sphinx-search-engine-on-mac-os-x-or-ld-library-not-found-for-lmysqlclient/
You’re not out of the woods yet. Rails needs to know the location of the socket it uses to connect to the MySQL server. I created a symbolic link to the location Rails normally looks for this socket. You could specify the location of the socket in the config/database.yml file, however, then you’ll need to do this with each of your applications.
ln -s /private/tmp/mysql.sock /opt/local/var/run/mysql5/mysqld.sock
If there is an easier way to get MySQL back for Rails on Mac OS X please let me know. I’d love a quick fix to this problem.


