Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customizable :locked_at and :locked_until fields #55

Merged
merged 1 commit into from
Aug 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 53 additions & 32 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,58 +1,79 @@
# This configuration was generated by `rubocop --auto-gen-config`
# on 2015-10-21 11:03:44 -0400 using RuboCop version 0.29.1.
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2018-08-22 18:00:04 +0300 using RuboCop version 0.58.2.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 1
# Offense count: 6
# Configuration parameters: Include.
# Include: **/*.gemfile, **/Gemfile, **/gems.rb
Bundler/DuplicatedGem:
Exclude:
- 'Gemfile'

# Offense count: 2
Metrics/AbcSize:
Max: 18
Max: 20

# Offense count: 37
# Configuration parameters: AllowURI, URISchemes.
Metrics/LineLength:
Max: 184
# Offense count: 5
# Configuration parameters: CountComments, ExcludedMethods.
# ExcludedMethods: refine
Metrics/BlockLength:
Max: 478

# Offense count: 3
# Offense count: 2
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 25

# Offense count: 6
# Cop supports --auto-correct.
Style/Blocks:
Enabled: false
# Offense count: 1
# Configuration parameters: NamePrefix, NamePrefixBlacklist, NameWhitelist, MethodDefinitionMacros.
# NamePrefix: is_, has_, have_
# NamePrefixBlacklist: is_, has_, have_
# NameWhitelist: is_a?
# MethodDefinitionMacros: define_method, define_singleton_method
Naming/PredicateName:
Exclude:
- 'spec/**/*'
- 'lib/mongoid/locker.rb'

# Offense count: 3
# Offense count: 2
Style/Documentation:
Enabled: false
Exclude:
- 'spec/**/*'
- 'test/**/*'
- 'lib/mongoid/locker.rb'

# Offense count: 2
Style/DoubleNegation:
Enabled: false

# Offense count: 2
# Configuration parameters: Exclude.
Style/FileName:
Enabled: false
Exclude:
- 'lib/mongoid/locker.rb'

# Offense count: 1
# Configuration parameters: NamePrefix, NamePrefixBlacklist.
Style/PredicateName:
Enabled: false
# Configuration parameters: MinBodyLength.
Style/GuardClause:
Exclude:
- 'lib/mongoid/locker.rb'

# Offense count: 1
# Configuration parameters: SupportedStyles.
# Cop supports --auto-correct.
# Configuration parameters: .
# SupportedStyles: compact, exploded
Style/RaiseArgs:
EnforcedStyle: compact

# Offense count: 2
Style/RegexpLiteral:
MaxSlashes: 0

# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: ExactNameMatch, AllowPredicates, AllowDSLWriters, Whitelist.
Style/TrivialAccessors:
Enabled: false
# Configuration parameters: .
# SupportedStyles: percent, brackets
Style/SymbolArray:
EnforcedStyle: percent
MinSize: 3

# Offense count: 63
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
Max: 184
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
## Changelog

### 0.3.7 (Next)
### 1.0.0 (Next)

