From 5f6cf05c0affb6614f0c8c5c36cca2bd88dc3a1c Mon Sep 17 00:00:00 2001 From: Morgan Brown Date: Tue, 9 Jul 2024 18:13:17 +0200 Subject: [PATCH] Simplify enabling concurrent_write --- lib/cached_resource/cached_resource.rb | 22 +++++++++++----------- lib/cached_resource/configuration.rb | 6 ++++-- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/lib/cached_resource/cached_resource.rb b/lib/cached_resource/cached_resource.rb index 82ecfd4..22c7bee 100644 --- a/lib/cached_resource/cached_resource.rb +++ b/lib/cached_resource/cached_resource.rb @@ -19,17 +19,17 @@ def cached_resource(options={}) def setup_cached_resource!(options) @cached_resource = CachedResource::Configuration.new(options) # puts "OK WE ARE SET UP!" - if @cached_resource.concurrent_write - @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 + # if @cached_resource.concurrent_write + # @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 end diff --git a/lib/cached_resource/configuration.rb b/lib/cached_resource/configuration.rb index ea532c6..82a0d78 100644 --- a/lib/cached_resource/configuration.rb +++ b/lib/cached_resource/configuration.rb @@ -47,7 +47,8 @@ def initialize(options={}) # Set our concurrent_write. Can't override OpenStruct setters # in a straightforward way. - @concurrent_write = data.delete :concurrent_write + # @concurrent_write = data.delete :concurrent_write + self.concurrent_write = data.delete :concurrent_write super(data) end @@ -68,10 +69,11 @@ def off! self.enabled = false end + # Toggle writing to cache in a thread. Requires + # concurrent/promise if enabled. def concurrent_write=(value) require_concurrent_ruby if value @concurrent_write = value - puts "WE REQUIRED ID!\n", value end # require concurrent/promise, throwing an exception if necessary