Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.