mongoid-locking 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +29 -0
- data/.tool-versions +1 -0
- data/CHANGELOG.md +7 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +84 -0
- data/LICENSE.txt +21 -0
- data/README.md +57 -0
- data/Rakefile +12 -0
- data/gemfiles/mongoid6.gemfile +6 -0
- data/gemfiles/mongoid7.gemfile +6 -0
- data/lib/mongoid/locking/contextual/atomic.rb +183 -0
- data/lib/mongoid/locking/contextual/mongo.rb +31 -0
- data/lib/mongoid/locking/persistable/creatable.rb +18 -0
- data/lib/mongoid/locking/persistable/updatable.rb +27 -0
- data/lib/mongoid/locking/persistable.rb +53 -0
- data/lib/mongoid/locking/selectable.rb +18 -0
- data/lib/mongoid/locking/version.rb +7 -0
- data/lib/mongoid/locking.rb +32 -0
- data/lib/mongoid/stale_object_error.rb +4 -0
- data/sig/mongoid/locking.rbs +6 -0
- metadata +153 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c503286097299824c534beaed6886b61ee0b314dae7a362a2b556d142074dd54
|
4
|
+
data.tar.gz: 80ef23fc4832dc9610f4c1723deea805904d0085961caf6b08eb8f0b721a0d28
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cf84bddf69cfa2d9ba33c6b1ca2bc71360da3e91b7f8fd4c80878477eafa93153081f63ba523f93d1e7981eb96d54a083262b2bb3bf2a842a31f38a8308e94a1
|
7
|
+
data.tar.gz: ca61600a4d453867b75829b4b7a72d6cd9c8f952ab26ead8a386459b7e3eda20bd31bd01b9c7b0c5595b3d30ed293b7c9ee50e76cf8963d5065d21880b7a9278
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require:
|
2
|
+
- rubocop-rspec
|
3
|
+
|
4
|
+
AllCops:
|
5
|
+
TargetRubyVersion: 2.6
|
6
|
+
NewCops: disable
|
7
|
+
|
8
|
+
Style/StringLiterals:
|
9
|
+
Enabled: true
|
10
|
+
EnforcedStyle: double_quotes
|
11
|
+
|
12
|
+
Style/StringLiteralsInInterpolation:
|
13
|
+
Enabled: true
|
14
|
+
EnforcedStyle: double_quotes
|
15
|
+
|
16
|
+
Style/FrozenStringLiteralComment:
|
17
|
+
Enabled: false
|
18
|
+
|
19
|
+
Style/EachWithObject:
|
20
|
+
Enabled: false
|
21
|
+
|
22
|
+
Layout/LineLength:
|
23
|
+
Max: 120
|
24
|
+
|
25
|
+
Metrics/BlockLength:
|
26
|
+
Enabled: False
|
27
|
+
|
28
|
+
RSpec/DescribeClass:
|
29
|
+
Enabled: false
|
data/.tool-versions
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby 2.6.7
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at rodrigo.aquino@fullhealthmedical.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
mongoid-locking (0.1.0)
|
5
|
+
mongoid (> 6.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (5.2.8.1)
|
11
|
+
activesupport (= 5.2.8.1)
|
12
|
+
activesupport (5.2.8.1)
|
13
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
14
|
+
i18n (>= 0.7, < 2)
|
15
|
+
minitest (~> 5.1)
|
16
|
+
tzinfo (~> 1.1)
|
17
|
+
ast (2.4.2)
|
18
|
+
bson (4.15.0)
|
19
|
+
byebug (11.1.3)
|
20
|
+
concurrent-ruby (1.1.10)
|
21
|
+
diff-lcs (1.5.0)
|
22
|
+
i18n (1.12.0)
|
23
|
+
concurrent-ruby (~> 1.0)
|
24
|
+
json (2.6.2)
|
25
|
+
minitest (5.16.3)
|
26
|
+
mongo (2.18.1)
|
27
|
+
bson (>= 4.14.1, < 5.0.0)
|
28
|
+
mongoid (6.4.8)
|
29
|
+
activemodel (>= 5.1, < 6.0.0)
|
30
|
+
mongo (>= 2.5.1, < 3.0.0)
|
31
|
+
parallel (1.22.1)
|
32
|
+
parser (3.1.2.1)
|
33
|
+
ast (~> 2.4.1)
|
34
|
+
rainbow (3.1.1)
|
35
|
+
rake (13.0.6)
|
36
|
+
regexp_parser (2.5.0)
|
37
|
+
rexml (3.2.5)
|
38
|
+
rspec (3.11.0)
|
39
|
+
rspec-core (~> 3.11.0)
|
40
|
+
rspec-expectations (~> 3.11.0)
|
41
|
+
rspec-mocks (~> 3.11.0)
|
42
|
+
rspec-core (3.11.0)
|
43
|
+
rspec-support (~> 3.11.0)
|
44
|
+
rspec-expectations (3.11.0)
|
45
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
46
|
+
rspec-support (~> 3.11.0)
|
47
|
+
rspec-mocks (3.11.1)
|
48
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
49
|
+
rspec-support (~> 3.11.0)
|
50
|
+
rspec-support (3.11.0)
|
51
|
+
rubocop (1.36.0)
|
52
|
+
json (~> 2.3)
|
53
|
+
parallel (~> 1.10)
|
54
|
+
parser (>= 3.1.2.1)
|
55
|
+
rainbow (>= 2.2.2, < 4.0)
|
56
|
+
regexp_parser (>= 1.8, < 3.0)
|
57
|
+
rexml (>= 3.2.5, < 4.0)
|
58
|
+
rubocop-ast (>= 1.20.1, < 2.0)
|
59
|
+
ruby-progressbar (~> 1.7)
|
60
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
61
|
+
rubocop-ast (1.21.0)
|
62
|
+
parser (>= 3.1.1.0)
|
63
|
+
rubocop-rspec (2.12.1)
|
64
|
+
rubocop (~> 1.31)
|
65
|
+
ruby-progressbar (1.11.0)
|
66
|
+
thread_safe (0.3.6)
|
67
|
+
tzinfo (1.2.10)
|
68
|
+
thread_safe (~> 0.1)
|
69
|
+
unicode-display_width (2.2.0)
|
70
|
+
|
71
|
+
PLATFORMS
|
72
|
+
arm64-darwin-21
|
73
|
+
|
74
|
+
DEPENDENCIES
|
75
|
+
byebug
|
76
|
+
mongoid (~> 6.0)
|
77
|
+
mongoid-locking!
|
78
|
+
rake (~> 13.0)
|
79
|
+
rspec
|
80
|
+
rubocop
|
81
|
+
rubocop-rspec
|
82
|
+
|
83
|
+
BUNDLED WITH
|
84
|
+
2.3.14
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 Rodrigo RA
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# Mongoid::Locking
|
2
|
+
|
3
|
+
It adds Optimistic Locking to `Mongoid::Document` objects.
|
4
|
+
|
5
|
+
This gem is inspired in the [ActiveRecord::Locking](https://api.rubyonrails.org/v7.0.3.1/classes/ActiveRecord/Locking/Optimistic.html)
|
6
|
+
module.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Install the gem and add to the application's Gemfile by executing:
|
11
|
+
|
12
|
+
$ bundle add mongoid-locking
|
13
|
+
|
14
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
15
|
+
|
16
|
+
$ gem install mongoid-locking
|
17
|
+
|
18
|
+
## Usage
|
19
|
+
|
20
|
+
- Include `Mongoid::Locking` module
|
21
|
+
|
22
|
+
```
|
23
|
+
class Order
|
24
|
+
include Mongoid::Document
|
25
|
+
include Mongoid::Locking
|
26
|
+
end
|
27
|
+
```
|
28
|
+
|
29
|
+
- Handle `Mongoid::StaleObjectError` when performing updates
|
30
|
+
|
31
|
+
```
|
32
|
+
# ...
|
33
|
+
def update_order
|
34
|
+
order.update(attributes)
|
35
|
+
rescue Mongoid::StaleObjectError => e
|
36
|
+
add_error("This order has been changed ...")
|
37
|
+
end
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
## Development
|
42
|
+
|
43
|
+
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.
|
44
|
+
|
45
|
+
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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
46
|
+
|
47
|
+
## Contributing
|
48
|
+
|
49
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/fullhealthmedical/fhweb/mongoid-locking. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/fullhealthmedical/fhweb/mongoid-locking/blob/master/CODE_OF_CONDUCT.md).
|
50
|
+
|
51
|
+
## License
|
52
|
+
|
53
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
54
|
+
|
55
|
+
## Code of Conduct
|
56
|
+
|
57
|
+
Everyone interacting in the Mongoid::Locking project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/fullhealthmedical/fhweb/mongoid-locking/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,183 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Locking
|
3
|
+
module Contextual
|
4
|
+
# :nodoc:
|
5
|
+
module Atomic
|
6
|
+
# Execute an atomic $addToSet on the matching documents.
|
7
|
+
#
|
8
|
+
# @example Add the value to the set.
|
9
|
+
# context.add_to_set(members: "Dave", genres: "Electro")
|
10
|
+
#
|
11
|
+
# @param [ Hash ] adds The operations.
|
12
|
+
#
|
13
|
+
# @return [ nil ] Nil.
|
14
|
+
def add_to_set(adds)
|
15
|
+
_update_many_with_locking("$addToSet" => collect_operations(adds))
|
16
|
+
end
|
17
|
+
|
18
|
+
# Perform an atomic $addToSet/$each on the matching documents.
|
19
|
+
#
|
20
|
+
# @example Add the value to the set.
|
21
|
+
# context.add_each_to_set(members: ["Dave", "Bill"], genres: ["Electro", "Disco"])
|
22
|
+
#
|
23
|
+
# @param [ Hash ] adds The operations.
|
24
|
+
#
|
25
|
+
# @return [ nil ] Nil.
|
26
|
+
def add_each_to_set(adds)
|
27
|
+
_update_many_with_locking("$addToSet" => collect_each_operations(adds))
|
28
|
+
end
|
29
|
+
|
30
|
+
# Perform an atomic $bit operation on the matching documents.
|
31
|
+
#
|
32
|
+
# @example Perform the bitwise op.
|
33
|
+
# context.bit(likes: { and: 14, or: 4 })
|
34
|
+
#
|
35
|
+
# @param [ Hash ] bits The operations.
|
36
|
+
#
|
37
|
+
# @return [ nil ] Nil.
|
38
|
+
def bit(bits)
|
39
|
+
_update_many_with_locking("$bit" => collect_operations(bits))
|
40
|
+
end
|
41
|
+
|
42
|
+
# Perform an atomic $inc operation on the matching documents.
|
43
|
+
#
|
44
|
+
# @example Perform the atomic increment.
|
45
|
+
# context.inc(likes: 10)
|
46
|
+
#
|
47
|
+
# @param [ Hash ] incs The operations.
|
48
|
+
#
|
49
|
+
# @return [ nil ] Nil.
|
50
|
+
def inc(incs)
|
51
|
+
_update_many_with_locking("$inc" => collect_operations(incs))
|
52
|
+
end
|
53
|
+
|
54
|
+
# Perform an atomic $pop operation on the matching documents.
|
55
|
+
#
|
56
|
+
# @example Pop the first value on the matches.
|
57
|
+
# context.pop(members: -1)
|
58
|
+
#
|
59
|
+
# @example Pop the last value on the matches.
|
60
|
+
# context.pop(members: 1)
|
61
|
+
#
|
62
|
+
# @param [ Hash ] pops The operations.
|
63
|
+
#
|
64
|
+
# @return [ nil ] Nil.
|
65
|
+
def pop(pops)
|
66
|
+
_update_many_with_locking("$pop" => collect_operations(pops))
|
67
|
+
end
|
68
|
+
|
69
|
+
# Perform an atomic $pull operation on the matching documents.
|
70
|
+
#
|
71
|
+
# @example Pull the value from the matches.
|
72
|
+
# context.pull(members: "Dave")
|
73
|
+
#
|
74
|
+
# @note Expression pulling is not yet supported.
|
75
|
+
#
|
76
|
+
# @param [ Hash ] pulls The operations.
|
77
|
+
#
|
78
|
+
# @return [ nil ] Nil.
|
79
|
+
def pull(pulls)
|
80
|
+
_update_many_with_locking("$pull" => collect_operations(pulls))
|
81
|
+
end
|
82
|
+
|
83
|
+
# Perform an atomic $pullAll operation on the matching documents.
|
84
|
+
#
|
85
|
+
# @example Pull all the matching values from the matches.
|
86
|
+
# context.pull_all(:members, [ "Alan", "Vince" ])
|
87
|
+
#
|
88
|
+
# @param [ Hash ] pulls The operations.
|
89
|
+
#
|
90
|
+
# @return [ nil ] Nil.
|
91
|
+
def pull_all(pulls)
|
92
|
+
_update_many_with_locking("$pullAll" => collect_operations(pulls))
|
93
|
+
end
|
94
|
+
|
95
|
+
# Perform an atomic $push operation on the matching documents.
|
96
|
+
#
|
97
|
+
# @example Push the value to the matching docs.
|
98
|
+
# context.push(members: "Alan")
|
99
|
+
#
|
100
|
+
# @param [ Hash ] pushes The operations.
|
101
|
+
#
|
102
|
+
# @return [ nil ] Nil.
|
103
|
+
def push(pushes)
|
104
|
+
_update_many_with_locking("$push" => collect_operations(pushes))
|
105
|
+
end
|
106
|
+
|
107
|
+
# Perform an atomic $push/$each operation on the matching documents.
|
108
|
+
#
|
109
|
+
# @example Push the values to the matching docs.
|
110
|
+
# context.push_all(members: [ "Alan", "Fletch" ])
|
111
|
+
#
|
112
|
+
# @param [ Hash ] pushes The operations.
|
113
|
+
#
|
114
|
+
# @return [ nil ] Nil.
|
115
|
+
def push_all(pushes)
|
116
|
+
_update_many_with_locking("$push" => collect_each_operations(pushes))
|
117
|
+
end
|
118
|
+
|
119
|
+
# Perform an atomic $rename of fields on the matching documents.
|
120
|
+
#
|
121
|
+
# @example Rename the fields on the matching documents.
|
122
|
+
# context.rename(members: :artists)
|
123
|
+
#
|
124
|
+
# @param [ Hash ] renames The operations.
|
125
|
+
#
|
126
|
+
# @return [ nil ] Nil.
|
127
|
+
def rename(renames)
|
128
|
+
operations = renames.inject({}) do |ops, (old_name, new_name)|
|
129
|
+
ops[old_name] = new_name.to_s
|
130
|
+
ops
|
131
|
+
end
|
132
|
+
_update_many_with_locking("$rename" => collect_operations(operations))
|
133
|
+
end
|
134
|
+
|
135
|
+
# Perform an atomic $set of fields on the matching documents.
|
136
|
+
#
|
137
|
+
# @example Set the field value on the matches.
|
138
|
+
# context.set(name: "Depeche Mode")
|
139
|
+
#
|
140
|
+
# @param [ Hash ] sets The operations.
|
141
|
+
#
|
142
|
+
# @return [ nil ] Nil.
|
143
|
+
#
|
144
|
+
# @since 3.0.0
|
145
|
+
def set(sets)
|
146
|
+
_update_many_with_locking("$set" => collect_operations(sets))
|
147
|
+
end
|
148
|
+
|
149
|
+
# Perform an atomic $unset of a field on the matching documents.
|
150
|
+
#
|
151
|
+
# @example Unset the field on the matches.
|
152
|
+
# context.unset(:name)
|
153
|
+
#
|
154
|
+
# @param [ String | Symbol | Array<String|Symbol> | Hash ] args
|
155
|
+
# The name(s) of the field(s) to unset.
|
156
|
+
# If a Hash is specified, its keys will be used irrespective of what
|
157
|
+
# each key's value is, even if the value is nil or false.
|
158
|
+
#
|
159
|
+
# @return [ nil ] Nil.
|
160
|
+
def unset(*args)
|
161
|
+
fields = args.map { |a| a.is_a?(Hash) ? a.keys : a }
|
162
|
+
.__find_args__
|
163
|
+
.map { |f| [database_field_name(f), true] }
|
164
|
+
view.update_many("$unset" => Hash[fields])
|
165
|
+
end
|
166
|
+
|
167
|
+
private
|
168
|
+
|
169
|
+
def _update_many_with_locking(operations)
|
170
|
+
if _locking?
|
171
|
+
view.update_many(operations.merge("$inc" => { "lock_version" => 1 }))
|
172
|
+
else
|
173
|
+
view.update_many(operations)
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
def _locking?
|
178
|
+
@klass.included_modules.include?(Mongoid::Locking)
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Locking
|
3
|
+
module Contextual
|
4
|
+
# :nodoc:
|
5
|
+
module Mongo
|
6
|
+
# Update the documents for the provided method.
|
7
|
+
#
|
8
|
+
# @api private
|
9
|
+
#
|
10
|
+
# @example Update the documents.
|
11
|
+
# context.update_documents(attrs)
|
12
|
+
#
|
13
|
+
# @param [ Hash ] attributes The updates.
|
14
|
+
# @param [ Symbol ] method The method to use.
|
15
|
+
#
|
16
|
+
# @return [ true, false ] If the update succeeded.
|
17
|
+
def update_documents(attributes, method = :update_one, opts = {})
|
18
|
+
return false unless attributes
|
19
|
+
|
20
|
+
attributes = Hash[attributes.transform_keys { |k| klass.database_field_name(k.to_s) }]
|
21
|
+
updates = attributes.__consolidate__(klass)
|
22
|
+
if _locking?
|
23
|
+
updates["$inc"] ||= {}
|
24
|
+
updates["$inc"]["lock_version"] = 1
|
25
|
+
end
|
26
|
+
view.send(method, updates, opts)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Locking
|
3
|
+
module Persistable
|
4
|
+
module Creatable # :nodoc:
|
5
|
+
def insert_as_embedded
|
6
|
+
raise Errors::NoParent, self.class.name unless _parent
|
7
|
+
|
8
|
+
if _parent.new_record?
|
9
|
+
_parent.insert
|
10
|
+
else
|
11
|
+
selector = _parent.atomic_selector
|
12
|
+
_update_one_locked(_root.collection, selector, positionally(selector, atomic_inserts))
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Locking
|
3
|
+
module Persistable
|
4
|
+
module Updatable # :nodoc:
|
5
|
+
# Overrides the default Mongoid::Persistable::Updatable#update_document
|
6
|
+
# to:
|
7
|
+
# - include the lock_version in the atomic selector
|
8
|
+
# - increment the lock_version in the atomic updates
|
9
|
+
def update_document(options = {})
|
10
|
+
prepare_update(options) do
|
11
|
+
updates, conflicts = init_atomic_updates
|
12
|
+
return if updates.empty?
|
13
|
+
|
14
|
+
coll = collection(_root)
|
15
|
+
selector = atomic_selector
|
16
|
+
|
17
|
+
_update_one_locked(coll, selector, updates)
|
18
|
+
|
19
|
+
conflicts.each_pair do |key, value|
|
20
|
+
coll.find(selector).update_one(positionally(selector, { key => value }), session: _session)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Locking
|
3
|
+
module Persistable # :nodoc:
|
4
|
+
include ::Mongoid::Locking::Persistable::Creatable
|
5
|
+
include ::Mongoid::Locking::Persistable::Updatable
|
6
|
+
|
7
|
+
# Overrides the default Mongoid::Persistable#persist_atomic_operations
|
8
|
+
# to:
|
9
|
+
# - include the lock_version in the atomic selector
|
10
|
+
# - increment the lock_version in the atomic updates
|
11
|
+
def persist_atomic_operations(operations)
|
12
|
+
return unless persisted? && operations.present?
|
13
|
+
|
14
|
+
selector = atomic_selector
|
15
|
+
_update_one_locked(_root.collection, selector, operations)
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def _update_one_locked(collection, selector, updates)
|
21
|
+
if _locking?
|
22
|
+
updates["$inc"] ||= {}
|
23
|
+
updates["$inc"]["lock_version"] = 1
|
24
|
+
end
|
25
|
+
|
26
|
+
result = collection.find(selector).update_one(positionally(selector, updates), session: _session)
|
27
|
+
|
28
|
+
if _locking?
|
29
|
+
_assert_updated(result)
|
30
|
+
_increase_lock_version
|
31
|
+
end
|
32
|
+
|
33
|
+
result
|
34
|
+
end
|
35
|
+
|
36
|
+
def _assert_updated(result)
|
37
|
+
raise Mongoid::StaleObjectError, self.class.name.try(:downcase) if result.n < 1
|
38
|
+
end
|
39
|
+
|
40
|
+
def _increase_lock_version
|
41
|
+
doc = embedded? ? _root : self
|
42
|
+
|
43
|
+
doc.lock_version = (doc.lock_version || 0) + 1
|
44
|
+
end
|
45
|
+
|
46
|
+
def _locking?
|
47
|
+
klass = embedded? ? _root.class : self.class
|
48
|
+
|
49
|
+
klass.included_modules.include?(::Mongoid::Locking)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Locking
|
3
|
+
module Selectable # :nodoc:
|
4
|
+
# Overrides the default Mongoid::Selectable#atomic_selector to not memoize
|
5
|
+
# its result.
|
6
|
+
#
|
7
|
+
# It is required when saving the same object multiple times in a row.
|
8
|
+
def atomic_selector
|
9
|
+
embedded? ? embedded_atomic_selector : root_atomic_selector
|
10
|
+
end
|
11
|
+
|
12
|
+
# https://github.com/mongodb/mongoid/blob/e03120a56894bc773dcf1e51209eb2f3e6f2b61f/lib/mongoid/selectable.rb#L55
|
13
|
+
def root_atomic_selector
|
14
|
+
{ "_id" => id, "lock_version" => lock_version }
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require "mongoid"
|
2
|
+
require_relative "locking/version"
|
3
|
+
require_relative "stale_object_error"
|
4
|
+
require_relative "locking/contextual/atomic"
|
5
|
+
require_relative "locking/contextual/mongo"
|
6
|
+
require_relative "locking/selectable"
|
7
|
+
require_relative "locking/persistable/creatable"
|
8
|
+
require_relative "locking/persistable/updatable"
|
9
|
+
require_relative "locking/persistable"
|
10
|
+
|
11
|
+
module Mongoid
|
12
|
+
##
|
13
|
+
# Adds optimistic locking to a Mongoid::Document class.
|
14
|
+
#
|
15
|
+
# @since 0.1.0
|
16
|
+
module Locking
|
17
|
+
def self.included(base)
|
18
|
+
base.field :lock_version, type: Integer
|
19
|
+
base.before_create { self.lock_version = 0 }
|
20
|
+
|
21
|
+
base.include Mongoid::Locking::Selectable
|
22
|
+
# base.include Mongoid::Locking::Reloadable if Mongoid::VERSION >= "7"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# monkey patching for collection updates
|
28
|
+
Mongoid::Contextual::Mongo.prepend Mongoid::Locking::Contextual::Mongo
|
29
|
+
Mongoid::Contextual::Mongo.prepend Mongoid::Locking::Contextual::Atomic
|
30
|
+
|
31
|
+
# monkey patching for document updates
|
32
|
+
Mongoid::Persistable.prepend Mongoid::Locking::Persistable
|
metadata
ADDED
@@ -0,0 +1,153 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid-locking
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rodrigo RA
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-09-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: mongoid
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '6.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '6.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: byebug
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '13.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '13.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop-rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: Implements optimistic locking for Mongoid objects.
|
98
|
+
email:
|
99
|
+
- rodrigorcomp@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".rspec"
|
105
|
+
- ".rubocop.yml"
|
106
|
+
- ".tool-versions"
|
107
|
+
- CHANGELOG.md
|
108
|
+
- CODE_OF_CONDUCT.md
|
109
|
+
- Gemfile
|
110
|
+
- Gemfile.lock
|
111
|
+
- LICENSE.txt
|
112
|
+
- README.md
|
113
|
+
- Rakefile
|
114
|
+
- gemfiles/mongoid6.gemfile
|
115
|
+
- gemfiles/mongoid7.gemfile
|
116
|
+
- lib/mongoid/locking.rb
|
117
|
+
- lib/mongoid/locking/contextual/atomic.rb
|
118
|
+
- lib/mongoid/locking/contextual/mongo.rb
|
119
|
+
- lib/mongoid/locking/persistable.rb
|
120
|
+
- lib/mongoid/locking/persistable/creatable.rb
|
121
|
+
- lib/mongoid/locking/persistable/updatable.rb
|
122
|
+
- lib/mongoid/locking/selectable.rb
|
123
|
+
- lib/mongoid/locking/version.rb
|
124
|
+
- lib/mongoid/stale_object_error.rb
|
125
|
+
- sig/mongoid/locking.rbs
|
126
|
+
homepage: https://github.com/fullhealthmedical/mongoid-locking
|
127
|
+
licenses:
|
128
|
+
- MIT
|
129
|
+
metadata:
|
130
|
+
allowed_push_host: https://rubygems.org
|
131
|
+
homepage_uri: https://github.com/fullhealthmedical/mongoid-locking
|
132
|
+
source_code_uri: https://github.com/fullhealthmedical/mongoid-locking
|
133
|
+
changelog_uri: https://github.com/fullhealthmedical/blob/main/CHANGELOG.md
|
134
|
+
post_install_message:
|
135
|
+
rdoc_options: []
|
136
|
+
require_paths:
|
137
|
+
- lib
|
138
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
139
|
+
requirements:
|
140
|
+
- - ">="
|
141
|
+
- !ruby/object:Gem::Version
|
142
|
+
version: 2.6.0
|
143
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
144
|
+
requirements:
|
145
|
+
- - ">="
|
146
|
+
- !ruby/object:Gem::Version
|
147
|
+
version: '0'
|
148
|
+
requirements: []
|
149
|
+
rubygems_version: 3.0.3.1
|
150
|
+
signing_key:
|
151
|
+
specification_version: 4
|
152
|
+
summary: Implements optimistic locking for Mongoid objects.
|
153
|
+
test_files: []
|