active_cached_resource 0.1.2 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ea5e9eb3e2aafbeefeb5453054237509459e44b440935b6565c382131abc4852
4
- data.tar.gz: c4d901d53c3802d7c3aed95019b9274f2bf75de1c6a604431db17354a7d99514
3
+ metadata.gz: fcd11f4ab22e963e432b2130606aa5f56102a284b86752847ea1f2b3c2ca8407
4
+ data.tar.gz: 87a5cfc1d2a99e4de13748937ec4649c9814c850008f4ebc6246c2f9ad35c746
5
5
  SHA512:
6
- metadata.gz: 405fe2f90ddb601bfbc717e0bf571e3da7b42598feee52895edbd6a9baf4ba75fe70b7a3d5cf4d95bcd00e3892735f42423d49d3b84dd32b4819336c6efed47e
7
- data.tar.gz: 2536aaf04b87746d3cf344d8578e6bb6e83e19d1640a7366a29996b9ed52ca88e59dce54b6b2e90be60fc56d1b7361dc72b9dc6073585ed347372cba2e55d2bf
6
+ metadata.gz: 2cba101b3f9755198b284ba74a1a2644ac4f4eeca7848460db78ff9db0a41163d45f1eb1d300f14460cdde6081db0007938dc47f1524f01e4c5b5b35c27ffe94
7
+ data.tar.gz: d15d419528bc2793104bb951bdc12665ded24a17d075e6afc19fd70ad9ade8d0ac42f60517a14316bb912df03e384e1e2b544097fb4ab9e9ce68414183f4dde8
data/CHANGELOG.md CHANGED
@@ -1,10 +1,23 @@
1
- ## [Unreleased]
1
+ ## [0.1.4] - 2024-12-20
2
+ - CI Improvements
3
+ - Added annotations
4
+ - Runs tests from ActiveResource
5
+ - Separated Collection caching logic into ActiveCachedResource::Collection as opposed to monkey patching ActiveResource::Collection
6
+ - Changed name of SQL adapter from `active_record` to `active_record_sql`
7
+ - Changed method name of ActiveCachedResource::Model from `clear` to `clear_cache`
8
+
9
+ ## [0.1.3] - 2024-12-19
10
+ - Minor patch on ActiveResource. Removed deprecator log.
11
+
12
+ ## [0.1.2] - 2024-12-19
13
+ - Minor patch on ActiveResource::Collection initializing attributes.
2
14
 
3
15
  ## [0.1.1] - 2024-12-17
4
16
 
5
17
  - Added ruby yard documentation
6
18
  - Added LICENSE
7
19
  - Improved gemspec
20
+ - Changed name of ActiveSupport::Cache adapter from `active_support` to `active_support_cache`
8
21
 
9
22
 
10
23
  ## [0.1.0] - 2024-12-16
@@ -1,3 +1,5 @@
1
+ require_relative "collection"
2
+
1
3
  module ActiveCachedResource
2
4
  module Caching
3
5
  GLOBAL_PREFIX = "acr"
@@ -9,6 +11,10 @@ module ActiveCachedResource
9
11
  class << self
10
12
  alias_method :find_without_cache, :find
11
13
  alias_method :find, :find_with_cache
14
+
15
+ def collection_parser
16
+ _collection_parser || ActiveCachedResource::Collection
17
+ end
12
18
  end
13
19
  end
14
20
 
@@ -119,7 +125,7 @@ module ActiveCachedResource
119
125
  should_reload = options.delete(:reload) || !cached_resource.enabled
120
126
 
121
127
  # When bypassing cache, include the reload option as a query parameter for collection requests.
122
- # Hacky but this way ActiveResource::Collection#request_resources! can access it
128
+ # Hacky but this way ActiveCachedResource::Collection#request_resources! can access it
123
129
  if should_reload && args.first == :all
124
130
  options[:params] = {} if options[:params].blank?
125
131
  options[:params][RELOAD_PARAM] = should_reload
@@ -127,7 +133,7 @@ module ActiveCachedResource
127
133
  end
128
134
 
129
135
  if args.first == :all
130
- # Let ActiveResource::Collection handle the caching so that lazy loading is more effective
136
+ # Let ActiveCachedResource::Collection handle the caching so that lazy loading is more effective
131
137
  return find_via_reload(*args)
132
138
  end
133
139
 
@@ -139,7 +145,7 @@ module ActiveCachedResource
139
145
  # This method clears all cached entries that match the cache key prefix.
140
146
  #
141
147
  # @return [void]
142
- def clear
148
+ def clear_cache
143
149
  cached_resource.cache.clear("#{cache_key_prefix}/")
144
150
  end
145
151
 
@@ -189,16 +195,16 @@ module ActiveCachedResource
189
195
 
190
196
  # Determines if the given object should be cached.
191
197
  #
192
- # @param object [Object, ActiveResource::Collection] The object to check for caching eligibility.
198
+ # @param object [Object, ActiveCachedResource::Collection] The object to check for caching eligibility.
193
199
  # @return [Boolean] Returns true if the object should be cached, false otherwise.
194
200
  def should_cache?(object)
195
201
  return false unless cached_resource.enabled
196
202
 
197
203
  # Calling `present?` on the `collection_parser`, an instance or descendent of
198
- # `ActiveResource::Collection` will trigger a request.
204
+ # `ActiveCachedResource::Collection` will trigger a request.
199
205
  # Checking if `requested?` first, will prevent an unnecessary network request when calling `present?`.
200
206
  case object
201
- when ActiveResource::Collection
207
+ when ActiveCachedResource::Collection
202
208
  object.requested? && object.present?
203
209
  else
204
210
  object.present?
@@ -0,0 +1,28 @@
1
+ module ActiveCachedResource
2
+ class Collection < ActiveResource::Collection
3
+ private
4
+
5
+ def request_resources!
6
+ return @elements if requested? || resource_class.nil?
7
+
8
+ # Delete the reload param from query params.
9
+ # This is drilled down via `params` option to determine if the collection should be reloaded
10
+ should_reload = query_params.delete(ActiveCachedResource::Caching::RELOAD_PARAM)
11
+ if !should_reload
12
+ from_cache = resource_class.send(:cache_read, from, path_params, query_params, prefix_options)
13
+ @elements = from_cache
14
+ return @elements if @elements
15
+ end
16
+
17
+ super # This sets @elements
18
+
19
+ if resource_class.send(:should_cache?, @elements)
20
+ resource_class.send(:cache_write, @elements, from, path_params, query_params, prefix_options)
21
+ end
22
+
23
+ @elements
24
+ ensure
25
+ @requested = true
26
+ end
27
+ end
28
+ end
@@ -7,7 +7,7 @@ require_relative "caching_strategies/base"
7
7
  module ActiveCachedResource
8
8
  class Configuration < OpenStruct
9
9
  CACHING_STRATEGIES = {
10
- active_record: ActiveCachedResource::CachingStrategies::SQLCache,
10
+ active_record_sql: ActiveCachedResource::CachingStrategies::SQLCache,
11
11
  active_support_cache: ActiveCachedResource::CachingStrategies::ActiveSupportCache
12
12
  }
13
13
 
@@ -18,7 +18,7 @@ module ActiveCachedResource
18
18
  # @param model [Class] The model class for which the configuration is being set.
19
19
  # @param options [Hash] A hash of options to customize the configuration.
20
20
  # @option options [Symbol] :cache_store The cache store to be used.
21
- # @option options [Symbol] :cache_strategy The cache strategy to be used. One of :active_record or :active_support_cache.
21
+ # @option options [Symbol] :cache_strategy The cache strategy to be used. One of :active_record_sql or :active_support_cache.
22
22
  # @option options [String] :cache_key_prefix The prefix for cache keys (default: model name underscored).
23
23
  # @option options [Logger] :logger The logger instance to be used (default: ActiveCachedResource::Logger).
24
24
  # @option options [Boolean] :enabled Whether caching is enabled (default: true).
@@ -15,7 +15,7 @@ module ActiveCachedResource
15
15
  #
16
16
  # @param options [Hash] A hash of options to customize the configuration.
17
17
  # @option options [Symbol] :cache_store The cache store to be used. Must be a CachingStrategies::Base instance.
18
- # @option options [Symbol] :cache_strategy The cache strategy to be used. One of :active_record or :active_support_cache.
18
+ # @option options [Symbol] :cache_strategy The cache strategy to be used. One of :active_record_sql or :active_support_cache.
19
19
  # @option options [String] :cache_key_prefix The prefix for cache keys (default: model name underscored).
20
20
  # @option options [Logger] :logger The logger instance to be used (default: ActiveCachedResource::Logger).
21
21
  # @option options [Boolean] :enabled Whether caching is enabled (default: true).
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveCachedResource
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.4"
5
5
  end
@@ -12,53 +12,4 @@ module ActiveResource
12
12
  class Base
13
13
  include ActiveCachedResource::Model
14
14
  end
15
-
16
- class Collection
17
- private
18
-
19
- # Monkey patch ActiveResource::Collection to handle caching
20
- # @see lib/activeresource/lib/active_resource/collection.rb
21
- def request_resources!
22
- return @elements if requested?
23
-
24
- # MONKEY PATCH
25
- # Delete the reload param from query params.
26
- # This is drilled down via `params` option to determine if the collection should be reloaded
27
- should_reload = query_params.delete(ActiveCachedResource::Caching::RELOAD_PARAM)
28
- if !should_reload
29
- from_cache = resource_class.send(:cache_read, from, path_params, query_params, prefix_options)
30
- @elements = from_cache
31
- return @elements if @elements
32
- end
33
- # MONKEY PATCH
34
-
35
- response =
36
- case from
37
- when Symbol
38
- resource_class.get(from, path_params)
39
- when String
40
- path = "#{from}#{query_string(query_params)}"
41
- resource_class.format.decode(resource_class.connection.get(path, resource_class.headers).body)
42
- else
43
- path = resource_class.collection_path(prefix_options, query_params)
44
- resource_class.format.decode(resource_class.connection.get(path, resource_class.headers).body)
45
- end
46
-
47
- # Update the elements
48
- parse_response(response)
49
- @elements.map! { |e| resource_class.instantiate_record(e, prefix_options) }
50
-
51
- # MONKEY PATCH
52
- # Write cache
53
- resource_class.send(:cache_write, @elements, from, path_params, query_params, prefix_options)
54
- @elements
55
- # MONKEY PATCH
56
- rescue ActiveResource::ResourceNotFound
57
- # Swallowing ResourceNotFound exceptions and return nothing - as per ActiveRecord.
58
- # Needs to be empty array as Array methods are delegated
59
- []
60
- ensure
61
- @requested = true
62
- end
63
- end
64
15
  end
@@ -202,7 +202,7 @@ module ActiveResource # :nodoc:
202
202
  #
203
203
  # [Array<Object>] The collection of resources retrieved from the API.
204
204
  def request_resources!
205
- return @elements if requested?
205
+ return @elements if requested? || resource_class.nil?
206
206
  response =
207
207
  case from
208
208
  when Symbol
@@ -21,11 +21,5 @@ module ActiveResource
21
21
  app.config.active_job.custom_serializers << ActiveResource::ActiveJobSerializer
22
22
  end
23
23
  end
24
-
25
- initializer "active_resource.deprecator" do |app|
26
- if app.respond_to?(:deprecators)
27
- app.deprecators[:active_resource] = ActiveResource.deprecator
28
- end
29
- end
30
24
  end
31
25
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_cached_resource
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.4
5
5
  platform: ruby
6
- original_platform: ''
7
6
  authors:
8
7
  - Jean Luis Urena
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2024-12-19 00:00:00.000000000 Z
10
+ date: 2024-12-22 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: activemodel-serializers-xml
@@ -86,6 +85,7 @@ files:
86
85
  - lib/active_cached_resource/caching_strategies/active_support_cache.rb
87
86
  - lib/active_cached_resource/caching_strategies/base.rb
88
87
  - lib/active_cached_resource/caching_strategies/sql_cache.rb
88
+ - lib/active_cached_resource/collection.rb
89
89
  - lib/active_cached_resource/configuration.rb
90
90
  - lib/active_cached_resource/logger.rb
91
91
  - lib/active_cached_resource/model.rb