Update README.md

better ruby GH markup
[ci skip]
This commit is contained in:
Josef Šimánek 2013-11-12 02:16:54 +01:00
parent 316ba49db4
commit b7166acdbe

View File

@ -16,25 +16,27 @@ and a few other bits-and-bobs.
Add this to your Gemfile: Add this to your Gemfile:
gem "mongoid-enum" ```ruby
gem "mongoid-enum"
```
And then run `bundle install`. And then run `bundle install`.
# Usage # Usage
```` ```ruby
class Payment class Payment
include Mongoid::Document include Mongoid::Document
include Mongoid::Enum include Mongoid::Enum
enum :status, [:pending, :approved, :declined] enum :status, [:pending, :approved, :declined]
end end
```` ```
Aaaaaaand then you get things like: Aaaaaaand then you get things like:
```` ```ruby
payment = Payment.create payment = Payment.create
payment.status payment.status
@ -49,7 +51,7 @@ payment.pending?
Payment.approved Payment.approved
# => Mongoid::Criteria for payments with status == :approved # => Mongoid::Criteria for payments with status == :approved
```` ```
# Features # Features
@ -64,13 +66,13 @@ convenience.
## Accessors ## Accessors
Your enums will get getters-and-setters with the same name. So using the Your enums will get getters-and-setters with the same name. So using the
'Payment' example above: `Payment` example above:
```` ```ruby
payment.status = :declined payment.status = :declined
payment.status payment.status
# => :declined # => :declined
```` ```
And you also get bang(!) and query(?) methods for each of the values in And you also get bang(!) and query(?) methods for each of the values in
your enum (see [this example](#usage). your enum (see [this example](#usage).
@ -82,10 +84,10 @@ 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 help you elsewhere in your app, should you need to display, or leverage
the list of values. Using the above example: the list of values. Using the above example:
```` ```ruby
Payment::STATUS Payment::STATUS
# => [:pending, :approved, :declined] # => [:pending, :approved, :declined]
```` ```
## Validations ## Validations
@ -99,11 +101,11 @@ disable this behaviour (see [below](#options)).
A scope added for each of your enum's values. Using the example above, A scope added for each of your enum's values. Using the example above,
you'd automatically get: you'd automatically get:
```` ```ruby
Payment.pending # => Mongoid::Criteria Payment.pending # => Mongoid::Criteria
Payment.approved # => Mongoid::Criteria Payment.approved # => Mongoid::Criteria
Payment.declined # => Mongoid::Criteria Payment.declined # => Mongoid::Criteria
```` ```
# Options # Options
@ -114,25 +116,29 @@ 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 (`:pending` in the example above). You can override this with the
`:default` option: `:default` option:
enum :roles, [:manager, :administrator], :default => "" ```ruby
enum :roles, [:manager, :administrator], :default => ""
```
## Multiple values ## Multiple values
Sometimes you'll need to store multiple values from your list, this Sometimes you'll need to store multiple values from your list, this
couldn't be easier: couldn't be easier:
enum, :roles => [:basic, :manager, :administrator], :multiple => true ```ruby
enum, :roles => [:basic, :manager, :administrator], :multiple => true
user = User.create # ...
user.roles << :basic
user.roles << :manager
user.save!
user.manager? # => true user = User.create
user.administrator? # => false user.roles << :basic
user.roles # => [:basic, :manager] user.roles << :manager
user.save!
user.manager? # => true
user.administrator? # => false
user.roles # => [:basic, :manager]
```
## Validations ## Validations
@ -141,8 +147,9 @@ 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 complex validations, or you just want to throw caution to the wind, you
can turn them off: can turn them off:
enum :status => [:up, :down], :validate => false ```ruby
enum :status => [:up, :down], :validate => false
```
# Issues and Feature Requests # Issues and Feature Requests