rails_soft_lock 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +20 -0
- data/.ruby-version +1 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/README.md +104 -0
- data/Rakefile +12 -0
- data/lib/rails_soft_lock/configuration.rb +62 -0
- data/lib/rails_soft_lock/lock_object.rb +61 -0
- data/lib/rails_soft_lock/model_extensions.rb +86 -0
- data/lib/rails_soft_lock/railtie.rb +14 -0
- data/lib/rails_soft_lock/redis_adapter.rb +68 -0
- data/lib/rails_soft_lock/version.rb +5 -0
- data/lib/rails_soft_lock.rb +35 -0
- data/lib/tasks/rails_soft_lock.rake +43 -0
- data/sig/rails_soft_lock/configuration.rbs +50 -0
- data/sig/rails_soft_lock/lock_object.rbs +41 -0
- data/sig/rails_soft_lock/model_extensions.rbs +6 -0
- data/sig/rails_soft_lock/railtie.rbs +7 -0
- data/sig/rails_soft_lock/redis_adapter.rbs +29 -0
- data/sig/rails_soft_lock/version.rbs +3 -0
- data/sig/rails_soft_lock.rbs +17 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 952f05764b7d784066806fb56d9e91481bf75e7b32116d67b0d0b808c067ef63
|
4
|
+
data.tar.gz: ca98fdc3816f3ae28e344d410f34ed80e445a836eeda7bc88972c732d4f45623
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 51d3d93903af11c4fb821555fa0cbfa6308c8e5fe88d96be8ce9f5e0aaa36d31650aa35c169bc9dd233d9eda99c8a1c6aefd86641a9a8edf812324712ff5ee7a
|
7
|
+
data.tar.gz: 749c56642dbbee636c6053f612688a2f0306624dd5019aff37fa258e8ef20ef32443b33f48c88297d92723f456e6471bbdf644293048911581d4ebf650282053
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
AllCops:
|
2
|
+
NewCops: enable
|
3
|
+
|
4
|
+
plugins:
|
5
|
+
- rubocop-rake
|
6
|
+
- rubocop-rspec
|
7
|
+
- rubocop-rails
|
8
|
+
- rubocop-rspec_rails
|
9
|
+
|
10
|
+
Style/StringLiterals:
|
11
|
+
EnforcedStyle: double_quotes
|
12
|
+
|
13
|
+
Style/StringLiteralsInInterpolation:
|
14
|
+
EnforcedStyle: double_quotes
|
15
|
+
|
16
|
+
RSpec/ExampleLength:
|
17
|
+
Max: 8
|
18
|
+
|
19
|
+
Rails/ApplicationRecord:
|
20
|
+
Enabled: false
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-3.4.1
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
9
|
+
nationality, personal appearance, race, caste, color, religion, or sexual
|
10
|
+
identity and orientation.
|
11
|
+
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
13
|
+
diverse, inclusive, and healthy community.
|
14
|
+
|
15
|
+
## Our Standards
|
16
|
+
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
18
|
+
community include:
|
19
|
+
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
24
|
+
and learning from the experience
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the overall
|
26
|
+
community
|
27
|
+
|
28
|
+
Examples of unacceptable behavior include:
|
29
|
+
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or advances of
|
31
|
+
any kind
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
33
|
+
* Public or private harassment
|
34
|
+
* Publishing others' private information, such as a physical or email address,
|
35
|
+
without their explicit permission
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
37
|
+
professional setting
|
38
|
+
|
39
|
+
## Enforcement Responsibilities
|
40
|
+
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
44
|
+
or harmful.
|
45
|
+
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
49
|
+
decisions when appropriate.
|
50
|
+
|
51
|
+
## Scope
|
52
|
+
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
54
|
+
an individual is officially representing the community in public spaces.
|
55
|
+
Examples of representing our community include using an official email address,
|
56
|
+
posting via an official social media account, or acting as an appointed
|
57
|
+
representative at an online or offline event.
|
58
|
+
|
59
|
+
## Enforcement
|
60
|
+
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
62
|
+
reported to the community leaders responsible for enforcement at
|
63
|
+
[INSERT CONTACT METHOD].
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
65
|
+
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
67
|
+
reporter of any incident.
|
68
|
+
|
69
|
+
## Enforcement Guidelines
|
70
|
+
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
73
|
+
|
74
|
+
### 1. Correction
|
75
|
+
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
77
|
+
unprofessional or unwelcome in the community.
|
78
|
+
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
82
|
+
|
83
|
+
### 2. Warning
|
84
|
+
|
85
|
+
**Community Impact**: A violation through a single incident or series of
|
86
|
+
actions.
|
87
|
+
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
92
|
+
like social media. Violating these terms may lead to a temporary or permanent
|
93
|
+
ban.
|
94
|
+
|
95
|
+
### 3. Temporary Ban
|
96
|
+
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
98
|
+
sustained inappropriate behavior.
|
99
|
+
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
101
|
+
communication with the community for a specified period of time. No public or
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
104
|
+
Violating these terms may lead to a permanent ban.
|
105
|
+
|
106
|
+
### 4. Permanent Ban
|
107
|
+
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
111
|
+
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
113
|
+
community.
|
114
|
+
|
115
|
+
## Attribution
|
116
|
+
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
118
|
+
version 2.1, available at
|
119
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
120
|
+
|
121
|
+
Community Impact Guidelines were inspired by
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
123
|
+
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
126
|
+
[https://www.contributor-covenant.org/translations][translations].
|
127
|
+
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
129
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
132
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 Sergey Arkhipov
|
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,104 @@
|
|
1
|
+
# RailsSoftLock
|
2
|
+
|
3
|
+
This gem implements the ability to lock Rails Active Records using adapters for in-memory databases, such as redis, nats, etc.
|
4
|
+
Locks can be done by using the active record attribute.
|
5
|
+
it is possible to define the uniqueness scope of the attribute.
|
6
|
+
The gem is under active development.
|
7
|
+
Currently, an adapter to redis-compatible databases, such as redis, walkey, etc., has been implemented.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Install the gem and add to the application's Gemfile:
|
12
|
+
|
13
|
+
```bash
|
14
|
+
gem "rails_soft_lock", git: "https://github.com/sergey-arkhipov/rails_soft_lock.git"
|
15
|
+
|
16
|
+
```
|
17
|
+
|
18
|
+
After run
|
19
|
+
|
20
|
+
```bash
|
21
|
+
bundle install
|
22
|
+
```
|
23
|
+
|
24
|
+
Run rake task
|
25
|
+
|
26
|
+
```bash
|
27
|
+
rake rails_soft_lock:install
|
28
|
+
|
29
|
+
```
|
30
|
+
|
31
|
+
This will install config for gem, if file does not exists
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
## config/initializers/rails_soft_lock.rb
|
35
|
+
# frozen_string_literal: true
|
36
|
+
|
37
|
+
# Configuration for RailsSoftLock gem
|
38
|
+
# This file sets up the adapter and options for soft locking Active Record models
|
39
|
+
require "rails_soft_lock"
|
40
|
+
RailsSoftLock.configure do |config|
|
41
|
+
# Specify the adapter for storing locks
|
42
|
+
config.adapter = :redis
|
43
|
+
|
44
|
+
# Configuration for the Redis adapter
|
45
|
+
config.adapter_options = {
|
46
|
+
redis: Rails.application.config_for(:redis).merge(
|
47
|
+
timeout: 5
|
48
|
+
)
|
49
|
+
}
|
50
|
+
```
|
51
|
+
|
52
|
+
You can add any modification there.
|
53
|
+
|
54
|
+
Gem use ConnectionPool inside for safety connect to Redis adapter (now inplemented)
|
55
|
+
|
56
|
+
## Usage
|
57
|
+
|
58
|
+
Gem assumes that the User model is used to determine the user who sets the lock.
|
59
|
+
|
60
|
+
Model < ApplicationRecord should include `RailsSoftLock::ModelExtensions`
|
61
|
+
and `acts_as_locked_by` with `acts_as_locked_scope` should be set, for example
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
class Article < ApplicationRecord
|
65
|
+
include RailsSoftLock::ModelExtensions
|
66
|
+
|
67
|
+
acts_as_locked_by(:attribyte)
|
68
|
+
acts_as_locked_scope(proc { :scoped_attribute || "none" })
|
69
|
+
|
70
|
+
```
|
71
|
+
|
72
|
+
See `spec/rails_soft_lock/model_extensions_spec.rb for implemented methods`
|
73
|
+
|
74
|
+
### Attention
|
75
|
+
|
76
|
+
Pay attention how method `locak_or_find` work
|
77
|
+
|
78
|
+
Method return hash
|
79
|
+
`has_locked: false, locked_by: user.id`
|
80
|
+
|
81
|
+
`has_lock`: false implies that there was no lock on the passed object before this point.
|
82
|
+
Not to be confused with the result of executing the lock itself.
|
83
|
+
Since this is an in-memory base and the goal is quick and easy access, the method sets the lock,
|
84
|
+
reports that there was no lock before, and returns the user of the lock.
|
85
|
+
If there was a lock, true is returned and the user of this current lock.
|
86
|
+
The lock itself is not changed.
|
87
|
+
|
88
|
+
## Development
|
89
|
+
|
90
|
+
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.
|
91
|
+
|
92
|
+
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).
|
93
|
+
|
94
|
+
## Contributing
|
95
|
+
|
96
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rails_soft_lock. 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/[USERNAME]/rails_soft_lock/blob/master/CODE_OF_CONDUCT.md).
|
97
|
+
|
98
|
+
## License
|
99
|
+
|
100
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
101
|
+
|
102
|
+
## Code of Conduct
|
103
|
+
|
104
|
+
Everyone interacting in the RailsSoftLock project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/rails_soft_lock/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# lib/rails_soft_lock/configuration.rb
|
4
|
+
module RailsSoftLock
|
5
|
+
# Configuration class for RailsSoftLock gem.
|
6
|
+
class Configuration
|
7
|
+
# List of supported adapters.
|
8
|
+
VALID_ADAPTERS = %i[redis nats memcached].freeze
|
9
|
+
attr_reader :adapter
|
10
|
+
attr_accessor :adapter_options, :acts_as_locked_by, :acts_as_locked_scope
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
@adapter = :redis # Default adapter
|
14
|
+
@adapter_options = adapter_options || {} # Default adapter options
|
15
|
+
@acts_as_locked_by = :none
|
16
|
+
@acts_as_locked_scope = -> { "default" }
|
17
|
+
end
|
18
|
+
|
19
|
+
def [](key)
|
20
|
+
send(key)
|
21
|
+
end
|
22
|
+
|
23
|
+
def []=(key, value)
|
24
|
+
send("#{key}=", value)
|
25
|
+
end
|
26
|
+
|
27
|
+
# Sets the adapter and validates it.
|
28
|
+
# @param value [Symbol] The adapter to use.
|
29
|
+
# @raise [ArgumentError] If the adapter is not supported.
|
30
|
+
def adapter=(value)
|
31
|
+
raise ArgumentError, "Adapter must be one of: #{VALID_ADAPTERS.join(", ")}" unless VALID_ADAPTERS.include?(value)
|
32
|
+
|
33
|
+
@adapter = value
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
class << self
|
38
|
+
# Configures the RailsSoftLock gem with a block.
|
39
|
+
# @yield [config] Yields the configuration object to the block.
|
40
|
+
# @return [void]
|
41
|
+
def configure
|
42
|
+
@configuration ||= Configuration.new
|
43
|
+
if block_given?
|
44
|
+
yield(@configuration)
|
45
|
+
else
|
46
|
+
warn "[RailsSoftLock] No configuration block provided in `configure`"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# Returns the current configuration instance.
|
51
|
+
# @return [Configuration] The configuration object.
|
52
|
+
def configuration
|
53
|
+
@configuration ||= Configuration.new
|
54
|
+
end
|
55
|
+
|
56
|
+
# Resets the configuration (useful for testing).
|
57
|
+
# @return [void]
|
58
|
+
def reset_configuration
|
59
|
+
@configuration = nil
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# lib/rails_soft_lock/lock_object.rb
|
4
|
+
|
5
|
+
module RailsSoftLock
|
6
|
+
# Service for managing object locks
|
7
|
+
# A lock object contains the following parameters:
|
8
|
+
# - object_name: The name of the lock storage, typically the model name with an optional scope
|
9
|
+
# - object_key: The identifier of the lock instance, typically a unique database record ID
|
10
|
+
# - object_value: The identifier of the locker that locked the record
|
11
|
+
class LockObject
|
12
|
+
# Подключаем адаптер на основе конфигурации
|
13
|
+
def self.adapter
|
14
|
+
case RailsSoftLock.configuration.adapter
|
15
|
+
when :redis
|
16
|
+
RailsSoftLock::RedisAdapter
|
17
|
+
when :nats
|
18
|
+
RailsSoftLock::NatsAdapter
|
19
|
+
when :memcached
|
20
|
+
RailsSoftLock::MemcachedAdapter
|
21
|
+
else
|
22
|
+
raise ArgumentError, "Unknown adapter: #{RailsSoftLock.configuration.adapter}"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
include adapter
|
27
|
+
|
28
|
+
attr_reader :object_name, :object_key, :object_value
|
29
|
+
|
30
|
+
def initialize(object_name:, object_key: nil, object_value: nil)
|
31
|
+
@object_name = object_name
|
32
|
+
@object_key = object_key
|
33
|
+
@object_value = object_value.present? ? object_value.to_s : nil # Convert to string for consistency
|
34
|
+
end
|
35
|
+
|
36
|
+
# Returns the ID of the locker who locked the object
|
37
|
+
# @return [String, nil] The locker's ID or nil if not locked
|
38
|
+
def locked_by
|
39
|
+
get
|
40
|
+
end
|
41
|
+
|
42
|
+
# Attempts to lock the object or returns the existing lock
|
43
|
+
# @return [Hash] { has_locked: Boolean, locked_by: String or nil }
|
44
|
+
def lock_or_find
|
45
|
+
locked_object = get
|
46
|
+
{ has_locked: create, locked_by: locked_object || object_value }
|
47
|
+
end
|
48
|
+
|
49
|
+
# Unlocks the object
|
50
|
+
# @return [Boolean] True if the lock was removed, false otherwise
|
51
|
+
def unlock
|
52
|
+
delete
|
53
|
+
end
|
54
|
+
|
55
|
+
# Returns all locks in the storage
|
56
|
+
# @return [Hash] All key-value pairs in the storage
|
57
|
+
def all_locks
|
58
|
+
all
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# lib/rails_soft_lock/model_extensions.rb
|
4
|
+
|
5
|
+
module RailsSoftLock
|
6
|
+
# Определяет настройки блокировки модели
|
7
|
+
module ModelExtensions
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
|
10
|
+
class_methods do
|
11
|
+
def acts_as_locked_by(attribute = :none)
|
12
|
+
RailsSoftLock.configuration[:acts_as_locked_by] = attribute
|
13
|
+
end
|
14
|
+
|
15
|
+
def current_locked_attribute
|
16
|
+
RailsSoftLock.configuration[:acts_as_locked_by]
|
17
|
+
end
|
18
|
+
|
19
|
+
def acts_as_locked_scope(attribute = nil)
|
20
|
+
RailsSoftLock.configuration[:acts_as_locked_scope] = attribute || -> { "default" }
|
21
|
+
end
|
22
|
+
|
23
|
+
def current_locked_scope
|
24
|
+
RailsSoftLock.configuration[:acts_as_locked_scope]
|
25
|
+
end
|
26
|
+
|
27
|
+
def all_locks
|
28
|
+
RailsSoftLock::LockObject.new(object_name: object_name).all_locks
|
29
|
+
end
|
30
|
+
|
31
|
+
def unlock(object_key)
|
32
|
+
RailsSoftLock::LockObject.new(object_name: object_name, object_key: object_key).unlock
|
33
|
+
end
|
34
|
+
|
35
|
+
def object_name
|
36
|
+
scope = current_locked_scope&.call || "default"
|
37
|
+
"#{name}-#{scope}"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
included do # rubocop:disable Metrics/BlockLength
|
42
|
+
delegate :object_name, to: :class
|
43
|
+
|
44
|
+
def lock_or_find(user)
|
45
|
+
lock_object_for(user).lock_or_find
|
46
|
+
end
|
47
|
+
|
48
|
+
def unlock(user)
|
49
|
+
lock_object_for(user).unlock
|
50
|
+
end
|
51
|
+
|
52
|
+
def locked?
|
53
|
+
locked_by.present?
|
54
|
+
end
|
55
|
+
|
56
|
+
def locked_by
|
57
|
+
user_id = base_lock_object.locked_by&.to_i
|
58
|
+
user_id ? User.find_by(id: user_id) : nil
|
59
|
+
end
|
60
|
+
|
61
|
+
def object_key
|
62
|
+
attribute = RailsSoftLock.configuration[:acts_as_locked_by]
|
63
|
+
raise ArgumentError, "No locked attribute defined" if attribute == :none
|
64
|
+
|
65
|
+
send(attribute)
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def lock_object_for(user)
|
71
|
+
RailsSoftLock::LockObject.new(
|
72
|
+
object_name: object_name,
|
73
|
+
object_key: object_key,
|
74
|
+
object_value: user.id.to_s
|
75
|
+
)
|
76
|
+
end
|
77
|
+
|
78
|
+
def base_lock_object
|
79
|
+
RailsSoftLock::LockObject.new(
|
80
|
+
object_name: object_name,
|
81
|
+
object_key: object_key
|
82
|
+
)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# lib/rails_soft_lock/railtie.rb
|
4
|
+
|
5
|
+
module RailsSoftLock
|
6
|
+
# Load rails_soft_lock:install
|
7
|
+
# After load rails rails_soft_lock:install check config/initializers/rails_soft_lock.rb
|
8
|
+
# and create initializer if do not exists
|
9
|
+
class Railtie < Rails::Railtie
|
10
|
+
rake_tasks do
|
11
|
+
load "tasks/rails_soft_lock.rake"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# lib/rails_soft_lock/redis_adapter.rb
|
4
|
+
|
5
|
+
require "redis"
|
6
|
+
require "connection_pool"
|
7
|
+
|
8
|
+
module RailsSoftLock
|
9
|
+
# Adapter for store lock in redis
|
10
|
+
module RedisAdapter
|
11
|
+
# Initialize Redis client
|
12
|
+
def redis_client # rubocop:disable Metrics/MethodLength
|
13
|
+
@redis_client ||= begin
|
14
|
+
redis_config = RailsSoftLock.configuration.adapter_options.fetch(:redis, {})
|
15
|
+
|
16
|
+
# Set config defaults
|
17
|
+
defaults = {
|
18
|
+
url: "redis://localhost:6379/0",
|
19
|
+
timeout: 5
|
20
|
+
}
|
21
|
+
# Merge config options
|
22
|
+
config = defaults.merge(redis_config)
|
23
|
+
|
24
|
+
ConnectionPool::Wrapper.new do
|
25
|
+
Redis.new(**config)
|
26
|
+
end
|
27
|
+
rescue Redis::CannotConnectError => e
|
28
|
+
raise RailsSoftLock::Error, "Failed to connect to Redis: #{e.message}"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# Retrieves a value by key from the specified hash
|
33
|
+
# @return [String, nil] The value associated with the key
|
34
|
+
def get
|
35
|
+
redis_client.hget(@object_name, @object_key)
|
36
|
+
end
|
37
|
+
|
38
|
+
# Creates a new key-value pair if the key does not exist
|
39
|
+
# @return [Boolean] true if the key was created, false if it already existed
|
40
|
+
def create
|
41
|
+
result = redis_client.multi do |transaction|
|
42
|
+
transaction.hsetnx(@object_name, @object_key, @object_value)
|
43
|
+
transaction.hget(@object_name, @object_key)
|
44
|
+
end
|
45
|
+
!result.first # has_locked - false id created
|
46
|
+
end
|
47
|
+
|
48
|
+
# Updates the value for an existing key or creates a new key-value pair
|
49
|
+
# @return [Boolean] true if the key was updated, false if it was created
|
50
|
+
def update
|
51
|
+
result = redis_client.hset(@object_name, @object_key, @object_value)
|
52
|
+
result.zero?
|
53
|
+
end
|
54
|
+
|
55
|
+
# Deletes a key from the specified hash
|
56
|
+
# @return [Boolean] true if the key was deleted, false if it did not exist
|
57
|
+
def delete
|
58
|
+
result = redis_client.hdel(@object_name, @object_key)
|
59
|
+
!result.zero?
|
60
|
+
end
|
61
|
+
|
62
|
+
# Retrieves all key-value pairs in the specified hash
|
63
|
+
# @return [Hash] The key-value pairs in the hash
|
64
|
+
def all
|
65
|
+
redis_client.hgetall(@object_name)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "zeitwerk"
|
4
|
+
loader = Zeitwerk::Loader.for_gem
|
5
|
+
loader.setup
|
6
|
+
|
7
|
+
# RailsSoftLock - module for soft lock ApplicationRecord by attribyte
|
8
|
+
module RailsSoftLock
|
9
|
+
# Error class for gem
|
10
|
+
class Error < StandardError; end
|
11
|
+
|
12
|
+
def self.lock_manager(object_name:, object_key: nil, object_value: nil)
|
13
|
+
@lock_manager ||= LockObject.new(
|
14
|
+
object_name: object_name,
|
15
|
+
object_key: object_key,
|
16
|
+
object_value: object_value
|
17
|
+
)
|
18
|
+
end
|
19
|
+
|
20
|
+
# Pass methods to adapter
|
21
|
+
def self.get(object_name, object_key)
|
22
|
+
lock_manager(object_name: object_name, object_key: object_key).locked_by
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.create(object_name, object_key, object_value)
|
26
|
+
lock_manager(object_name: object_name, object_key: object_key, object_value: object_value).lock_or_find
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.update(object_name, object_key, object_value)
|
30
|
+
lock_manager(object_name: object_name, object_key: object_key, object_value: object_value).lock_or_find
|
31
|
+
end
|
32
|
+
|
33
|
+
# Load rake task if Rails
|
34
|
+
require "rails_soft_lock/railtie" if defined?(Rails)
|
35
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# lib/tasks/rails_soft_lock.rake
|
4
|
+
|
5
|
+
namespace :rails_soft_lock do
|
6
|
+
desc "Generate a RailsSoftLock configuration file in config/initializers"
|
7
|
+
task :install do # rubocop:disable Rails/RakeEnvironment
|
8
|
+
initializer_path = Rails.root.join("config/initializers/rails_soft_lock.rb")
|
9
|
+
|
10
|
+
if File.exist?(initializer_path)
|
11
|
+
puts "RailsSoftLock configuration file already exists at #{initializer_path}"
|
12
|
+
else
|
13
|
+
File.write(initializer_path, <<~RUBY)
|
14
|
+
# frozen_string_literal: true
|
15
|
+
|
16
|
+
# Configuration for RailsSoftLock gem
|
17
|
+
# This file sets up the adapter and options for soft locking Active Record models
|
18
|
+
|
19
|
+
RailsSoftLock.configure do |config|
|
20
|
+
# Specify the adapter for storing locks
|
21
|
+
config.adapter = :redis
|
22
|
+
|
23
|
+
# configuration for the redis adapter
|
24
|
+
config.adapter_options = {
|
25
|
+
redis: rails.application.config_for(:redis).merge(
|
26
|
+
timeout: 5
|
27
|
+
)
|
28
|
+
}
|
29
|
+
|
30
|
+
# (Optional) Attribute used for locking
|
31
|
+
# config.acts_as_locked_by = :lock_attribute
|
32
|
+
|
33
|
+
# (Optional) Scope for separating locks
|
34
|
+
# config.acts_as_locked_scope = -> { "default_scope" }
|
35
|
+
|
36
|
+
# (Optional) Model class for locked_by lookups
|
37
|
+
# config.locked_by_class = User
|
38
|
+
end
|
39
|
+
RUBY
|
40
|
+
puts "Created RailsSoftLock configuration file at #{initializer_path}"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# lib/rails_soft_lock/configuration.rb
|
2
|
+
module RailsSoftLock
|
3
|
+
self.@configuration: untyped
|
4
|
+
|
5
|
+
# Configuration class for RailsSoftLock gem.
|
6
|
+
class Configuration
|
7
|
+
@adapter: untyped
|
8
|
+
|
9
|
+
@adapter_options: untyped
|
10
|
+
|
11
|
+
@acts_as_locked_by: untyped
|
12
|
+
|
13
|
+
@acts_as_locked_scope: untyped
|
14
|
+
|
15
|
+
# List of supported adapters.
|
16
|
+
VALID_ADAPTERS: ::Array[:redis | :nats | :memcached]
|
17
|
+
|
18
|
+
attr_reader adapter: untyped
|
19
|
+
|
20
|
+
attr_accessor adapter_options: untyped
|
21
|
+
|
22
|
+
attr_accessor acts_as_locked_by: untyped
|
23
|
+
|
24
|
+
attr_accessor acts_as_locked_scope: untyped
|
25
|
+
|
26
|
+
def initialize: () -> void
|
27
|
+
|
28
|
+
def []: (untyped key) -> untyped
|
29
|
+
|
30
|
+
def []=: (untyped key, untyped value) -> untyped
|
31
|
+
|
32
|
+
# Sets the adapter and validates it.
|
33
|
+
# @param value [Symbol] The adapter to use.
|
34
|
+
# @raise [ArgumentError] If the adapter is not supported.
|
35
|
+
def adapter=: (untyped value) -> untyped
|
36
|
+
end
|
37
|
+
|
38
|
+
# Configures the RailsSoftLock gem with a block.
|
39
|
+
# @yield [config] Yields the configuration object to the block.
|
40
|
+
# @return [void]
|
41
|
+
def self.configure: () ?{ (untyped) -> untyped } -> untyped
|
42
|
+
|
43
|
+
# Returns the current configuration instance.
|
44
|
+
# @return [Configuration] The configuration object.
|
45
|
+
def self.configuration: () -> untyped
|
46
|
+
|
47
|
+
# Resets the configuration (useful for testing).
|
48
|
+
# @return [void]
|
49
|
+
def self.reset_configuration: () -> untyped
|
50
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module RailsSoftLock
|
2
|
+
# Service for managing object locks
|
3
|
+
# A lock object contains the following parameters:
|
4
|
+
# - object_name: The name of the lock storage, typically the model name with an optional scope
|
5
|
+
# - object_key: The identifier of the lock instance, typically a unique database record ID
|
6
|
+
# - object_value: The identifier of the locker that locked the record
|
7
|
+
class LockObject
|
8
|
+
@object_name: untyped
|
9
|
+
|
10
|
+
@object_key: untyped
|
11
|
+
|
12
|
+
@object_value: untyped
|
13
|
+
|
14
|
+
# Подключаем адаптер на основе конфигурации
|
15
|
+
def self.adapter: () -> untyped
|
16
|
+
|
17
|
+
attr_reader object_name: untyped
|
18
|
+
|
19
|
+
attr_reader object_key: untyped
|
20
|
+
|
21
|
+
attr_reader object_value: untyped
|
22
|
+
|
23
|
+
def initialize: (object_name: untyped, ?object_key: untyped?, ?object_value: untyped?) -> void
|
24
|
+
|
25
|
+
# Returns the ID of the locker who locked the object
|
26
|
+
# @return [String, nil] The locker's ID or nil if not locked
|
27
|
+
def locked_by: () -> untyped
|
28
|
+
|
29
|
+
# Attempts to lock the object or returns the existing lock
|
30
|
+
# @return [Hash] { has_locked: Boolean, locked_by: String or nil }
|
31
|
+
def lock_or_find: () -> { has_locked: untyped, locked_by: untyped }
|
32
|
+
|
33
|
+
# Unlocks the object
|
34
|
+
# @return [Boolean] True if the lock was removed, false otherwise
|
35
|
+
def unlock: () -> untyped
|
36
|
+
|
37
|
+
# Returns all locks in the storage
|
38
|
+
# @return [Hash] All key-value pairs in the storage
|
39
|
+
def all_locks: () -> untyped
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module RailsSoftLock
|
2
|
+
# Adapter for store lock in redis
|
3
|
+
module RedisAdapter
|
4
|
+
@redis_client: untyped
|
5
|
+
|
6
|
+
# Initialize Redis client
|
7
|
+
def redis_client: () -> untyped
|
8
|
+
|
9
|
+
# Retrieves a value by key from the specified hash
|
10
|
+
# @return [String, nil] The value associated with the key
|
11
|
+
def get: () -> untyped
|
12
|
+
|
13
|
+
# Creates a new key-value pair if the key does not exist
|
14
|
+
# @return [Boolean] true if the key was created, false if it already existed
|
15
|
+
def create: () -> untyped
|
16
|
+
|
17
|
+
# Updates the value for an existing key or creates a new key-value pair
|
18
|
+
# @return [Boolean] true if the key was updated, false if it was created
|
19
|
+
def update: () -> untyped
|
20
|
+
|
21
|
+
# Deletes a key from the specified hash
|
22
|
+
# @return [Boolean] true if the key was deleted, false if it did not exist
|
23
|
+
def delete: () -> untyped
|
24
|
+
|
25
|
+
# Retrieves all key-value pairs in the specified hash
|
26
|
+
# @return [Hash] The key-value pairs in the hash
|
27
|
+
def all: () -> untyped
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# RailsSoftLock - module for soft lock ApplicationRecord by attribyte
|
2
|
+
module RailsSoftLock
|
3
|
+
self.@lock_manager: untyped
|
4
|
+
|
5
|
+
# Error class for gem
|
6
|
+
class Error < StandardError
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.lock_manager: (object_name: untyped, ?object_key: untyped?, ?object_value: untyped?) -> untyped
|
10
|
+
|
11
|
+
# Pass methods to adapter
|
12
|
+
def self.get: (untyped object_name, untyped object_key) -> untyped
|
13
|
+
|
14
|
+
def self.create: (untyped object_name, untyped object_key, untyped object_value) -> untyped
|
15
|
+
|
16
|
+
def self.update: (untyped object_name, untyped object_key, untyped object_value) -> untyped
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails_soft_lock
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sergey Arkhipov
|
8
|
+
- Vladimir Peskov
|
9
|
+
bindir: lib
|
10
|
+
cert_chain: []
|
11
|
+
date: 2025-04-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: connection_pool
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.5'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: zeitwerk
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.7'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.7'
|
41
|
+
description: Using In-Memory Databases to Work with Rails Active Record Locks
|
42
|
+
email:
|
43
|
+
- Sergey-Arkhipov@yandex.ru
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".rspec"
|
49
|
+
- ".rubocop.yml"
|
50
|
+
- ".ruby-version"
|
51
|
+
- CHANGELOG.md
|
52
|
+
- CODE_OF_CONDUCT.md
|
53
|
+
- LICENSE.txt
|
54
|
+
- README.md
|
55
|
+
- Rakefile
|
56
|
+
- lib/rails_soft_lock.rb
|
57
|
+
- lib/rails_soft_lock/configuration.rb
|
58
|
+
- lib/rails_soft_lock/lock_object.rb
|
59
|
+
- lib/rails_soft_lock/model_extensions.rb
|
60
|
+
- lib/rails_soft_lock/railtie.rb
|
61
|
+
- lib/rails_soft_lock/redis_adapter.rb
|
62
|
+
- lib/rails_soft_lock/version.rb
|
63
|
+
- lib/tasks/rails_soft_lock.rake
|
64
|
+
- sig/rails_soft_lock.rbs
|
65
|
+
- sig/rails_soft_lock/configuration.rbs
|
66
|
+
- sig/rails_soft_lock/lock_object.rbs
|
67
|
+
- sig/rails_soft_lock/model_extensions.rbs
|
68
|
+
- sig/rails_soft_lock/railtie.rbs
|
69
|
+
- sig/rails_soft_lock/redis_adapter.rbs
|
70
|
+
- sig/rails_soft_lock/version.rbs
|
71
|
+
homepage: https://github.com/sergey-arkhipov/rails_soft_lock
|
72
|
+
licenses:
|
73
|
+
- MIT
|
74
|
+
metadata:
|
75
|
+
homepage_uri: https://github.com/sergey-arkhipov/rails_soft_lock
|
76
|
+
source_code_uri: https://github.com/sergey-arkhipov/rails_soft_lock
|
77
|
+
changelog_uri: https://github.com/sergey-arkhipov/rails_soft_lock
|
78
|
+
rubygems_mfa_required: 'true'
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: 3.3.0
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubygems_version: 3.6.2
|
94
|
+
specification_version: 4
|
95
|
+
summary: Lock Active record by attribyte using in-memory adapters
|
96
|
+
test_files: []
|