0.0
Low commit activity in last 3 years
A long-lived project that still receives updates
Sligntly extension of builtin enum.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

>= 6.0.0
 Project Readme

BetterEnum

Sligtly extension of builtin enum.

Usage

Enum definication

The examples below is to be under following model with enum.

class Employee < ApplicationRecord
  enum gender: {male: 0, female: 1, other: 2}
end

Translation definication

Define translation definication under activerecord > enums as follows:

en:
  activerecord:
    enums:
      employee:
        gender:
          male: 'Male'
          female: 'Female'
          other: 'Other'

Singleton methods

You can get specified translated label and specified value, and also get set of key and translated label pairs.

Employee.gender_label(:male) # => "Male"
Employee.gender_value(:female) # => 1
Employee.gender_labels # => {"male" => "Male", "female" => "Female", "other" => "Other"}

Instance methods

You can get translated label and value.

female_employee.gender_label # => "Female"
other_employee.gender_value # => 2

Installation

Add this line to your application's Gemfile:

gem 'better_enum'

And then execute:

$ bundle

Or install it yourself as:

$ gem install better_enum

Contributing

Bug reports and pull requests are accepted on GitHub at https://github.com/smoriwaki/better_enum.

License

The gem is available as open source under the terms of the MIT License.