Skip to content

Commit

Permalink
Require concurrent/ruby when concurrent_write changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Morgan Brown committed Jul 9, 2024
1 parent dbd8fcc commit 48f7174
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
17 changes: 9 additions & 8 deletions lib/cached_resource/cached_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ def cached_resource(options={})
def setup_cached_resource!(options)
@cached_resource = CachedResource::Configuration.new(options)
if @cached_resource.concurrent_write
begin
send :require, 'concurrent/promise'
rescue LoadError
@cached_resource.logger.error(
"`concurrent_write` option is enabled, but `concurrent-ruby` is not an installed dependency"
)
raise
end
@cached_resource.require_concurrent_ruby
# begin
# send :require, 'concurrent/promise'
# rescue LoadError
# @cached_resource.logger.error(
# "`concurrent_write` option is enabled, but `concurrent-ruby` is not an installed dependency"
# )
# raise
# end
end
send :include, CachedResource::Caching
@cached_resource
Expand Down
19 changes: 19 additions & 0 deletions lib/cached_resource/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,25 @@ def off!
self.enabled = false
end

def concurrent_write=(value)
super(value)

if value
self.require_concurrent_ruby
end
end

def require_concurrent_ruby
begin
send :require, 'concurrent/promise'
rescue LoadError
@cached_resource.logger.error(
"`concurrent_write` option is enabled, but `concurrent-ruby` is not an installed dependency"
)
raise
end
end

private

# Get a randomized ttl value between ttl * ttl_randomization_scale begin
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'active_resource'
require 'active_support'
require 'active_support/time'
require 'concurrent/promise'
# require 'concurrent/promise'

$:.unshift(File.dirname(__FILE__) + '/../lib')
require 'cached_resource'
Expand Down

0 comments on commit 48f7174

Please sign in to comment.