Project

ez_enum

0.0
No commit activity in last 3 years
No release in over 3 years
Provides a simple abstraction for defining enumerations in a Ruby module. Supports listing members and localization of constant names.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.3
>= 0
>= 0
>= 0

Runtime

 Project Readme

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