has_enumeration 0.2.1
Extends ActiveRecord with the has_enumeration method allowing a symbolic enumeration to be stored in an ActiveRecord attribute. The enumeration is specified as a mapping between symbols and their underlying representation in the database. Predicates are provided for each symbol in the enumeration and the symbols may be used in finder methods. For example: class TrafficLight < ActiveRecord::Base # Implicit mapping when the attribute is a string storing the string # version of the symbol: has_enumeration :color, [:red, :yellow, :green] # Explicit mappings between symbols and underlying values are also # supported. E.g.: # has_enumeration :color, :red => 1, :yellow => 2, :green => 3 end tl = TrafficLight.new tl.color = :red tl.color.value # ==> :red tl.color.red? # ==> true tl.color.yellow? # ==> false TrafficLight.all(:conditions => {:color => :red}) # ==> all red traffic lights When using ActiveRecord 3, the symbolic values can also be used when interacting with the underlying Arel attributes for a model: attr = TrafficLight.arel_table[:color] TrafficLight.where(attr.in([:yellow, :green])) # ==> yellow or green lights has_enumeration has been tested with: Rubies: Ruby 1.8.7, Ruby 1.9.2, and JRuby 1.5.1. ActiveRecord: 2.3.8 and 3.0.0.rc