Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/burgalon/mongoid_optimistic_locking
Addon for Mongoid to allow optimistic locking (CAS)
https://github.com/burgalon/mongoid_optimistic_locking
Last synced: about 23 hours ago
JSON representation
Addon for Mongoid to allow optimistic locking (CAS)
- Host: GitHub
- URL: https://github.com/burgalon/mongoid_optimistic_locking
- Owner: burgalon
- License: mit
- Created: 2012-01-25T16:28:50.000Z (almost 13 years ago)
- Default Branch: master
- Last Pushed: 2013-04-26T18:23:31.000Z (over 11 years ago)
- Last Synced: 2024-03-15T02:47:13.789Z (8 months ago)
- Language: Ruby
- Homepage:
- Size: 133 KB
- Stars: 41
- Watchers: 5
- Forks: 14
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# mongoid\_optimistic\_locking
This gem helps to abstract the ["Update if Current"](http://www.mongodb.org/display/DOCS/Atomic+Operations#AtomicOperations-%22UpdateifCurrent%22) method which may be used as a replacement for [transactions in Mongo](http://docs.mongodb.org/manual/faq/developers/#how-do-i-do-transactions-and-locking-in-mongodb).
The gem is an addon over [Mongoid ODM](http://mongoid.org/) and is based on [ActiveRecord's Optimistic Locking](http://api.rubyonrails.org/classes/ActiveRecord/Locking/Optimistic.html).
## Compatibility
Works with *Mongoid 3*.
For Mongoid 2 use version 0.0.2
## Rails 3 Installation
Add the gem to your `Gemfile`:
gem 'mongoid_optimistic_locking'
## Usage
To use it, all you have to do is add include `Mongoid::OptimisticLocking`:
class Post
include Mongoid::Document
include Mongoid::OptimisticLocking
field :text
endThis will add a `_lock_version` field in the document which will be incremented every time a save is called.
Be sure to rescue `Mongoid::Errors::StaleDocument` to handle applicative logic in case the object was changed.For example:
class PostController < ApplicationController
## Adds an "UPDATE: ...some text..." to an existing document
def add_update
begin
post = Post.find(params[:id])
post.text += "---UPDATE--- " + params[:more_text]
post.save
rescue Mongoid::Errors::StaleDocument
retry
end
end
endThat's it!
## Embedded Document Caveats
While `Mongoid::OptimisticLocking` can be used to some degree within embedded documents, there are certain limitations due to Mongoid's document embedding callback structure. Consider the following example:
class Post
include Mongoid::Document
field :text
embeds_many :comments
endclass Comment
include Mongoid::Document
include Mongoid::OptimisticLocking
embedded_in :post
field :text
endpost = Post.new
comment = post.comments.build(:text => 'hello')
comment.save # will use optimistic locking checks
post.save # will not use optimistic locking checks## Open sourced by
[Boxee](http://www.boxee.tv)
## References
[Mongo Developer FAQ - How do I do transactions/locking?](http://docs.mongodb.org/manual/faq/developers/#how-do-i-do-transactions-and-locking-in-mongodb)[Mongo Atomic Operations - "Update if Current"](http://www.mongodb.org/display/DOCS/Atomic+Operations#AtomicOperations-%22UpdateifCurrent%22)
[Presentation from "Startup Day" on Mongoid Optimistic Locking](https://speakerdeck.com/u/burgalon/p/mongoid-optimistic-locking)