Emulate the handy enum seen in other programming languages
Add this line to your application's Gemfile:
gem 'enumerated_constants'
And then execute:
$ bundle
Or install it yourself as:
$ gem install enumerated_constants
Include EnumeratedConstants
in a module that defines a set of related constants, e.g.
require 'enumerated_constants'
class User
attr_accessor :role
module Role
extend EnumeratedConstants
ADMIN = 'admin'.freeze
MANAGER = 'manager'.freeze
GUEST = 'guest'.freeze
PRIVILEGED = [ADMIN, MANAGER].freeze
end
def validate_role!
unless Role.include?(self.role)
raise "Invalid role, expecting one of #{Role.all.join(', ')}"
end
end
end
Return all constant values in the module that are not Array
s.
User::Role.all
# => ["admin", "manager", "guest"]
Iterate through each constant value returned by all
User::Role.each do |role|
puts role
end
# "admin"
# "manager"
# "guest"
Return all constants values except for the ones passed in. Note, the arguments should be the name of the constants, not the constants' value.
User::Role.except(:guest)
# => ["admin", "manager"]
Return true if the value passed in is a value of one of the constants in the module
User::Role.include?('guest')
# => true
User::Role.include?('hacker')
# => false
Map the values of the module to other values
User::Role.map { |role| "#{role} user" }
# => ["admin user", "manager user", "guest user"]
Randomly select value(s) from the module
User::Role.sample
# => "guest"
User::Role.sample
# => "admin"
User::Role.sample(2)
# => ["manager", "admin"]
Return the sorted constant values
User::Role.sort
# => ["admin", "guest", "manager"]
After checking out the repo, run bin/setup
to install dependencies. Then, run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
to create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
- Fork it ( https://github.com/[my-github-username]/enumerated_constants/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request