Created
March 20, 2010 17:32
-
-
Save jmorton/338782 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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