* Your contribution here.
* [#55](https://github.com/mongoid/mongoid-locker/pull/55): Customizable :locked_at and :locked_until fields - [@dks17](https://github.com/dks17).

### 0.3.6 (4/18/2018)

Expand Down
2 changes: 1 addition & 1 deletion Dangerfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
danger.import_dangerfile(gem: "mongoid-danger")
danger.import_dangerfile(gem: 'mongoid-danger')
8 changes: 5 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ group :development do
end

group :development, :test do
gem 'byebug', platforms: %i[mri mingw x64_mingw]
gem 'mongoid-compatibility'
gem 'mongoid-danger', '~> 0.1.1'
gem 'pry-byebug'
gem 'rack', '~> 1.5'
gem 'rspec', '~> 3.0'
gem 'rake', '11.3.0'
gem 'rubocop', '0.29.1'
gem 'mongoid-danger', '~> 0.1.1'
gem 'rspec', '~> 3.0'
gem 'rubocop'
end
53 changes: 49 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
[![Build Status](https://secure.travis-ci.org/mongoid/mongoid-locker.svg?branch=master)](http://travis-ci.org/mongoid/mongoid-locker)
[![Code Climate](https://codeclimate.com/github/mongoid/mongoid-locker.svg)](https://codeclimate.com/github/mongoid/mongoid-locker)

Document-level locking for MongoDB via Mongoid. The need arose at [Jux](https://jux.com) from multiple processes on multiple servers trying to act upon the same document and stepping on each other's toes. Mongoid-Locker is an easy way to ensure only one process can perform a certain operation on a document at a time.
Document-level locking for MongoDB via Mongoid. The need arose at [Jux](https://jux.com) from multiple processes on multiple servers trying to act upon the same document and stepping on each other's toes. Mongoid-Locker is an easy way to ensure only one process can perform a certain operation on a document at a time.

[Tested](http://travis-ci.org/mongoid/mongoid-locker) against:
- MRI: `2.3.6`, `2.4.3`, `2.5.0`
- MRI: `2.3.6`, `2.4.3`, `2.5.0`
- Mongoid: `2`, `3`, `4`, `5`, `6`, `7`

See [.travis.yml](.travis.yml) for the latest test matrix.
Expand All @@ -19,14 +19,17 @@ Add to your `Gemfile`:
gem 'mongoid-locker'
```

and run `bundle install`. In the model you wish to lock, include `Mongoid::Locker` after `Mongoid::Document`. For example:
and run `bundle install`. In the model you wish to lock, include `Mongoid::Locker` after `Mongoid::Document`. For example:

```ruby
class QueueItem
include Mongoid::Document
include Mongoid::Locker

field :completed_at, :type => Time
field :locked_at, type: Time
field :locked_until, type: Time

field :completed_at, type: Time
end
```

Expand Down Expand Up @@ -57,6 +60,48 @@ Note that these locks are only enforced when using `#with_lock`, not at the data

More in-depth method documentation can be found at [rdoc.info](http://rdoc.info/github/mongoid/mongoid-locker/frames).

### Customizable :locked_at and :locked_until field names
By default, Locker uses fields with `:locked_at` and `:locked_until` names which should be defined in a model.
```ruby
class User
include Mongoid::Document
include Mongoid::Locker

field :locked_at, type: Time
field :locked_until, type: Time
end
```

Use `Mongoid::Locker.configure` to setup field names which used by Locker for all models where it's included.
```ruby
Mongoid::Locker.configure do |config|
config.locked_at_field = :global_locked_at
config.locked_until_field = :global_locked_until
end

class User
include Mongoid::Document
include Mongoid::Locker

field :global_locked_at, type: Time
field :global_locked_until, type: Time
end
```

The `locker` method in your model accepts `:locked_at_field` and `:locked_until_field` options to setup field names which used by Locker for the model. This can be useful when another library uses the same field for different purposes.
```ruby
class User
include Mongoid::Document
include Mongoid::Locker

field :locker_locked_at, type: Time
field :locker_locked_until, type: Time

locker locked_at_field: :locker_locked_at,
locked_until_field: :locker_locked_until
end
```

## Copyright & License

Copyright (c) 2012-2018 Aidan Feldman & Contributors
Expand Down
2 changes: 0 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# encoding: utf-8

require 'rubygems'
require 'bundler/setup'
require 'bundler/gem_tasks'
Expand Down
18 changes: 18 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## Upgrading Mongoid Locker

### Upgrading to 1.0.0
`Mongoid::Locker` no longer defines `locked_at` and `locked_until` fields when included. You must define these fields manually.

```ruby
class User
include Mongoid::Document
include Mongoid::Locker

field :locked_at, type: Time
field :locked_until, type: Time
end
```

You can customize the fields used with a `locker` class method or via a global `configure`. See [Customizable :locked_at and :locked_until field names](https://github.com/mongoid/mongoid-locker#customizable-locked_at-and-locked_until-field-names) for more information.

See [#55](https://github.com/mongoid/mongoid-locker/pull/55) for more information.
Loading