diff --git a/README.md b/README.md index cf6f48e..473c31b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # Mongoid::Enum -Heavily inspired by DHH's ActiveRecord::Enum, this little library is +Heavily inspired by [DHH's +ActiveRecord::Enum](https://github.com/rails/rails/commit/db41eb8a6ea88b854bf5cd11070ea4245e1639c5), this little library is just there to help you cut down the cruft in your models and make the world a happier place at the same time. @@ -28,6 +29,79 @@ class Payment 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](#Usage). + + +## 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)[#Options]). + + +## 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