Created
October 26, 2011 16:58
-
-
Save rondy/1316998 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
module HasStatuses | |
def has_statuses(*statuses) | |
self.const_set "Statuses", Module.new | |
statuses.each { |status| self::Statuses.const_set status.to_s.upcase, status } | |
self.const_set "ALL_STATUSES", self::Statuses.constants.collect { |constant| self::Statuses.const_get(constant) } | |
end | |
Object.send :extend, self | |
end | |
class Task | |
has_statuses :new, :in_dev, :done | |
end | |
puts Task::Statuses.constants.inspect # => [:NEW, :IN_DEV, :DONE] | |
puts Task::Statuses::NEW # => :new | |
puts Task::Statuses::IN_DEV # => :in_dev | |
puts Task::Statuses::DONE # => :done | |
puts Task::ALL_STATUSES.inspect # => ["new", "in_dev", "done"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment