Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/marcoroth/easy_enum
Turn any Ruby class in an easy to use enum.
https://github.com/marcoroth/easy_enum
enum enums gem ruby
Last synced: 10 days ago
JSON representation
Turn any Ruby class in an easy to use enum.
- Host: GitHub
- URL: https://github.com/marcoroth/easy_enum
- Owner: marcoroth
- License: mit
- Created: 2019-05-02T00:04:26.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-08-22T23:15:03.000Z (3 months ago)
- Last Synced: 2024-10-23T14:00:03.676Z (19 days ago)
- Topics: enum, enums, gem, ruby
- Language: Ruby
- Homepage: https://github.com/marcoroth/easy_enum
- Size: 86.9 KB
- Stars: 13
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# EasyEnum
Turn any Ruby class in an easy to use enum.
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'easy_enum'
```And then execute:
$ bundle
Or install it yourself as:
$ gem install easy_enum
## Basic Usage
```ruby
class Color < EasyEnum::EasyEnum
easy_enum(green: 0, red: 1, blue: 2)
end# or
class Color < EasyEnum::EasyEnum
self.easy_enum = {
green: 0,
red: 1,
blue: 2
}
end```
### Getting an Instance
```ruby
Color.green
Color.red
Color.blueColor.new(0)
Color.new(1)
Color.new(2)Color.key(:green)
Color.key(:red)
Color.key(:blue)Color.value(0)
Color.value(1)
Color.value(2)```
### Getting Values
```ruby
green = Color.green
green.value
# => 0red = Color.red
red.value
# => 1blue = Color.blue
blue.value
# => 2```
### Check the value of your instance
```ruby
color = Color.greencolor.green? # => true
color.red? # => false
color.blue? # => false```
### Exploring the Enum
```ruby
Color.members
# => {:green=>0, :red=>1, :blue=>2}Color.keys
# => [:green, :red, :blue]Color.values
# => [0, 1, 2]Color.key?(:green)
# => trueColor.value?(0)
# => true```
### Comparison
```ruby
green = Color.greengreen == :green # => true
green == 0 # => true
green == Color.green # => true
green == Color.key(:green) # => true
green == Color.value(0) # => truegreen == Color.red # => false
```
### Redefining the value of your instance
```ruby
color = Color.green
color.value # => 0
color.key # => :greencolor.value = 1
color.value # => 1
color.key # => :redcolor.key = :blue
color.value # => 2
color.key # => :bluecolor.key = :yellow
# => raises EasyEnum::KeyNotInEnumcolor.value = 3
# => raises EasyEnum::ValueNotInEnum```
## Development
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also 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`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/marcoroth/easy_enum.
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).