cymbalize
Use symbols in ActiveRecord columns. This gem implements a subset of nofxx's symbolize gem.
Installation
Include the gem in your Gemfile:
gem 'cymbalize', '~> 0.1.0'
Features
Please note that :scopes
, :methods
, and :allow_blank
don't do
anything unless :in
is an array with at least one symbol.
Symbolize a column with symbolize
class User < ActiveRecord::Base
symbolize :gender
end
u = User.new(:gender => :robot)
u.gender # :robot
Add a list of valid values with :in
class User < ActiveRecord::Base
symbolize :gender, :in => [:decepticon, :autobot]
end
u = User.new(:gender => :autobot)
u.valid? # true
u.gender = :robot
u.valid? # false
Add boolean methods with :methods
class User < ActiveRecord::Base
symbolize :gender, :in => [:asterix, :obelix], :methods => true
end
u = User.new(:gender => :asterix)
u.asterix? # true
u.obelix? # false
Add ActiveRecord scopes with :scopes
class User < ActiveRecord::Base
symbolize :gender, :in => [:zerg, :protoss, :terran], :scopes => true
end
u = User.create(:gender => :zerg)
User.zerg.include?(u) # true
User.protoss.include?(u) # false
Allow blank values with :allow_blank
class User < ActiveRecord::Base
symbolize :gender, :in => [:mod, :rocker], :allow_blank => true
end
u = User.new(:gender => nil)
u.valid? # true
u.gender # nil
u.gender = ''
u.valid? # true
u.gender # nil