Skip to content

Instantly share code, notes, and snippets.

@rondy
Created October 26, 2011 16:58
Show Gist options
  • Save rondy/1316998 to your computer and use it in GitHub Desktop.
Save rondy/1316998 to your computer and use it in GitHub Desktop.
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