https://github.com/berniechiu/mongoid_enum_mapper
An ActiveRecord Enum Like Function Supporting for Mongoid
https://github.com/berniechiu/mongoid_enum_mapper
enum mongoid ruby
Last synced: about 1 year ago
JSON representation
An ActiveRecord Enum Like Function Supporting for Mongoid
- Host: GitHub
- URL: https://github.com/berniechiu/mongoid_enum_mapper
- Owner: berniechiu
- License: mit
- Created: 2017-10-02T06:19:15.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-03-05T13:58:51.000Z (over 8 years ago)
- Last Synced: 2025-03-23T11:03:16.218Z (about 1 year ago)
- Topics: enum, mongoid, ruby
- Language: Ruby
- Size: 43 KB
- Stars: 2
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
[](https://badge.fury.io/rb/mongoid_enum_mapper)
[](https://codeclimate.com/github/berniechiu/mongoid_enum_mapper/maintainability)
[](https://codeclimate.com/github/berniechiu/mongoid_enum_mapper/test_coverage)
[](https://travis-ci.org/berniechiu/mongoid_enum_mapper)
# Mongoid Enum Mapper
Inspired by [ActiveRecord::Enum](http://api.rubyonrails.org/v5.1/classes/ActiveRecord/Enum.html), the library supports a lightweight solution to map enum key value
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'mongoid_enum_mapper'
```
And then execute:
$ bundle
Or install it yourself as:
$ gem install mongoid_enum_mapper
## Usage
```ruby
# Allow Mongoid to have ActiveRecord like Enum type of Mapping
#=> OrderDelivery.rb Model
include Mongoid::EnumMapper
define_enum :status, { pending: 0, shipping: 1, shipped: 2 }
# Examples
od = OrderDelivery.new
od.status #=> Default :pending
od.status = :shipping #=> :shipping
od.status #=> :shipping
od[:status] #=> 1
od.status = :invalid #=> :invalid
od.status #=> :shipping
od.update(status: :pending)
od.status #=> :pending
OrderDelivery::STATUS #=> { pending: 0, shipping: 1, shipped: 2 }
OrderDelivery.update_all(OrderDelivery::STATUS[:shipping])
```
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/berniechiu/mongoid_enum_mapper.
## License
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).