Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/necojackarc/simpler_enum
Providing Rails like enumerated type
https://github.com/necojackarc/simpler_enum
enum rails ruby
Last synced: 2 days ago
JSON representation
Providing Rails like enumerated type
- Host: GitHub
- URL: https://github.com/necojackarc/simpler_enum
- Owner: necojackarc
- License: mit
- Created: 2015-11-10T15:03:11.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2018-02-10T22:22:12.000Z (almost 7 years ago)
- Last Synced: 2024-03-23T01:43:48.676Z (9 months ago)
- Topics: enum, rails, ruby
- Language: Ruby
- Homepage: https://rubygems.org/gems/simpler_enum
- Size: 31.3 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# SimplerEnum
[![Build Status](https://travis-ci.org/necojackarc/simpler_enum.svg?branch=master)](https://travis-ci.org/necojackarc/simpler_enum)
[![Code Climate](https://codeclimate.com/github/necojackarc/simpler_enum/badges/gpa.svg)](https://codeclimate.com/github/necojackarc/simpler_enum)
[![Test Coverage](https://codeclimate.com/github/necojackarc/simpler_enum/badges/coverage.svg)](https://codeclimate.com/github/necojackarc/simpler_enum/coverage)SimplerEnum provides Rails like enumerated type.
It reproduces part of [ActiveRecord::Enum](http://edgeapi.rubyonrails.org/classes/ActiveRecord/Enum.html) features.
I hope it will help you when you want to use [ActiveRecord::Enum](http://edgeapi.rubyonrails.org/classes/ActiveRecord/Enum.html) in your not Rails repositories or older Rails version repositories.
## Supported Ruby versions
- Ruby 1.9
- Ruby 2.x## Installation
Add this line to your application's Gemfile:
```ruby
gem 'simpler_enum'
```And then execute:
$ bundle
Or install it yourself as:
$ gem install simpler_enum
## Usage
Any class can include `SimplerEnum` and you can define an enumerated type like this:
```ruby
require "simpler_enum"class Person
include SimplerEnumsimpler_enum mood: %i(awesome excellent great good fine)
def initialize(mood: :fine)
self.mood = mood
end
end
```Or,
```ruby
require "simpler_enum"class Person
include SimplerEnumsimpler_enum mood: {
awesome: 0,
excellent: 1,
great: 2,
good: 3,
fine: 4
}def initialize(mood: :fine)
self.mood = mood
end
end
```Both behave like this:
```ruby
[1] pry(main)> Person.moods
=> {:awesome=>0, :excellent=>1, :great=>2, :good=>3, :fine=>4}
[2] pry(main)> necojackarc = Person.new(mood: :awesome)
=> #
[3] pry(main)> necojackarc.awesome?
=> true
[4] pry(main)> necojackarc.excellent?
=> false
[5] pry(main)> necojackarc.great!
=> :great
[6] pry(main)> necojackarc.awesome?
=> false
[7] pry(main)> necojackarc.great?
=> true
[8] pry(main)> necojackarc.mood = :fine
=> :fine
[9] pry(main)> necojackarc.great?
=> false
[10] pry(main)> necojackarc.fine?
=> true
[11] pry(main)> necojackarc.mood = 1 # excellent
=> 1
[12] pry(main)> necojackarc.fine?
=> false
[13] pry(main)> necojackarc.excellent?
=> true
[14] pry(main)> necojackarc.mood
=> :excellent
[15] pry(main)> necojackarc.mood = "good"
=> "good"
[16] pry(main)> necojackarc.mood
=> :good
```## License
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).