Code Monkey home page Code Monkey logo

ar_lock's Introduction

ArLock for Rails3

<img src=“https://codeclimate.com/github/InWork/ar_lock.png” />

This Rails3 Gem implements an atomic locking model based on ActiveRecord.

Install

Inside your Gemfile:

gem "ar_lock"

and then run:

  • bundle install

Inside your Application:

Geting a lock is always an atomic operation. If you try to get more than one lock (by specifying an array), this is also executed atomic.

Get lock(s):

Get one lock:

Lock.get :lock_name

Get multiple locks as one atomic operation:

Lock.get [:lock_name1, :lock_name2]

optional you can specify a value (for example the user, which acquired the lock):

Lock.get :lock_name, value: 'value'

You can let Lock.get block, until the lock was acquired:

Lock.get :lock_name, blocking: true

If you do not want a lock which was already acquired will block your code:

Lock.get :lock_name, blocking: true, successful_if: :value_matches

It is possible to force a lock. Values of already acquired locks will be overwriten by the given value.

Lock.get :lock_name, value: 'value', force: true

Release lock(s):

Release one Lock:

Lock.release :lock_name

Release multiple locks:

Lock.release [:lock_name1, :lock_name2]

Only release a lock, if the value matches:

Lock.release :lock_name, value: 'value'

You can also release all locks at once:

Lock.release_all

Get the value of a lock:

To read the value of a lock:

Lock.get_value :lock_name

Generators

  • rails generate ar_lock:migration

Database Setup

Use

rails g ar_lock:migration

This will create a database migration:

TIMESTAMP_add_ar_lock_table.rb

License

This project is licenced under the MIT license.

Author

Philip Kurmann (philip (at) kman.ch)

ar_lock's People

Contributors

philkman avatar lstuker avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar James Cloos avatar Damiano Radice (damo) 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.