Skip to content

Commit d8a827e

Browse files
committed
Rubocop
1 parent a22d32b commit d8a827e

File tree

13 files changed

+166
-114
lines changed

13 files changed

+166
-114
lines changed

.rubocop.yml

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,41 @@
1+
require:
2+
- rubocop-rake
3+
- rubocop-rspec
4+
15
AllCops:
6+
NewCops: enable
27
TargetRubyVersion: 3.1
8+
Exclude:
9+
- 'test.rb'
10+
11+
Metrics/MethodLength:
12+
Enabled: false
13+
14+
Metrics/AbcSize:
15+
Enabled: false
16+
17+
# TODO: revisit this exclusion - (zero?)
18+
Style/NumericPredicate:
19+
Exclude:
20+
- lib/redis_single_file/semaphore.rb
21+
22+
#
23+
# rspec stuff
24+
#
25+
26+
# Prefer have_received for setting message expectations.
27+
# Setup redis_mock as a spy using allow or instance_spy.
28+
RSpec/MessageSpies:
29+
Enabled: false
30+
31+
# Prefer allow over expect when configuring a response.
32+
RSpec/StubbedMock:
33+
Enabled: false
334

4-
Style/StringLiterals:
5-
EnforcedStyle: double_quotes
35+
# Example has too many expectations [3/1].
36+
RSpec/MultipleExpectations:
37+
Max: 6
638

7-
Style/StringLiteralsInInterpolation:
8-
EnforcedStyle: double_quotes
39+
# Example has too many lines. [8/5]
40+
RSpec/ExampleLength:
41+
Max: 10

Gemfile

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
# frozen_string_literal: true
22

3-
source "https://rubygems.org"
3+
source 'https://rubygems.org'
44

55
# Specify your gem's dependencies in redis_single_file.gemspec
66
gemspec
77

8-
gem "rake", "~> 13.0"
8+
gem 'rake', '~> 13.0'
99

10-
gem "rspec", "~> 3.0"
10+
gem 'rspec', '~> 3.0'
1111

12-
gem "rubocop", "~> 1.21"
12+
gem 'rubocop', '~> 1.21'
13+
gem 'rubocop-rake', '~> 0.6.0'
14+
gem 'rubocop-rspec', '~> 3.4.0 '
1315

14-
gem "pry", "~> 0.15.2"
16+
gem 'pry', '~> 0.15.2'
1517

1618
gem 'mock_redis', '~> 0.49.0'

Gemfile.lock

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ GEM
5656
unicode-display_width (>= 2.4.0, < 4.0)
5757
rubocop-ast (1.38.0)
5858
parser (>= 3.3.1.0)
59+
rubocop-rake (0.6.0)
60+
rubocop (~> 1.0)
61+
rubocop-rspec (3.4.0)
62+
rubocop (~> 1.61)
5963
ruby-progressbar (1.13.0)
6064
unicode-display_width (3.1.4)
6165
unicode-emoji (~> 4.0, >= 4.0.4)
@@ -72,6 +76,8 @@ DEPENDENCIES
7276
redis-single-file!
7377
rspec (~> 3.0)
7478
rubocop (~> 1.21)
79+
rubocop-rake (~> 0.6.0)
80+
rubocop-rspec (~> 3.4.0)
7581

7682
BUNDLED WITH
7783
2.6.3

Rakefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# frozen_string_literal: true
22

3-
require "bundler/gem_tasks"
4-
require "rspec/core/rake_task"
3+
require 'bundler/gem_tasks'
4+
require 'rspec/core/rake_task'
55

66
RSpec::Core::RakeTask.new(:spec)
77

8-
require "rubocop/rake_task"
8+
require 'rubocop/rake_task'
99

1010
RuboCop::RakeTask.new
1111

bin/console

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#!/usr/bin/env ruby
22
# frozen_string_literal: true
33

4-
require "bundler/setup"
5-
require "redis_single_file"
4+
require 'bundler/setup'
5+
require 'redis_single_file'
66

77
# You can add fixtures and/or initialization code here to make experimenting
88
# with your gem easier. You can also use a different console, if you like.
99

10-
require "irb"
10+
require 'irb'
1111
IRB.start(__FILE__)

lib/redis_single_file.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,20 @@
33
require 'redis'
44
require 'singleton'
55

6-
require_relative "redis_single_file/version"
7-
require_relative "redis_single_file/configuration"
8-
require_relative "redis_single_file/semaphore"
6+
require_relative 'redis_single_file/version'
7+
require_relative 'redis_single_file/configuration'
8+
require_relative 'redis_single_file/semaphore'
99

10+
#
11+
# RedisSingleFile - Distributed Execution Synchronization
12+
#
13+
# Redis single file is a queue-based implementation of a remote/shared
14+
# semaphore for distributed execution synchronization. A distributed
15+
# semaphore may be useful for synchronizing execution across numerous
16+
# instances or between the application and background job workers.
17+
#
18+
# @author lifeBCE
19+
#
1020
module RedisSingleFile
1121
# alias semaphore as mutex
1222
Mutex = Semaphore

lib/redis_single_file/semaphore.rb

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def initialize(
8282
# @return [nil] redis blpop timeout
8383
def synchronize(timeout: 0, &blk)
8484
synchronize!(timeout:, &blk)
85-
rescue QueueTimeout => _err
85+
rescue QueueTimeout => _e
8686
nil
8787
end
8888

@@ -140,16 +140,14 @@ def unlock_queue
140140
end
141141

142142
def with_retry_protection
143-
begin
144-
yield if block_given?
145-
rescue Redis::ConnectionError => _err
146-
retry_count ||= 0
147-
retry_count += 1
143+
yield if block_given?
144+
rescue Redis::ConnectionError => _e
145+
retry_count ||= 0
146+
retry_count += 1
148147

149-
# retry 5 times over 15 seconds then give up
150-
sleep(retry_count) && retry if retry_count < 6
151-
raise # re-raise after all retries exhausted
152-
end
148+
# retry 5 times over 15 seconds then give up
149+
sleep(retry_count) && retry if retry_count < 6
150+
raise # re-raise after all retries exhausted
153151
end
154152
end
155153
end

lib/redis_single_file/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module RedisSingleFile
4-
VERSION = "0.1.1"
4+
VERSION = '0.1.1'
55
end

redis-single-file.gemspec

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
# frozen_string_literal: true
22

3-
require_relative "lib/redis_single_file/version"
3+
require_relative 'lib/redis_single_file/version'
44

55
Gem::Specification.new do |spec|
6-
spec.name = "redis-single-file"
6+
spec.name = 'redis-single-file'
77
spec.version = RedisSingleFile::VERSION
8-
spec.authors = ["LifeBCE"]
9-
spec.email = ["[email protected]"]
8+
spec.authors = ['LifeBCE']
9+
spec.email = ['[email protected]']
1010

11-
spec.summary = "Distributed semaphore implementation with redis."
12-
spec.description = "Synchronize execution across numerous instances."
13-
spec.homepage = "https://coming.com/soon"
14-
spec.license = "MIT"
15-
spec.required_ruby_version = ">= 3.1.0"
11+
spec.summary = 'Distributed semaphore implementation with redis.'
12+
spec.description = 'Synchronize execution across numerous instances.'
13+
spec.homepage = 'https://coming.com/soon'
14+
spec.license = 'MIT'
15+
spec.required_ruby_version = '>= 3.1.0'
1616

17-
spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
17+
spec.metadata['allowed_push_host'] = "TODO: Set to your gem server 'https://example.com'"
1818

19-
spec.metadata["homepage_uri"] = spec.homepage
20-
# spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
21-
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
19+
spec.metadata['homepage_uri'] = spec.homepage
20+
# spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
21+
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
2222

2323
# Specify which files should be added to the gem when it is released.
2424
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
@@ -29,13 +29,14 @@ Gem::Specification.new do |spec|
2929
f.start_with?(*%w[bin/ test/ spec/ features/ .git .github appveyor Gemfile])
3030
end
3131
end
32-
spec.bindir = "exe"
32+
spec.bindir = 'exe'
3333
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
34-
spec.require_paths = ["lib"]
34+
spec.require_paths = ['lib']
3535

3636
# Uncomment to register a new dependency of your gem
37-
spec.add_dependency "redis", "~> 5.3.0"
37+
spec.add_dependency 'redis', '~> 5.3.0'
3838

3939
# For more information and examples about making a new gem, check out our
4040
# guide at: https://bundler.io/guides/creating_gem.html
41+
spec.metadata['rubygems_mfa_required'] = 'true'
4142
end
Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
# frozen_string_literal: true
22

33
RSpec.describe RedisSingleFile::Configuration do
4-
it "defaults are configured as expected" do
4+
after do
5+
# reset singleton to defaults
6+
RedisSingleFile.configuration do |config|
7+
config.host = RedisSingleFile::Configuration::DEFAULT_HOST
8+
config.port = RedisSingleFile::Configuration::DEFAULT_PORT
9+
config.name = RedisSingleFile::Configuration::DEFAULT_NAME
10+
config.expire_in = RedisSingleFile::Configuration::DEFAULT_EXPIRE_IN
11+
end
12+
end
13+
14+
it 'defaults are configured as expected' do
515
expect(RedisSingleFile::Configuration::DEFAULT_HOST).to eq('localhost')
616
expect(RedisSingleFile::Configuration::DEFAULT_PORT).to eq('6379')
717
expect(RedisSingleFile::Configuration::DEFAULT_NAME).to eq('default')
@@ -10,34 +20,26 @@
1020
expect(RedisSingleFile::Configuration::DEFAULT_QUEUE_KEY).to eq('RedisSingleFile/Queue/%s')
1121
end
1222

13-
it "delegations return expected default values" do
14-
expect(RedisSingleFile::Configuration.host).to eq('localhost')
15-
expect(RedisSingleFile::Configuration.port).to eq('6379')
16-
expect(RedisSingleFile::Configuration.name).to eq('default')
17-
expect(RedisSingleFile::Configuration.expire_in).to eq(300)
18-
expect(RedisSingleFile::Configuration.mutex_key).to eq('RedisSingleFile/Mutex/%s')
19-
expect(RedisSingleFile::Configuration.queue_key).to eq('RedisSingleFile/Queue/%s')
23+
it 'delegations return expected default values' do
24+
expect(described_class.host).to eq('localhost')
25+
expect(described_class.port).to eq('6379')
26+
expect(described_class.name).to eq('default')
27+
expect(described_class.expire_in).to eq(300)
28+
expect(described_class.mutex_key).to eq('RedisSingleFile/Mutex/%s')
29+
expect(described_class.queue_key).to eq('RedisSingleFile/Queue/%s')
2030
end
2131

22-
it "configuration block changes values" do
32+
it 'configuration block changes values' do
2333
RedisSingleFile.configuration do |config|
2434
config.host = 'test_host'
2535
config.port = '1234'
2636
config.name = 'queue_name'
2737
config.expire_in = 100
2838
end
2939

30-
expect(RedisSingleFile::Configuration.host).to eq('test_host')
31-
expect(RedisSingleFile::Configuration.port).to eq('1234')
32-
expect(RedisSingleFile::Configuration.name).to eq('queue_name')
33-
expect(RedisSingleFile::Configuration.expire_in).to eq(100)
34-
35-
# reset singleton to defaults
36-
RedisSingleFile.configuration do |config|
37-
config.host = RedisSingleFile::Configuration::DEFAULT_HOST
38-
config.port = RedisSingleFile::Configuration::DEFAULT_PORT
39-
config.name = RedisSingleFile::Configuration::DEFAULT_NAME
40-
config.expire_in = RedisSingleFile::Configuration::DEFAULT_EXPIRE_IN
41-
end
40+
expect(described_class.host).to eq('test_host')
41+
expect(described_class.port).to eq('1234')
42+
expect(described_class.name).to eq('queue_name')
43+
expect(described_class.expire_in).to eq(100)
4244
end
4345
end

0 commit comments

Comments
 (0)