cache_keeper 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a594d57869401830f847424c24851913039af6979f2e5f9665d77b96bb2c955e
4
- data.tar.gz: c80f3edb0b84157a3e49ef8438347931c515ecffe2a55c9a08a630b2fca1c6c0
3
+ metadata.gz: 3bef46cb308534d247ff50712366a7548864043aa0bf6cd4a75ccd2edeed1fee
4
+ data.tar.gz: 4c6375f02da64d4d60c38c6a7e2839c0e1d4084271011bc4a8fcd5d83a65caaa
5
5
  SHA512:
6
- metadata.gz: 46806eb4e857047be29c784b9c8455a2ce92ac79ebe19fac9bcec704fa9c29c48b63ebf546e78d615cf8d429044bc7557dbac7e94abc939b1288353f856729b3
7
- data.tar.gz: b1902aa78ec63d686c7435924c412c997828c117cc498a6a474d0b00c3175c621542d5677a74bb97172523d0a24df38ee916ab8c0a0edafc42047a31dfa87a21
6
+ metadata.gz: 27a68b7c00a743092d3b6e317df3c38edae644be451c0760c186b66e648ecca7acccb5b4540369b3885f7e6a9fb9d5c5723e4fcd4f8877d6fc126d6b4853cc0a
7
+ data.tar.gz: 8a9fea0f3c9b654c530e6519bf6ccdbc0fabe7ff0ccd2e33d3efed9dc3272437135afe7ea07a98c86b9e927f01e3cb7c139d92a78828372354a6181ef25b057d
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cache_keeper (0.1.0)
4
+ cache_keeper (0.1.2)
5
5
  rails (>= 6.1.0)
6
6
 
7
7
  GEM
@@ -32,7 +32,7 @@ class CacheKeeper::CachedMethod
32
32
  private
33
33
 
34
34
  def cache_entry
35
- Rails.cache.send :read_entry, Rails.cache.send(:normalize_key, cache_key)
35
+ Rails.cache.send :read_entry, Rails.cache.send(:normalize_key, cache_key, {})
36
36
  end
37
37
 
38
38
  def cache_key
@@ -1,20 +1,22 @@
1
- module CacheKeeper::Caching
2
- extend ActiveSupport::Concern
1
+ module CacheKeeper
2
+ module Caching
3
+ extend ActiveSupport::Concern
3
4
 
4
- included do
5
- def self.caches(*method_names, **options)
6
- method_names.each do |method_name|
7
- CacheKeeper.manager.handle self, method_name, options
5
+ included do
6
+ def self.caches(*method_names, **options)
7
+ method_names.each do |method_name|
8
+ CacheKeeper.manager.handle self, method_name, options
8
9
 
9
- # If the method is already defined, we need to hook it
10
- method_added method_name
10
+ # If the method is already defined, we need to hook it
11
+ method_added method_name
12
+ end
11
13
  end
12
- end
13
14
 
14
- def self.method_added(method_name)
15
- super
15
+ def self.method_added(method_name)
16
+ super
16
17
 
17
- CacheKeeper.manager.activate_if_handling self, method_name
18
+ CacheKeeper.manager.activate_if_handling self, method_name
19
+ end
18
20
  end
19
21
  end
20
22
  end
@@ -1,20 +1,22 @@
1
- class CacheKeeper::Configuration
2
- DEFAULT_MUST_REVALIDATE = false
3
- DEFAULT_QUEUES = {}
1
+ module CacheKeeper
2
+ class Configuration
3
+ DEFAULT_MUST_REVALIDATE = false
4
+ DEFAULT_QUEUES = {}
4
5
 
5
- def must_revalidate
6
- return rails_config[:must_revalidate] unless rails_config[:must_revalidate].nil?
6
+ def must_revalidate
7
+ return rails_config[:must_revalidate] unless rails_config[:must_revalidate].nil?
7
8
 
8
- DEFAULT_MUST_REVALIDATE
9
- end
9
+ DEFAULT_MUST_REVALIDATE
10
+ end
10
11
 
11
- def queues
12
- rails_config[:queues] || DEFAULT_QUEUES
13
- end
12
+ def queues
13
+ rails_config[:queues] || DEFAULT_QUEUES
14
+ end
14
15
 
15
- private
16
+ private
16
17
 
17
- def rails_config
18
- Rails.application.config.cache_keeper
18
+ def rails_config
19
+ Rails.application.config.cache_keeper
20
+ end
19
21
  end
20
22
  end
@@ -1,22 +1,31 @@
1
- class CacheKeeper::Engine < ::Rails::Engine
2
- isolate_namespace CacheKeeper
1
+ module CacheKeeper
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace CacheKeeper
3
4
 
