Skip to content

Commit

Permalink
feat: use zeitwerk loader instead of ActiveSupport::Autoload
Browse files Browse the repository at this point in the history
  • Loading branch information
seuros committed Jan 21, 2024
1 parent 01ddea4 commit b5082fd
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 16 deletions.
17 changes: 8 additions & 9 deletions lib/with_advisory_lock.rb
@@ -1,16 +1,15 @@
require 'with_advisory_lock/version'
require 'active_support'
require_relative 'with_advisory_lock/failed_to_acquire_lock'
require 'zeitwerk'

module WithAdvisoryLock
extend ActiveSupport::Autoload
loader = Zeitwerk::Loader.for_gem
loader.inflector.inflect(
'mysql' => 'MySQL',
'postgresql' => 'PostgreSQL',
)
loader.setup

autoload :Concern
autoload :Base
autoload :DatabaseAdapterSupport
autoload :Flock
autoload :MySQL, 'with_advisory_lock/mysql'
autoload :PostgreSQL, 'with_advisory_lock/postgresql'
module WithAdvisoryLock
end

ActiveSupport.on_load :active_record do
Expand Down
2 changes: 1 addition & 1 deletion lib/with_advisory_lock/base.rb
Expand Up @@ -79,7 +79,7 @@ def stable_hashcode(input)
else
# Ruby MRI's String#hash is randomly seeded as of Ruby 1.9 so
# make sure we use a deterministic hash.
Zlib.crc32(input.to_s)
Zlib.crc32(input.to_s, 0)
end
end

Expand Down
6 changes: 1 addition & 5 deletions lib/with_advisory_lock/concern.rb
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require 'active_support/concern'

module WithAdvisoryLock
module Concern
extend ActiveSupport::Concern
Expand All @@ -15,9 +13,7 @@ def with_advisory_lock(lock_name, options = {}, &block)

def with_advisory_lock!(lock_name, options = {}, &block)
result = with_advisory_lock_result(lock_name, options, &block)
unless result.lock_was_acquired?
raise WithAdvisoryLock::FailedToAcquireLock, lock_name
end
raise WithAdvisoryLock::FailedToAcquireLock, lock_name unless result.lock_was_acquired?

result.result
end
Expand Down
4 changes: 3 additions & 1 deletion lib/with_advisory_lock/failed_to_acquire_lock.rb
@@ -1,7 +1,9 @@
# frozen_string_literal: true

module WithAdvisoryLock
class FailedToAcquireLock < StandardError
def initialize(lock_name)
super("Failed to acquire lock #{lock_name}")
end
end
end
end
1 change: 1 addition & 0 deletions with_advisory_lock.gemspec
Expand Up @@ -25,6 +25,7 @@ Gem::Specification.new do |spec|
spec.metadata['changelog_uri'] = 'https://github.com/ClosureTree/with_advisory_lock/blob/master/CHANGELOG.md'

spec.add_runtime_dependency 'activerecord', '>= 6.1'
spec.add_runtime_dependency 'zeitwerk', '>= 2.6'

spec.add_development_dependency 'appraisal'
spec.add_development_dependency 'maxitest'
Expand Down

0 comments on commit b5082fd

Please sign in to comment.