A tiny rails plugin to assist in simulating enum db types in a db-agnostic fashion
Ruby
Pull request Compare This branch is 2 commits ahead, 1 commit behind FOMNick:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
spec
.gitignore
.rspec
MIT-LICENSE
README.rdoc
Rakefile
enum_simulator.gemspec
init.rb
install.rb
uninstall.rb

README.rdoc

EnumSimulator

A simple plugin for abstracting out standard ActiveRecord enumerated attributes

Installation

Let bundler do all the work for you by adding this to your Gemfile:

gem 'enum_simulator', :git => 'git://github.com/FOMNick/enum_simulator.git'

And then execute:

bundle install

Or install it manually:

sudo gem install FOMNick-enum_simulator

Example

In order to mark an AR attribute as enumerated, make sure the column in question is a string and large enough to accommodate the string representation of your largest possible value. Then, in the model file, just call “enum”, passing it the symbol of the attribute and an array of symbols you'd like to describe as the possible values:

class IceCream < ActiveRecord::Base
  enum :flavor, [:chocolate, :vanilla, :strawberry, :rockyroad]
end

enum_simulator will handle validation of inclusion and conversion of the attribute values from symbols to strings and back internally. Honestly, that's really all it does - it's just such a common pattern that I figured we might as well have a more succinct way of using it.

Copyright © 2011 Centresource, released under the MIT license