4
- config.cache_keeper = ActiveSupport::OrderedOptions.new
5
- config.cache_keeper.queues = ActiveSupport::InheritableOptions.new
5
+ config.cache_keeper = ActiveSupport::OrderedOptions.new
6
+ config.cache_keeper.queues = ActiveSupport::InheritableOptions.new
6
7
 
7
- initializer "cache_keeper.active_job_serializer" do |app|
8
- config.to_prepare do
9
- Rails.application.config.active_job.custom_serializers << CacheKeeper::CachedMethodSerializer
10
- end
11
- end
8
+ config.eager_load_namespaces << CacheKeeper
9
+ config.autoload_once_paths = %W(
10
+ #{root}/app/jobs
11
+ #{root}/app/models
12
+ #{root}/app/serializers
13
+ )
12
14
 
13
- initializer "cache_keeper.caching_methods" do |app|
14
- ActiveSupport.on_load :action_controller do
15
- ActionController::Base.send :include, CacheKeeper::Caching
15
+ initializer "cache_keeper.active_job_serializer" do |app|
16
+ config.to_prepare do
17
+ Rails.application.config.active_job.custom_serializers << CacheKeeper::CachedMethodSerializer
18
+ end
16
19
  end
17
20
 
18
- ActiveSupport.on_load :active_record do
19
- include CacheKeeper::Caching
21
+ initializer "cache_keeper.caching_methods" do |app|
22
+ ActiveSupport.on_load :action_controller do
23
+ ActionController::Base.send :include, CacheKeeper::Caching
24
+ end
25
+
26
+ ActiveSupport.on_load :active_record do
27
+ include CacheKeeper::Caching
28
+ end
20
29
  end
21
30
  end
22
31
  end
@@ -1,42 +1,44 @@
1
- class CacheKeeper::Manager
2
- attr_accessor :cached_methods
1
+ module CacheKeeper
2
+ class Manager
3
+ attr_accessor :cached_methods
3
4
 
4
- def initialize
5
- self.cached_methods = []
6
- end
5
+ def initialize
6
+ self.cached_methods = []
7
+ end
7
8
 
8
- def find(klass, method_name)
9
- cached_methods.find do |cached_method|
10
- cached_method.klass == klass && cached_method.method_name == method_name
9
+ def find(klass, method_name)
10
+ cached_methods.find do |cached_method|
11
+ cached_method.klass == klass && cached_method.method_name == method_name
12
+ end
11
13
  end
12
- end
13
14
 
14
- def handled?(klass, method_name)
15
- find(klass, method_name).present?
16
- end
15
+ def handled?(klass, method_name)
16
+ find(klass, method_name).present?
17
+ end
17
18
 
18
- def handle(klass, method_name, options)
19
- CacheKeeper::CachedMethod.new(klass, method_name, options).tap do |cached_method|
20
- cached_methods << cached_method
19
+ def handle(klass, method_name, options)
20
+ CacheKeeper::CachedMethod.new(klass, method_name, options).tap do |cached_method|
21
+ cached_methods << cached_method
22
+ end
21
23
  end
22
- end
23
24
 
24
- def activate_if_handling(klass, method_name)
25
- cached_method = find(klass, method_name) or return
25
+ def activate_if_handling(klass, method_name)
26
+ cached_method = find(klass, method_name) or return
26
27
 
27
- return unless requires_activation?(cached_method)
28
+ return unless requires_activation?(cached_method)
28
29
 
29
- CacheKeeper::ReplaceMethod.replace(cached_method) do
30
- cached_method.call(self)
30
+ CacheKeeper::ReplaceMethod.replace(cached_method) do
31
+ cached_method.call(self)
32
+ end
31
33
  end
32
- end
33
34
 
34
- private
35
+ private
35
36
 
36
- def requires_activation?(cached_method)
37
- return false if cached_method.klass.instance_methods.exclude?(cached_method.method_name) && cached_method.klass.private_instance_methods.exclude?(cached_method.method_name)
38
- return false if cached_method.klass.private_instance_methods.include?(cached_method.alias_for_original_method)
37
+ def requires_activation?(cached_method)
38
+ return false if cached_method.klass.instance_methods.exclude?(cached_method.method_name) && cached_method.klass.private_instance_methods.exclude?(cached_method.method_name)
39
+ return false if cached_method.klass.private_instance_methods.include?(cached_method.alias_for_original_method)
39
40
 
40
- true
41
+ true
42
+ end
41
43
  end
42
44
  end
@@ -1,46 +1,48 @@
1
1
  # Based on https://github.com/Shopify/sorbet/blob/master/gems/sorbet-runtime/lib/types/private/class_utils.rb
2
- class CacheKeeper::ReplaceMethod
3
- class << self
4
- def replace(cached_method, &block)
5
- klass = cached_method.klass
6
- method_name = cached_method.method_name
7
- alias_for_original_method = cached_method.alias_for_original_method
8
- original_visibility = visibility_method_name(klass, method_name)
2
+ module CacheKeeper
3
+ class ReplaceMethod
4
+ class << self
5
+ def replace(cached_method, &block)
6
+ klass = cached_method.klass
7
+ method_name = cached_method.method_name
8
+ alias_for_original_method = cached_method.alias_for_original_method
9
+ original_visibility = visibility_method_name(klass, method_name)
9
10
 
10
- define_method_with_visibility klass, method_name, alias_for_original_method, original_visibility, &block
11
- end
11
+ define_method_with_visibility klass, method_name, alias_for_original_method, original_visibility, &block
12
+ end
12
13
 
13
- private
14
+ private
14
15
 
15
- # `name` must be an instance method (for class methods, pass in mod.singleton_class)
16
- def visibility_method_name(klass, method_name)
17
- if klass.public_method_defined? method_name
18
- :public
19
- elsif klass.protected_method_defined? method_name
20
- :protected
21
- elsif klass.private_method_defined? method_name
22
- :private
23
- else
24
- raise NameError.new("undefined method `#{method_name}` for `#{klass}`")
16
+ # `name` must be an instance method (for class methods, pass in mod.singleton_class)
17
+ def visibility_method_name(klass, method_name)
18
+ if klass.public_method_defined? method_name
19
+ :public
20
+ elsif klass.protected_method_defined? method_name
21
+ :protected
22
+ elsif klass.private_method_defined? method_name
23
+ :private
24
+ else
25
+ raise NameError.new("undefined method `#{method_name}` for `#{klass}`")
26
+ end
25
27
  end
26
- end
27
28
 
28
- def define_method_with_visibility(klass, method_name, alias_for_original_method, visibility, &block)
29
- klass.module_exec do
30
- alias_method alias_for_original_method, method_name
31
- private alias_for_original_method
29
+ def define_method_with_visibility(klass, method_name, alias_for_original_method, visibility, &block)
30
+ klass.module_exec do
31
+ alias_method alias_for_original_method, method_name
32
+ private alias_for_original_method
32
33
 
33
- # Start a visibility (public/protected/private) region, so that
34
- # all of the method redefinitions happen with the right visibility
35
- # from the beginning. This ensures that any other code that is
36
- # triggered by `method_added`, sees the redefined method with the
37
- # right visibility.
38
- send visibility
34
+ # Start a visibility (public/protected/private) region, so that
35
+ # all of the method redefinitions happen with the right visibility
36
+ # from the beginning. This ensures that any other code that is
37
+ # triggered by `method_added`, sees the redefined method with the
38
+ # right visibility.
39
+ send visibility
39
40
 
40
- define_method method_name, &block
41
+ define_method method_name, &block
41
42
 
42
- if block && block.arity < 0 && respond_to?(:ruby2_keywords, true)
43
- ruby2_keywords method_name
43
+ if block && block.arity < 0 && respond_to?(:ruby2_keywords, true)
44
+ ruby2_keywords method_name
45
+ end
44
46
  end
45
47
  end
46
48
  end
@@ -1,3 +1,3 @@
1
1
  module CacheKeeper
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.2"
3
3
  end
data/lib/cache_keeper.rb CHANGED
@@ -1,11 +1,13 @@
1
1
  require "cache_keeper/engine"
2
2
 
3
- require "cache_keeper/caching"
4
- require "cache_keeper/configuration"
5
- require "cache_keeper/manager"
6
- require "cache_keeper/replace_method"
7
-
8
3
  module CacheKeeper
4
+ extend ActiveSupport::Autoload
5
+
6
+ autoload :Caching
7
+ autoload :Configuration
8
+ autoload :Manager
9
+ autoload :ReplaceMethod
10
+
9
11
  mattr_accessor :logger, default: ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new($stdout))
10
12
 
11
13
  mattr_accessor :configuration, default: CacheKeeper::Configuration.new
data/test/manager_test.rb CHANGED
@@ -2,6 +2,9 @@ require "test_helper"
2
2
 
3
3
  class CacheKeeper::ManagerTest < ActiveSupport::TestCase
4
4
  test "keeps record of the cached methods" do
5
+ # Load model
6
+ Recording
7
+
5
8
  assert_equal 1, CacheKeeper.manager.cached_methods.count
6
9
  assert_equal :slow_method, CacheKeeper.manager.cached_methods.first.method_name
7
10
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cache_keeper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Zamuner