项目作者: douglaslise

项目描述 :
Mongoid Enumerable
高级语言: Ruby
项目地址: git://github.com/douglaslise/mongoid_enumerable.git
创建时间: 2018-05-20T00:43:38Z
项目社区:https://github.com/douglaslise/mongoid_enumerable

开源协议:

下载


Mongoid Enumerable

Unit Tests
Lint
Maintainability
Test Coverage
Gem Version

Define enumerable fields in your Mongoid documents.

Installation

Add this line to your application’s Gemfile:

  1. gem 'mongoid_enumerable'

And then execute:

  1. bundle

Or install it yourself as:

  1. gem install mongoid_enumerable

Usage

Simply include MongoidEnumerable in your document.
After add enumerable with:

  • Field name
  • An array with possible values
  • Options (default, prefix and callbacks)

Example:

  1. class Task
  2. include Mongoid::Document
  3. include MongoidEnumerable
  4. enumerable :status, %w[completed running failed waiting], default: "waiting"
  5. end

Now we have methods in this document’s instance:

  1. task = Task.new
  2. task.status # "waiting"
  3. task.waiting? # true
  4. task.running! # changes status field to "running"
  5. task.running? # true
  6. task.waiting? # false

Options

Default

Defines which value is the default for new documents. If not specified the first value is used as default.

  1. enumerable :status, %w[completed running failed waiting], default: "waiting"

Prefix

You can define a prefix for your methods that could be useful if you have more than one enumerable with the same values.

  1. enumerable :build_status,
  2. %w[completed running failed waiting],
  3. default: "waiting",
  4. prefix: "build_"
  5. enumerable :deploy_status,
  6. %w[completed running failed waiting],
  7. default: "waiting",
  8. prefix: "deploy_"
  9. task.build_completed?
  10. task.build_failed!
  11. task.deploy_running?
  12. task.deploy_failed!

Callbacks

Before Change

You can define a before_change callback that runs before each change. If the method returns a falsey value (nil or false) then the change will be aborted.

The method must receive two parameters: the old and the new value, respectively.

Example:

  1. class Task
  2. include Mongoid::Document
  3. include MongoidEnumerable
  4. enumerable :status, %w[completed running failed waiting], default: "waiting", before_change: :can_status_change?
  5. def can_status_change?(old_value, new_value)
  6. new_value != "waiting"
  7. end
  8. end
  9. task = Task.new
  10. task.status # "waiting"
  11. task.running!
  12. task.status # "running"
  13. task.waiting!
  14. task.status # "running"
After Change

You can define an after_change callback that runs after each change. The method must receive two parameters: the old and the new value, respectively.

Example:

  1. class Task
  2. include Mongoid::Document
  3. include MongoidEnumerable
  4. enumerable :status, %w[completed running failed waiting],
  5. default: "waiting",
  6. after_change: :status_changed
  7. def status_changed(old_value, new_value)
  8. puts "Status changed from #{old_value} to #{new_value}."
  9. end
  10. end
  11. task = Task.new
  12. task.running!
  13. # Console output: "Status changed from waiting to running."

Scopes/Criterias

All values are added as scopes/criterias to your document class:

  1. Task.waiting # Returns all tasks with waiting status
  2. Task.running # Returns all tasks with running status

If prefixed, the scopes/criterias are prefixed too:

  1. Task.build_waiting # Returns all tasks with build waiting status
  2. Task.deploy_running # Returns all tasks with deploy running status

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/douglaslise/mongoid_enumerable.

Releasing a New Version

After change version in file lib/mongoid_enumerable/version.rb it is needed only to run this command in terminal:

  1. rake release