Skip to content

An ActiveRecord Enum Like Function Supporting for Mongoid

License

Notifications You must be signed in to change notification settings

berniechiu/mongoid_enum_mapper

Repository files navigation

Gem Version Maintainability Test Coverage Build Status

Mongoid Enum Mapper

Inspired by ActiveRecord::Enum, the library supports a lightweight solution to map enum key value

Installation

Add this line to your application's Gemfile:

gem 'mongoid_enum_mapper'

And then execute:

$ bundle

Or install it yourself as:

$ gem install mongoid_enum_mapper

Usage

# 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.

About

An ActiveRecord Enum Like Function Supporting for Mongoid

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published