Giter VIP home page Giter VIP logo

mongoid-enum's Introduction

Mongoid::Enum

Build Status

Heavily inspired by DHH's ActiveRecord::Enum, this little library is there to help you cut down the cruft in your models and make the world a happier place at the same time.

A single line will get you fields, accessors, validations and scopes, and a few other bits-and-bobs.

Installation

Add this to your Gemfile:

gem "mongoid-enum"

And then run bundle install.

Usage

class Payment
  include Mongoid::Document
  include Mongoid::Enum

  enum :status, [:pending, :approved, :declined]
end

Aaaaaaand then you get things like:

payment = Payment.create

payment.status
# => :pending

payment.approved!
# => :approved

payment.pending?
# => :false

Payment.approved
# => Mongoid::Criteria for payments with status == :approved

Features

Field

Your enum value is stored as either a Symbol, or an Array (when storing multiple values). The actual field name has a leading underscore (e.g.: _status), but is also aliased with its actual name for you convenience.

Accessors

Your enums will get getters-and-setters with the same name. So using the Payment example above:

payment.status = :declined
payment.status
# => :declined

And you also get bang(!) and query(?) methods for each of the values in your enum (see this example.

Constants

For each enum, you'll also get a constant named after it. This is to help you elsewhere in your app, should you need to display, or leverage the list of values. Using the above example:

Payment::STATUS
# => [:pending, :approved, :declined]

Validations

Enum values are automatically validated against the list. You can disable this behaviour (see below).

Scopes

A scope added for each of your enum's values. Using the example above, you'd automatically get:

Payment.pending # => Mongoid::Criteria
Payment.approved # => Mongoid::Criteria
Payment.declined # => Mongoid::Criteria

Options

Default value

If not specified, the default will be the first in your list of values (:pending in the example above). You can override this with the :default option:

enum :roles, [:manager, :administrator], :default => ""

Multiple values

Sometimes you'll need to store multiple values from your list, this couldn't be easier:

enum, :roles => [:basic, :manager, :administrator], :multiple => true

# ...

user = User.create
user.roles << :basic
user.roles << :manager
user.save!

user.manager? # => true
user.administrator? # => false
user.roles # => [:basic, :manager]

Validations

Validations are baked in by default, and ensure that the value(s) set in your field are always from your list of options. If you need more complex validations, or you just want to throw caution to the wind, you can turn them off:

enum :status => [:up, :down], :validate => false

Issues and Feature Requests

If you have any problems, or you have a suggestion, please submit an issue (and a failing test, if you can). Pull requests and feature requests are alwasy welcome and greatly appreciated.

mongoid-enum's People

Contributors

thetron avatar carlson avatar simi avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.