Skip to content

virtualstaticvoid/ez_enum

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EZEnum

Gem Version

Provides a simple abstraction for defining enumerations in a Ruby module. Supports listing members and localization of constant names.

Installation

Add this line to your application's Gemfile:

gem 'ez_enum'

And then execute:

$ bundle

Or install it yourself as:

$ gem install ez_enum

Usage

Define a Ruby module, giving it the name of the desired enumeration. Add constants to the module as needed. Include the EZEnum module into your module after your constant declarations. E.g.

module Status

  New = 1
  Processing = 2
  Failed = 3
  Complete = 4

  # included here, so that constants are used
  include EZEnum

end

Your module will now include an All constant, which contains an array of the constants defined in the module. E.g.

Status::All # => [New, Processing, Failed, Complete]

It will also have the following helper methods added to it:

  • choices_for_select - returns an array of the localized names and values for each constant. E.g.

    Status.choices_for_select # => [['New', 1], ['Processing', 2], ...]
  • display_for(value) - returns the localized name for the given constant value. E.g.

    Status.display_for(Status::Failed) # => 'Epic Fail'

To add support for localization, add sections under the respective language code in your locales file; using the lowercase name of the module and the lowercase name of the constant, provide the corresponding translation. E.g.

en:
  enums:
    status:
      new: New
      processing: Processing
      failed: Epic Fail
      completed: Completed

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Copyright

MIT License - Copyright (c) 2013 Chris Stefano

About

Elegant Enumerations for Ruby and Rails

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages