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

Run Rubocop and fix errors #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,14 @@ jobs:
bundler-cache: true
- run: |
bundle exec rake

rubocop:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: ruby
bundler-cache: true
- run: |
bundle exec rubocop
88 changes: 45 additions & 43 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
AllCops:
NewCops: enable
TargetRubyVersion: 3.1
Exclude:
- .git/**/*
- tmp/**/*
- suo.gemspec
- vendor/**/*

Lint/DuplicateMethods:
Enabled: true

Lint/DeprecatedClassMethods:
Enabled: true

Style/TrailingWhitespace:
Layout/TrailingWhitespace:
Enabled: true

Style/Tab:
Layout/IndentationStyle:
Enabled: true

Style/TrailingBlankLines:
Layout/TrailingEmptyLines:
Enabled: true

Style/NilComparison:
Expand All @@ -34,7 +36,7 @@ Style/RedundantReturn:
Style/ClassCheck:
Enabled: true

Style/EmptyLines:
Layout/EmptyLines:
Enabled: true

Style/EmptyLiteral:
Expand All @@ -43,80 +45,79 @@ Style/EmptyLiteral:
Style/Alias:
Enabled: true

Style/MethodCallParentheses:
Style/MethodCallWithoutArgsParentheses:
Enabled: true

Style/MethodDefParentheses:
Enabled: true

Style/SpaceBeforeBlockBraces:
Layout/SpaceBeforeBlockBraces:
Enabled: true

Style/SpaceInsideBlockBraces:
Layout/SpaceInsideBlockBraces:
Enabled: true

Style/SpaceInsideParens:
Layout/SpaceInsideParens:
Enabled: true

Style/DeprecatedHashMethods:
Style/PreferredHashMethods:
Enabled: true

Style/HashSyntax:
Enabled: true

Style/SpaceInsideHashLiteralBraces:
Layout/SpaceInsideHashLiteralBraces:
Enabled: true
EnforcedStyle: no_space

Style/SpaceInsideBrackets:
Enabled: true

Style/AndOr:
Enabled: false

Style/TrailingCommaInLiteral:
Style/TrailingCommaInArrayLiteral:
Enabled: true
EnforcedStyleForMultiline: consistent_comma

Style/SpaceBeforeComma:
Style/TrailingCommaInHashLiteral:
Enabled: true
EnforcedStyleForMultiline: consistent_comma

Style/SpaceBeforeComment:
Layout/SpaceBeforeComma:
Enabled: true

Style/SpaceBeforeSemicolon:
Layout/SpaceBeforeComment:
Enabled: true

Style/SpaceAroundBlockParameters:
Layout/SpaceBeforeSemicolon:
Enabled: true

Style/SpaceAroundOperators:
Layout/SpaceAroundBlockParameters:
Enabled: true

Style/SpaceAfterColon:
Layout/SpaceAroundOperators:
Enabled: true

Style/SpaceAfterComma:
Layout/SpaceAfterColon:
Enabled: true

Style/SpaceAroundKeyword:
Layout/SpaceAfterComma:
Enabled: true

Style/SpaceAfterNot:
Layout/SpaceAroundKeyword:
Enabled: true

Style/SpaceAfterSemicolon:
Layout/SpaceAfterNot:
Enabled: true

Lint/UselessComparison:
Layout/SpaceAfterSemicolon:
Enabled: true

Lint/InvalidCharacterLiteral:
Lint/BinaryOperatorWithIdenticalOperands:
Enabled: true

Lint/LiteralInInterpolation:
Enabled: true

Lint/LiteralInCondition:
Lint/LiteralAsCondition:
Enabled: true

Lint/UnusedBlockArgument:
Expand All @@ -134,19 +135,19 @@ Style/ParenthesesAroundCondition:
Style/WhileUntilDo:
Enabled: true

Style/EmptyLineBetweenDefs:
Layout/EmptyLineBetweenDefs:
Enabled: true

Style/EmptyLinesAroundAccessModifier:
Layout/EmptyLinesAroundAccessModifier:
Enabled: true

Style/EmptyLinesAroundMethodBody:
Layout/EmptyLinesAroundMethodBody:
Enabled: true

Style/ColonMethodCall:
Enabled: true

Lint/SpaceBeforeFirstArg:
Layout/SpaceBeforeFirstArg:
Enabled: true

Lint/UnreachableCode:
Expand All @@ -165,7 +166,7 @@ Style/StringLiterals:
Metrics/CyclomaticComplexity:
Max: 10

Metrics/LineLength:
Layout/LineLength:
Max: 128

Metrics/MethodLength:
Expand All @@ -174,8 +175,18 @@ Metrics/MethodLength:
Metrics/PerceivedComplexity:
Max: 8

Naming/BlockForwarding:
EnforcedStyle: explicit

Metrics/ModuleLength:
Exclude:
- "test/**/*"

# Disabled

Style/Documentation:
Enabled: false

Style/EvenOdd:
Enabled: false

Expand All @@ -185,7 +196,7 @@ Style/AsciiComments:
Style/NumericLiterals:
Enabled: false

Style/UnneededPercentQ:
Style/RedundantPercentQ:
Enabled: false

Style/SpecialGlobalVars:
Expand All @@ -206,14 +217,5 @@ Metrics/BlockNesting:
Metrics/ClassLength:
Enabled: false

Metrics/MethodLength:
Enabled: false

Metrics/ParameterLists:
Enabled: false

Metrics/PerceivedComplexity:
Enabled: false

Style/Documentation:
Enabled: false
7 changes: 7 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# frozen_string_literal: true

source "https://rubygems.org"

gem "codeclimate-test-reporter", "~> 0.4.7"
gem "minitest", "~> 5.25"
gem "rake", "~> 13.0"
gem "rubocop", "~> 1.68.0"

gemspec
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "bundler/gem_tasks"
require "rake/testtask"

Expand Down
2 changes: 2 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env ruby

# frozen_string_literal: true

require "bundler/setup"
require "suo"
require "irb"
Expand Down
2 changes: 2 additions & 0 deletions lib/suo.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "securerandom"
require "monitor"

Expand Down
42 changes: 20 additions & 22 deletions lib/suo/client/base.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Suo
module Client
class Base
Expand All @@ -9,14 +11,14 @@ class Base
ttl: 60,
}.freeze

BLANK_STR = "".freeze
BLANK_STR = ""

attr_accessor :client, :key, :resources, :options

include MonitorMixin

def initialize(key, options = {})
fail "Client required" unless options[:client]
raise "Client required" unless options[:client]

@options = DEFAULT_OPTIONS.merge(options)
@retry_count = (@options[:acquisition_timeout] / @options[:acquisition_delay].to_f).ceil
Expand Down Expand Up @@ -46,10 +48,8 @@ def locked?
end

def locks
val, _ = get
cleared_locks = deserialize_and_clear_locks(val)

cleared_locks
val, = get
deserialize_and_clear_locks(val)
end

def refresh(token)
Expand Down Expand Up @@ -81,12 +81,12 @@ def unlock(token)
break unless acquisition_lock
break if set(serialize_locks(cleared_locks), cas, expire: cleared_locks.empty?)
end
rescue LockClientError => _ # rubocop:disable Lint/HandleExceptions
rescue LockClientError => _e
# ignore - assume success due to optimistic locking
end

def clear
fail NotImplementedError
raise NotImplementedError
end

private
Expand Down Expand Up @@ -116,35 +116,33 @@ def acquire_lock(token = nil)
end

def get
fail NotImplementedError
raise NotImplementedError
end

def set(newval, cas) # rubocop:disable Lint/UnusedMethodArgument
fail NotImplementedError
def set(newval, cas)
raise NotImplementedError
end

def initial_set(val = BLANK_STR) # rubocop:disable Lint/UnusedMethodArgument
fail NotImplementedError
def initial_set(val = BLANK_STR)
raise NotImplementedError
end

def synchronize
mon_synchronize { yield }
def synchronize(&block)
mon_synchronize(&block)
end

def retry_with_timeout
def retry_with_timeout(&block)
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)

retry_count.times do
elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
break if elapsed >= options[:acquisition_timeout]

synchronize do
yield
end
synchronize(&block)

sleep(rand(options[:acquisition_delay] * 1000).to_f / 1000)
end
rescue => _
rescue StandardError => _e
raise LockClientError
end

Expand All @@ -157,12 +155,12 @@ def deserialize_and_clear_locks(val)
end

def deserialize_locks(val)
unpacked = (val.nil? || val == BLANK_STR) ? [] : MessagePack.unpack(val)
unpacked = val.nil? || val == BLANK_STR ? [] : MessagePack.unpack(val)

unpacked.map do |time, token|
[Time.at(time), token]
end
rescue EOFError, MessagePack::MalformedFormatError => _
rescue EOFError, MessagePack::MalformedFormatError => _e
[]
end

Expand Down
2 changes: 2 additions & 0 deletions lib/suo/client/memcached.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Suo
module Client
class Memcached < Base
Expand Down
Loading