Skip to content

Instantly share code, notes, and snippets.

@jmorton
Created March 20, 2010 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmorton/338782 to your computer and use it in GitHub Desktop.
Save jmorton/338782 to your computer and use it in GitHub Desktop.
class Task < ActiveRecord::Base
include Encoder
code :status do
Status::New = "N"
Status::Pending = "P"
Status::Finished = "F"
Status::OverDue = "O"
end
code :priority do
Priority::High = 1
Priority::Low = 5
end
end
t = Task.new # => #<Task id: nil, status: nil, priority: nil>
# Using a constant to assign the value
t.status = Task::Status::New # => "N"
t.status # => "N"
t.status.decode # => "New"
# Using a decoded value to assign the value
t.priority = 'high' # => "high"
t.priority # => "H"
t.priority.decode # => "High"
# Obtaining a list of the constant names defined for each attribute
Task::Status.constants # => ["Pending", "New", "OverDue", "Finished"]
Task::Priority.constants # => ["Low", "High"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment