active_remote-cached 0.1.3 → 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
  SHA1:
3
- metadata.gz: 81e9acd882928c8bbbef788810301b195c2400eb
4
- data.tar.gz: ac45bdd85f22bfa2185451e4bf0bfb2c03410510
3
+ metadata.gz: da0c6caa85c4c4bc50f0df7e6b3315783b546628
4
+ data.tar.gz: 408733b9f1a0708bbd7fb8289df0d55c454efe1c
5
5
  SHA512:
6
- metadata.gz: e24999c6d62259b1f88f625cc8ae7bdb96d2ae0c5bd3d0beca2418729823d3d8b2fbdd4581b17a27ecf82ca7ed5831975b67167639b158d24986f36d1a2f3497
7
- data.tar.gz: b699749dbd2d7d77cf9b4851d460c9d76b7bb2ac96374b9874001c950a7d45550ce4b3efd49a0e0e956592ef8780bcd507bf7e7e13cfebe5f8a7e3c59428476e
6
+ metadata.gz: 535e2f65f6933679c321832536c61d3048399ea18899403d090f8dfe5f74c885b45e13bac36933e4b7732a05d106e6e277bf391539f2ff40dbb145c4476b0622
7
+ data.tar.gz: 240613cf4187528adf150f8ff7b6e3ab111b12653b4fdf45ca62e635a66566635a6d9a285a1246758b3c00ebc84677e49b1a510059da52e3e0281c5e151250ca
@@ -3,6 +3,7 @@ require "active_support/cache"
3
3
  require "active_support/concern"
4
4
  require "active_support/core_ext/array/extract_options"
5
5
 
6
+ require "active_remote/cached/argument_keys"
6
7
  require "active_remote/cached/cache"
7
8
  require "active_remote/cached/version"
8
9
  require "active_remote/errors"
@@ -142,8 +143,19 @@ module ActiveRemote
142
143
  def self.#{method_name}(#{expanded_method_args}, __active_remote_cached_options = {})
143
144
  __active_remote_cached_options = ::ActiveRemote::Cached.default_options.merge(#{cached_finder_options}).merge(__active_remote_cached_options)
144
145
  namespace = __active_remote_cached_options.delete(:namespace)
145
- find_cache_key = [namespace, name, "#find", #{sorted_method_args}].compact
146
- search_cache_key = [namespace, name, "#search", #{sorted_method_args}].compact
146
+ find_cache_key = [
147
+ namespace,
148
+ name,
149
+ "#find",
150
+ ::ActiveRemote::Cached::ArgumentKeys.new(#{sorted_method_args}, __active_remote_cached_options).cache_key
151
+ ].compact
152
+
153
+ search_cache_key = [
154
+ namespace,
155
+ name,
156
+ "#search",
157
+ ::ActiveRemote::Cached::ArgumentKeys.new(#{sorted_method_args}, __active_remote_cached_options).cache_key
158
+ ].compact
147
159
 
148
160
  ::ActiveRemote::Cached.cache.delete(find_cache_key)
149
161
  ::ActiveRemote::Cached.cache.delete(search_cache_key)
@@ -163,7 +175,12 @@ module ActiveRemote
163
175
  def self.#{method_name}(#{expanded_method_args}, __active_remote_cached_options = {})
164
176
  __active_remote_cached_options = ::ActiveRemote::Cached.default_options.merge(#{cached_finder_options}).merge(__active_remote_cached_options)
165
177
  namespace = __active_remote_cached_options.delete(:namespace)
166
- cache_key = [namespace, name, "#find", #{sorted_method_args}].compact
178
+ cache_key = [
179
+ namespace,
180
+ name,
181
+ "#find",
182
+ ::ActiveRemote::Cached::ArgumentKeys.new(#{sorted_method_args}, __active_remote_cached_options).cache_key
183
+ ].compact
167
184
 
168
185
  ::ActiveRemote::Cached.cache.exist?(cache_key)
169
186
  end
@@ -184,7 +201,12 @@ module ActiveRemote
184
201
  def self.#{method_name}(#{expanded_method_args}, __active_remote_cached_options = {})
185
202
  __active_remote_cached_options = ::ActiveRemote::Cached.default_options.merge(#{cached_finder_options}).merge(__active_remote_cached_options)
186
203
  namespace = __active_remote_cached_options.delete(:namespace)
187
- cache_key = [namespace, name, "#search", #{sorted_method_args}].compact
204
+ cache_key = [
205
+ namespace,
206
+ name,
207
+ "#search",
208
+ ::ActiveRemote::Cached::ArgumentKeys.new(#{sorted_method_args}, __active_remote_cached_options).cache_key
209
+ ].compact
188
210
 
189
211
  ::ActiveRemote::Cached.cache.exist?(cache_key)
190
212
  end
@@ -218,7 +240,12 @@ module ActiveRemote
218
240
  def self.#{method_name}(#{expanded_method_args}, __active_remote_cached_options = {})
219
241
  __active_remote_cached_options = ::ActiveRemote::Cached.default_options.merge(#{cached_finder_options}).merge(__active_remote_cached_options)
220
242
  namespace = __active_remote_cached_options.delete(:namespace)
221
- cache_key = [namespace, name, "#find", #{sorted_method_args}].compact
243
+ cache_key = [
244
+ namespace,
245
+ name,
246
+ "#find",
247
+ ::ActiveRemote::Cached::ArgumentKeys.new(#{sorted_method_args}, __active_remote_cached_options).cache_key
248
+ ].compact
222
249
 
223
250
  ::ActiveRemote::Cached.cache.fetch(cache_key, __active_remote_cached_options) do
224
251
  if block_given?
@@ -260,7 +287,12 @@ module ActiveRemote
260
287
  def self.#{method_name}(#{expanded_method_args}, __active_remote_cached_options = {})
261
288
  __active_remote_cached_options = ::ActiveRemote::Cached.default_options.merge(#{cached_finder_options}).merge(__active_remote_cached_options)
262
289
  namespace = __active_remote_cached_options.delete(:namespace)
263
- cache_key = [namespace, name, "#search", #{sorted_method_args}].compact
290
+ cache_key = [
291
+ namespace,
292
+ name,
293
+ "#search",
294
+ ::ActiveRemote::Cached::ArgumentKeys.new(#{sorted_method_args}, __active_remote_cached_options).cache_key
295
+ ].compact
264
296
 
265
297
  ::ActiveRemote::Cached.cache.fetch(cache_key, __active_remote_cached_options) do
266
298
  if block_given?
@@ -307,7 +339,12 @@ module ActiveRemote
307
339
  def self.#{method_name}(#{expanded_method_args}, __active_remote_cached_options = {})
308
340
  __active_remote_cached_options = ::ActiveRemote::Cached.default_options.merge(#{cached_finder_options}).merge(__active_remote_cached_options)
309
341
  namespace = __active_remote_cached_options.delete(:namespace)
310
- cache_key = [namespace, name, "#search", #{sorted_method_args}].compact
342
+ cache_key = [
343
+ namespace,
344
+ name,
345
+ "#search",
346
+ ::ActiveRemote::Cached::ArgumentKeys.new(#{sorted_method_args}, __active_remote_cached_options).cache_key
347
+ ].compact
311
348
 
312
349
  ::ActiveRemote::Cached.cache.fetch(cache_key, __active_remote_cached_options) do
313
350
  results = []
@@ -0,0 +1,57 @@
1
+ module ActiveRemote::Cached
2
+ class ArgumentKeys
3
+ attr_reader :arguments, :argument_string, :options
4
+
5
+ REMOVE_CHARACTERS = /[[:space:]+=><{}\[\];:\-,]/
6
+ REPLACE_MAP = [
7
+ [" ", "SP"],
8
+ ["+", "PL"],
9
+ ["=", "EQ"],
10
+ [">", "GT"],
11
+ ["<", "LT"],
12
+ ["{", "LB"],
13
+ ["}", "RB"],
14
+ ["[", "LB2"],
15
+ ["]", "RB2"],
16
+ [";", "SC"],
17
+ [":", "CO"],
18
+ ["-", "DA"],
19
+ [",", "COM"],
20
+ ].freeze
21
+
22
+ def initialize(*arguments, options)
23
+ @options = options
24
+ @arguments = arguments.flatten.compact
25
+ @argument_string = ""
26
+
27
+ @arguments.each do |argument|
28
+ @argument_string << "#{argument}"
29
+ end
30
+ end
31
+
32
+ def cache_key
33
+ return @argument_string.gsub(REMOVE_CHARACTERS, "") if remove_characters?
34
+ if replace_characters?
35
+ REPLACE_MAP.each do |character, replacement|
36
+ @argument_string.gsub!(character, replacement)
37
+ end
38
+ end
39
+
40
+ @argument_string
41
+ end
42
+
43
+ def to_s
44
+ cache_key
45
+ end
46
+
47
+ private
48
+
49
+ def remove_characters?
50
+ options.fetch(:active_remote_cached_remove_characters, false)
51
+ end
52
+
53
+ def replace_characters?
54
+ options.fetch(:active_remote_cached_replace_characters, false)
55
+ end
56
+ end
57
+ end
@@ -56,7 +56,7 @@ module ActiveRemote::Cached
56
56
  end
57
57
 
58
58
  def valid_fetched_value?(value, options = {})
59
- return false if value.nil?
59
+ return false if value.nil? && !options.fetch(:allow_nil, false)
60
60
  return false if !options.fetch(:allow_empty, false) && value.respond_to?(:empty?) && value.empty?
61
61
  return true
62
62
  end
@@ -1,5 +1,5 @@
1
1
  module ActiveRemote
2
2
  module Cached
3
- VERSION = "0.1.3"
3
+ VERSION = "0.1.4"
4
4
  end
5
5
  end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe ::ActiveRemote::Cached::ArgumentKeys do
4
+ it "does not mutate a string by default" do
5
+ ::ActiveRemote::Cached::ArgumentKeys.new("hello", {}).cache_key.must_equal("hello")
6
+ end
7
+
8
+ it "returns a string of a symbol by default" do
9
+ ::ActiveRemote::Cached::ArgumentKeys.new(:hello, {}).cache_key.must_equal("hello")
10
+ end
11
+
12
+ it "does not mutate a string with special characters by default" do
13
+ ::ActiveRemote::Cached::ArgumentKeys.new("hello {}", {}).cache_key.must_equal("hello {}")
14
+ end
15
+
16
+ it "removes special characters from string with special characters when :active_remote_cached_remove_characters" do
17
+ options = { :active_remote_cached_remove_characters => true }
18
+ ::ActiveRemote::Cached::ArgumentKeys.new("hello {}", options).cache_key.must_equal("hello")
19
+ end
20
+
21
+ it "replaces special characters from string with special characters when :active_remote_cached_replace_characters" do
22
+ options = { :active_remote_cached_replace_characters => true }
23
+ ::ActiveRemote::Cached::ArgumentKeys.new("hello {}", options).cache_key.must_equal("helloSPLBRB")
24
+ end
25
+ end
@@ -71,7 +71,7 @@ describe FindMethodClass do
71
71
  end
72
72
 
73
73
  it "merges the default options in for the fetch call" do
74
- ::ActiveRemote::Cached.cache.expects(:fetch).with([FindMethodClass.name, "#find", :guid], :expires_in => 100).returns(:hello)
74
+ ::ActiveRemote::Cached.cache.expects(:fetch).with([FindMethodClass.name, "#find", "guid"], :expires_in => 100).returns(:hello)
75
75
 
76
76
  FindMethodClass.stub(:find, :hello) do
77
77
  FindMethodClass.cached_find_by_guid(:guid).must_equal(:hello)
@@ -79,7 +79,7 @@ describe FindMethodClass do
79
79
  end
80
80
 
81
81
  it "overrides the default options with local options for the fetch call" do
82
- ::ActiveRemote::Cached.cache.expects(:fetch).with([FindMethodClass.name, "#find", :guid], :expires_in => 200).returns(:hello)
82
+ ::ActiveRemote::Cached.cache.expects(:fetch).with([FindMethodClass.name, "#find", "guid"], :expires_in => 200).returns(:hello)
83
83
 
84
84
  FindMethodClass.stub(:find, :hello) do
85
85
  FindMethodClass.cached_find_by_guid(:guid, :expires_in => 200).must_equal(:hello)
@@ -92,7 +92,7 @@ describe FindMethodClass do
92
92
  end
93
93
 
94
94
  it "uses the namespace as a prefix to the cache key" do
95
- ::ActiveRemote::Cached.cache.expects(:fetch).with(["MyApp", FindMethodClass.name, "#find", :guid], :expires_in => 100).returns(:hello)
95
+ ::ActiveRemote::Cached.cache.expects(:fetch).with(["MyApp", FindMethodClass.name, "#find", "guid"], :expires_in => 100).returns(:hello)
96
96
 
97
97
  FindMethodClass.stub(:find, :hello) do
98
98
  FindMethodClass.cached_find_by_guid(:guid)
@@ -112,7 +112,7 @@ describe FindMethodClass do
112
112
  end
113
113
 
114
114
  it "overrides the default options with cached_finder options for the fetch call" do
115
- ::ActiveRemote::Cached.cache.expects(:fetch).with([FindMethodClass.name, "#find", :foo], :expires_in => 500).returns(:hello)
115
+ ::ActiveRemote::Cached.cache.expects(:fetch).with([FindMethodClass.name, "#find", "foo"], :expires_in => 500).returns(:hello)
116
116
 
117
117
  FindMethodClass.stub(:find, :hello) do
118
118
  FindMethodClass.cached_find_by_foo(:foo).must_equal(:hello)
@@ -120,7 +120,7 @@ describe FindMethodClass do
120
120
  end
121
121
 
122
122
  it "overrides the cached_finder options with local options for the fetch call" do
123
- ::ActiveRemote::Cached.cache.expects(:fetch).with([FindMethodClass.name, "#find", :foo], :expires_in => 200).returns(:hello)
123
+ ::ActiveRemote::Cached.cache.expects(:fetch).with([FindMethodClass.name, "#find", "foo"], :expires_in => 200).returns(:hello)
124
124
 
125
125
  FindMethodClass.stub(:find, :hello) do
126
126
  FindMethodClass.cached_find_by_foo(:foo, :expires_in => 200).must_equal(:hello)
@@ -95,7 +95,7 @@ describe SearchMethodClass do
95
95
  end
96
96
  end
97
97
 
98
- it "does not persist nil values" do
98
+ it "does not persist nil values by default" do
99
99
  SearchMethodClass.stub(:derp, nil) do
100
100
  SearchMethodClass.cached_search(:guid => :guid) do
101
101
  SearchMethodClass.derp
@@ -105,6 +105,16 @@ describe SearchMethodClass do
105
105
  end
106
106
  end
107
107
 
108
+ it "persists nil values when allow_nil sent" do
109
+ SearchMethodClass.stub(:derp, nil) do
110
+ SearchMethodClass.cached_search({:guid => :guid}, :allow_nil => true) do
111
+ SearchMethodClass.derp
112
+ end
113
+
114
+ SearchMethodClass.cached_exist_search_by_guid?(:guid).must_equal(true)
115
+ end
116
+ end
117
+
108
118
  it "does persist non nil values" do
109
119
  SearchMethodClass.stub(:derp, :derp) do
110
120
  SearchMethodClass.cached_search(:guid => :guid) do
@@ -128,7 +138,7 @@ describe SearchMethodClass do
128
138
  end
129
139
 
130
140
  it "merges the default options in for the fetch call" do
131
- ::ActiveRemote::Cached.cache.expects(:fetch).with([SearchMethodClass.name, "#search", :guid], :expires_in => 100).returns(:hello)
141
+ ::ActiveRemote::Cached.cache.expects(:fetch).with([SearchMethodClass.name, "#search", "guid"], :expires_in => 100).returns(:hello)
132
142
 
133
143
  SearchMethodClass.stub(:search, :hello) do
134
144
  SearchMethodClass.cached_search_by_guid(:guid).must_equal(:hello)
@@ -136,7 +146,7 @@ describe SearchMethodClass do
136
146
  end
137
147
 
138
148
  it "overrides the default options with local options for the fetch call" do
139
- ::ActiveRemote::Cached.cache.expects(:fetch).with([SearchMethodClass.name, "#search", :guid], :expires_in => 200).returns(:hello)
149
+ ::ActiveRemote::Cached.cache.expects(:fetch).with([SearchMethodClass.name, "#search", "guid"], :expires_in => 200).returns(:hello)
140
150
 
141
151
  SearchMethodClass.stub(:search, :hello) do
142
152
  SearchMethodClass.cached_search_by_guid(:guid, :expires_in => 200).must_equal(:hello)
@@ -149,7 +159,7 @@ describe SearchMethodClass do
149
159
  end
150
160
 
151
161
  it "uses the namespace as a prefix to the cache key" do
152
- ::ActiveRemote::Cached.cache.expects(:fetch).with(["MyApp", SearchMethodClass.name, "#search", :guid], :expires_in => 100).returns(:hello)
162
+ ::ActiveRemote::Cached.cache.expects(:fetch).with(["MyApp", SearchMethodClass.name, "#search", "guid"], :expires_in => 100).returns(:hello)
153
163
 
154
164
  SearchMethodClass.stub(:search, :hello) do
155
165
  SearchMethodClass.cached_search_by_guid(:guid)
@@ -169,7 +179,7 @@ describe SearchMethodClass do
169
179
  end
170
180
 
171
181
  it "overrides the default options with cached_finder options for the fetch call" do
172
- ::ActiveRemote::Cached.cache.expects(:fetch).with([SearchMethodClass.name, "#search", :foo], :expires_in => 500).returns(:hello)
182
+ ::ActiveRemote::Cached.cache.expects(:fetch).with([SearchMethodClass.name, "#search", "foo"], :expires_in => 500).returns(:hello)
173
183
 
174
184
  SearchMethodClass.stub(:find, :hello) do
175
185
  SearchMethodClass.cached_search_by_foo(:foo).must_equal(:hello)
@@ -177,7 +187,7 @@ describe SearchMethodClass do
177
187
  end
178
188
 
179
189
  it "overrides the cached_finder options with local options for the fetch call" do
180
- ::ActiveRemote::Cached.cache.expects(:fetch).with([SearchMethodClass.name, "#search", :foo], :expires_in => 200).returns(:hello)
190
+ ::ActiveRemote::Cached.cache.expects(:fetch).with([SearchMethodClass.name, "#search", "foo"], :expires_in => 200).returns(:hello)
181
191
 
182
192
  SearchMethodClass.stub(:find, :hello) do
183
193
  SearchMethodClass.cached_search_by_foo(:foo, :expires_in => 200).must_equal(:hello)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_remote-cached
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandon Dewitt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-29 00:00:00.000000000 Z
11
+ date: 2017-06-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_remote
@@ -110,9 +110,11 @@ files:
110
110
  - active_remote-cached.gemspec
111
111
  - lib/active_remote-cached.rb
112
112
  - lib/active_remote/cached.rb
113
+ - lib/active_remote/cached/argument_keys.rb
113
114
  - lib/active_remote/cached/cache.rb
114
115
  - lib/active_remote/cached/railtie.rb
115
116
  - lib/active_remote/cached/version.rb
117
+ - spec/active_remote/cached/argument_keys_spec.rb
116
118
  - spec/active_remote/cached/cache_spec.rb
117
119
  - spec/active_remote/cached_delete_methods_spec.rb
118
120
  - spec/active_remote/cached_exist_methods_spec.rb
@@ -138,12 +140,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
138
140
  version: '0'
139
141
  requirements: []
140
142
  rubyforge_project:
141
- rubygems_version: 2.6.10
143
+ rubygems_version: 2.5.1
142
144
  signing_key:
143
145
  specification_version: 4
144
146
  summary: Provides a configuration for caching mechanisms and finders on ActiveRemote
145
147
  models that are cached/cacheable
146
148
  test_files:
149
+ - spec/active_remote/cached/argument_keys_spec.rb
147
150
  - spec/active_remote/cached/cache_spec.rb
148
151
  - spec/active_remote/cached_delete_methods_spec.rb
149
152
  - spec/active_remote/cached_exist_methods_spec.rb