active_remote-cached 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5d24644649d050e9d4e4bf6164b050a30551a146
4
+ data.tar.gz: ff66a84b00c10d48fcf6bca41b50193f961b2b7f
5
+ SHA512:
6
+ metadata.gz: 222352b851868aedc14f66832239056f81a7f467f902f682f0ca2500d70296162bace6cc7144a2758bff153d5582f3fb0aed53886d0f48d30b7c8045bb060c38
7
+ data.tar.gz: f39cb8f7a1101dc3c92afc49c9d9c21b4cc460a850ef4b589dd53a387b732bfa48577bfccc8b9f238e5a5114c8d61e6d20cfb0975990b677493c457b3a377d08
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ .ruby-*
7
+ .rvmrc
8
+ Gemfile.lock
9
+ InstalledFiles
10
+ _yardoc
11
+ coverage
12
+ doc/
13
+ lib/bundler/man
14
+ pkg
15
+ rdoc
16
+ spec/reports
17
+ test/tmp
18
+ test/version_tmp
19
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in active_remote-cached.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Brandon Dewitt
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # ActiveRemote::Cached
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'active_remote-cached'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install active_remote-cached
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,12 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs.push "lib"
6
+ t.libs.push "spec"
7
+ t.pattern = "spec/**/*_spec.rb"
8
+ t.verbose = true
9
+ end
10
+
11
+ task :spec => :test
12
+ task :default => :spec
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_remote-cached/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "active_remote-cached"
8
+ gem.version = ActiveRemote::Cached::VERSION
9
+ gem.authors = ["Brandon Dewitt"]
10
+ gem.email = ["brandonsdewitt@gmail.com"]
11
+ gem.description = %q{ Provides "cached" finders and a DSL to enumerate which finders should have cached versions }
12
+ gem.summary = %q{ Provides a configuration for caching mechanisms and finders on ActiveRemote models that are cached/cacheable }
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency "active_remote"
21
+ gem.add_dependency "activesupport"
22
+
23
+ gem.add_development_dependency "bundler"
24
+ gem.add_development_dependency "mocha"
25
+ gem.add_development_dependency "pry"
26
+ gem.add_development_dependency "rake"
27
+ end
@@ -0,0 +1,186 @@
1
+ require "active_support/concern"
2
+ require "active_support/core_ext/array/extract_options"
3
+ require "active_remote-cached/version"
4
+ require "active_remote-cached/cache"
5
+
6
+ module ActiveRemote
7
+ module Cached
8
+ extend ::ActiveSupport::Concern
9
+
10
+ def self.cache(cache_provider = nil)
11
+ if cache_provider
12
+ @cache_provider = ::ActiveRemote::Cached::Cache.new(cache_provider)
13
+ end
14
+
15
+ @cache_provider
16
+ end
17
+
18
+ def self.default_options(options = nil)
19
+ if options
20
+ @default_options = options
21
+ end
22
+
23
+ @default_options || {}
24
+ end
25
+
26
+ module ClassMethods
27
+
28
+ def cached_finders_for(*cached_finder_keys)
29
+ options = cached_finder_keys.extract_options!
30
+
31
+ cached_finder_keys.each do |cached_finder_key|
32
+ _create_cached_finder_for(cached_finder_key, options)
33
+ end
34
+ end
35
+
36
+ def cached_finders(*keys)
37
+ cached_finders_for(*keys)
38
+ end
39
+
40
+ ##
41
+ # Underscored Methods
42
+ #
43
+ def _create_cached_finder_for(cached_finder_key, options = {})
44
+ cached_finder_key_set = [ cached_finder_key ].flatten.sort
45
+
46
+ ##
47
+ # Run each permutation of the arguments passed in
48
+ # and define each finder/searcher
49
+ #
50
+ cached_finder_key_set.permutation do |arguments|
51
+ delete_method_name = _cached_delete_method_name(arguments)
52
+ exist_method_name = _cached_exist_method_name(arguments)
53
+ find_method_name = _cached_find_method_name(arguments)
54
+ search_method_name = _cached_search_method_name(arguments)
55
+
56
+ unless self.respond_to?(delete_method_name)
57
+ _define_cached_delete_method(delete_method_name, arguments)
58
+ end
59
+
60
+ unless self.respond_to?(exist_method_name)
61
+ _define_cached_exist_method(exist_method_name, arguments)
62
+ end
63
+
64
+ unless self.respond_to?(find_method_name)
65
+ _define_cached_find_method(find_method_name, arguments)
66
+ end
67
+
68
+ unless self.respond_to?(search_method_name)
69
+ _define_cached_search_method(search_method_name, arguments)
70
+ end
71
+ end
72
+ end
73
+
74
+ def _cached_delete_method_name(arguments)
75
+ "cached_delete_by_#{arguments.join('_and_')}"
76
+ end
77
+
78
+ def _cached_exist_method_name(arguments)
79
+ "cached_exist_by_#{arguments.join('_and_')}"
80
+ end
81
+
82
+ def _cached_find_method_name(arguments)
83
+ "cached_find_by_#{arguments.join('_and_')}"
84
+ end
85
+
86
+ def _cached_search_method_name(arguments)
87
+ "cached_search_by_#{arguments.join('_and_')}"
88
+ end
89
+
90
+ def _define_cached_delete_method(method_name, *method_arguments)
91
+ method_arguments.flatten!
92
+ expanded_method_args = method_arguments.join(",")
93
+ sorted_method_args = method_arguments.sort.join(",")
94
+
95
+ self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
96
+ # def self.cached_delete_by_user_guid(user_guid, options = {})
97
+ # ::ActiveRemote::Cached.cache.delete([name, user_guid])
98
+ # end
99
+ def self.#{method_name}(#{expanded_method_args}, options = {})
100
+ ::ActiveRemote::Cached.cache.delete([name, #{sorted_method_args}])
101
+ end
102
+ RUBY
103
+ end
104
+
105
+ def _define_cached_exist_method(method_name, *method_arguments)
106
+ method_arguments.flatten!
107
+ expanded_method_args = method_arguments.join(",")
108
+ sorted_method_args = method_arguments.sort.join(",")
109
+
110
+ self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
111
+ # def self.cached_exist_by_user_guid(user_guid, options = {})
112
+ # ::ActiveRemote::Cached.cache.exist?([name, user_guid])
113
+ # end
114
+ def self.#{method_name}(#{expanded_method_args}, options = {})
115
+ ::ActiveRemote::Cached.cache.exist?([name, #{sorted_method_args}])
116
+ end
117
+ RUBY
118
+
119
+ self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
120
+ # def self.cached_exist_by_user_guid?(user_guid, options = {})
121
+ # ::ActiveRemote::Cached.cache.exist?([name, user_guid])
122
+ # end
123
+ def self.#{method_name}?(#{expanded_method_args}, options = {})
124
+ ::ActiveRemote::Cached.cache.exist?([name, #{sorted_method_args}])
125
+ end
126
+ RUBY
127
+ end
128
+
129
+ def _define_cached_find_method(method_name, *method_arguments)
130
+ method_arguments.flatten!
131
+ expanded_method_args = method_arguments.join(",")
132
+ sorted_method_args = method_arguments.sort.join(",")
133
+
134
+ expanded_search_args = ""
135
+ method_arguments.each do |method_argument|
136
+ expanded_search_args << ":#{method_argument} => #{method_argument},"
137
+ end
138
+
139
+ self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
140
+ # def self.cached_find_by_user_guid(user_guid, options = {})
141
+ # options = ::ActiveRemote::Cached.default_options.merge(options)
142
+ #
143
+ # ::ActiveRemote::Cached.cache.fetch([name, user_guid], options) do
144
+ # self.find(:user_guid => user_guid)
145
+ # end
146
+ # end
147
+ def self.#{method_name}(#{expanded_method_args}, options = {})
148
+ options = ::ActiveRemote::Cached.default_options.merge(options)
149
+
150
+ ::ActiveRemote::Cached.cache.fetch([name, #{sorted_method_args}], options) do
151
+ self.find(#{expanded_search_args})
152
+ end
153
+ end
154
+ RUBY
155
+ end
156
+
157
+ def _define_cached_search_method(method_name, *method_arguments)
158
+ method_arguments.flatten!
159
+ expanded_method_args = method_arguments.join(",")
160
+ sorted_method_args = method_arguments.sort.join(",")
161
+
162
+ expanded_search_args = ""
163
+ method_arguments.each do |method_argument|
164
+ expanded_search_args << ":#{method_argument} => #{method_argument},"
165
+ end
166
+
167
+ self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
168
+ # def self.cached_search_by_user_guid(user_guid, options = {})
169
+ # options = ::ActiveRemote::Cached.default_options.merge(options)
170
+ #
171
+ # ::ActiveRemote::Cached.cache.fetch([name, user_guid], options) do
172
+ # self.search(:user_guid => user_guid)
173
+ # end
174
+ # end
175
+ def self.#{method_name}(#{expanded_method_args}, options = {})
176
+ options = ::ActiveRemote::Cached.default_options.merge(options)
177
+
178
+ ::ActiveRemote::Cached.cache.fetch([name, #{sorted_method_args}], options) do
179
+ self.search(#{expanded_search_args})
180
+ end
181
+ end
182
+ RUBY
183
+ end
184
+ end
185
+ end
186
+ end
@@ -0,0 +1,30 @@
1
+ require 'delegate'
2
+
3
+ module ActiveRemote::Cached
4
+ class Cache < ::SimpleDelegator
5
+ attr_reader :cache_provider
6
+
7
+ def initialize(new_cache_provider)
8
+ @cache_provider = new_cache_provider
9
+ validate_provider_method_present(:delete)
10
+ validate_provider_method_present(:exist?)
11
+ validate_provider_method_present(:fetch)
12
+ validate_provider_method_present(:read)
13
+ validate_provider_method_present(:write)
14
+
15
+ super(@cache_provider)
16
+ end
17
+
18
+ private
19
+
20
+ def validate_provider_method_present(method_name)
21
+ unless self.cache_provider.respond_to?(method_name)
22
+ raise <<-CACHE_METHOD
23
+ ActiveRemote::Cached::Cache must respond_to? #{method_name}
24
+ in order to be used as a caching interface for ActiveRemote
25
+ CACHE_METHOD
26
+ end
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveRemote
2
+ module Cached
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe ::ActiveRemote::Cached::Cache do
4
+ describe "API" do
5
+ it "validates #write present" do
6
+ cache = OpenStruct.new(:read => nil, :delete => nil, :fetch => nil, :exist? => nil)
7
+ error = lambda { ::ActiveRemote::Cached.cache(cache)}.must_raise(RuntimeError)
8
+ error.message.must_match(/respond_to.*write/i)
9
+ end
10
+
11
+ it "validates #read present" do
12
+ cache = OpenStruct.new(:write => nil, :delete => nil, :fetch => nil, :exist? => nil)
13
+ error = lambda { ::ActiveRemote::Cached.cache(cache) }.must_raise(RuntimeError)
14
+ error.message.must_match(/respond_to.*read/i)
15
+ end
16
+
17
+ it "validates #delete present" do
18
+ cache = OpenStruct.new(:write => nil, :fetch => nil, :read => nil, :exist? => nil)
19
+ error = lambda { ::ActiveRemote::Cached.cache(cache) }.must_raise(RuntimeError)
20
+ error.message.must_match(/respond_to.*delete/i)
21
+ end
22
+
23
+ it "validates #exist? present" do
24
+ cache = OpenStruct.new(:write => nil, :fetch => nil, :read => nil, :delete => nil)
25
+ error = lambda { ::ActiveRemote::Cached.cache(cache) }.must_raise(RuntimeError)
26
+ error.message.must_match(/respond_to.*exist/i)
27
+ end
28
+
29
+ it "validates #fetch present" do
30
+ cache = OpenStruct.new(:write => nil, :delete => nil, :read => nil, :exist? => nil)
31
+ error = lambda { ::ActiveRemote::Cached.cache(cache) }.must_raise(RuntimeError)
32
+ error.message.must_match(/respond_to.*fetch/i)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ class DeleteMethodClass
4
+ include ::ActiveRemote::Cached
5
+
6
+ def self.find; nil; end
7
+ def self.search; nil; end
8
+
9
+ cached_finders_for :guid
10
+ cached_finders_for :guid, :user_guid
11
+ cached_finders_for [:user_guid, :client_guid]
12
+ cached_finders_for [:derp, :user_guid, :client_guid]
13
+ end
14
+
15
+ describe DeleteMethodClass do
16
+ describe "API" do
17
+ it "creates 'cached_delete_by_guid'" do
18
+ DeleteMethodClass.must_respond_to("cached_delete_by_guid")
19
+ end
20
+
21
+ it "creates 'cached_delete_by_user_guid'" do
22
+ DeleteMethodClass.must_respond_to("cached_delete_by_user_guid")
23
+ end
24
+
25
+ it "creates 'cached_delete_by_user_guid_and_client_guid'" do
26
+ DeleteMethodClass.must_respond_to("cached_delete_by_user_guid_and_client_guid")
27
+ end
28
+
29
+ it "creates 'cached_delete_by_client_guid_and_user_guid'" do
30
+ DeleteMethodClass.must_respond_to("cached_delete_by_client_guid_and_user_guid")
31
+ end
32
+
33
+ it "creates 'cached_delete_by_derp_and_user_guid_and_client_guid'" do
34
+ DeleteMethodClass.must_respond_to("cached_delete_by_derp_and_user_guid_and_client_guid")
35
+ end
36
+
37
+ it "creates 'cached_delete_by_client_guid_and_derp_and_user_guid'" do
38
+ DeleteMethodClass.must_respond_to("cached_delete_by_client_guid_and_derp_and_user_guid")
39
+ end
40
+
41
+ it "creates 'cached_delete_by_client_guid_and_user_guid_and_derp'" do
42
+ DeleteMethodClass.must_respond_to("cached_delete_by_client_guid_and_user_guid_and_derp")
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,73 @@
1
+ require 'spec_helper'
2
+
3
+ class ExistMethodClass
4
+ include ::ActiveRemote::Cached
5
+
6
+ def self.find; nil; end
7
+ def self.search; nil; end
8
+
9
+ cached_finders_for :guid
10
+ cached_finders_for :guid, :user_guid
11
+ cached_finders_for [:user_guid, :client_guid]
12
+ cached_finders_for [:derp, :user_guid, :client_guid]
13
+ end
14
+
15
+ describe ExistMethodClass do
16
+ describe "API" do
17
+ it "creates 'cached_exist_by_guid'" do
18
+ ExistMethodClass.must_respond_to("cached_exist_by_guid")
19
+ end
20
+
21
+ it "creates 'cached_exist_by_user_guid'" do
22
+ ExistMethodClass.must_respond_to("cached_exist_by_user_guid")
23
+ end
24
+
25
+ it "creates 'cached_exist_by_user_guid_and_client_guid'" do
26
+ ExistMethodClass.must_respond_to("cached_exist_by_user_guid_and_client_guid")
27
+ end
28
+
29
+ it "creates 'cached_exist_by_client_guid_and_user_guid'" do
30
+ ExistMethodClass.must_respond_to("cached_exist_by_client_guid_and_user_guid")
31
+ end
32
+
33
+ it "creates 'cached_exist_by_derp_and_user_guid_and_client_guid'" do
34
+ ExistMethodClass.must_respond_to("cached_exist_by_derp_and_user_guid_and_client_guid")
35
+ end
36
+
37
+ it "creates 'cached_exist_by_client_guid_and_derp_and_user_guid'" do
38
+ ExistMethodClass.must_respond_to("cached_exist_by_client_guid_and_derp_and_user_guid")
39
+ end
40
+
41
+ it "creates 'cached_exist_by_client_guid_and_user_guid_and_derp'" do
42
+ ExistMethodClass.must_respond_to("cached_exist_by_client_guid_and_user_guid_and_derp")
43
+ end
44
+
45
+ it "creates 'cached_exist_by_guid?'" do
46
+ ExistMethodClass.must_respond_to("cached_exist_by_guid?")
47
+ end
48
+
49
+ it "creates 'cached_exist_by_user_guid?'" do
50
+ ExistMethodClass.must_respond_to("cached_exist_by_user_guid?")
51
+ end
52
+
53
+ it "creates 'cached_exist_by_user_guid_and_client_guid?'" do
54
+ ExistMethodClass.must_respond_to("cached_exist_by_user_guid_and_client_guid?")
55
+ end
56
+
57
+ it "creates 'cached_exist_by_client_guid_and_user_guid?'" do
58
+ ExistMethodClass.must_respond_to("cached_exist_by_client_guid_and_user_guid?")
59
+ end
60
+
61
+ it "creates 'cached_exist_by_derp_and_user_guid_and_client_guid?'" do
62
+ ExistMethodClass.must_respond_to("cached_exist_by_derp_and_user_guid_and_client_guid?")
63
+ end
64
+
65
+ it "creates 'cached_exist_by_client_guid_and_derp_and_user_guid?'" do
66
+ ExistMethodClass.must_respond_to("cached_exist_by_client_guid_and_derp_and_user_guid?")
67
+ end
68
+
69
+ it "creates 'cached_exist_by_client_guid_and_user_guid_and_derp?'" do
70
+ ExistMethodClass.must_respond_to("cached_exist_by_client_guid_and_user_guid_and_derp?")
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,78 @@
1
+ require 'spec_helper'
2
+
3
+ class FindMethodClass
4
+ include ::ActiveRemote::Cached
5
+
6
+ def self.find; nil; end
7
+ def self.search; nil; end
8
+
9
+ cached_finders_for :guid
10
+ cached_finders_for :guid, :user_guid
11
+ cached_finders_for [:user_guid, :client_guid]
12
+ cached_finders_for [:derp, :user_guid, :client_guid]
13
+ end
14
+
15
+ describe FindMethodClass do
16
+ describe "API" do
17
+ it "creates 'cached_find_by_guid'" do
18
+ FindMethodClass.must_respond_to("cached_find_by_guid")
19
+ end
20
+
21
+ it "creates 'cached_find_by_user_guid'" do
22
+ FindMethodClass.must_respond_to("cached_find_by_user_guid")
23
+ end
24
+
25
+ it "creates 'cached_find_by_user_guid_and_client_guid'" do
26
+ FindMethodClass.must_respond_to("cached_find_by_user_guid_and_client_guid")
27
+ end
28
+
29
+ it "creates 'cached_find_by_client_guid_and_user_guid'" do
30
+ FindMethodClass.must_respond_to("cached_find_by_client_guid_and_user_guid")
31
+ end
32
+
33
+ it "creates 'cached_find_by_derp_and_user_guid_and_client_guid'" do
34
+ FindMethodClass.must_respond_to("cached_find_by_derp_and_user_guid_and_client_guid")
35
+ end
36
+
37
+ it "creates 'cached_find_by_client_guid_and_derp_and_user_guid'" do
38
+ FindMethodClass.must_respond_to("cached_find_by_client_guid_and_derp_and_user_guid")
39
+ end
40
+
41
+ it "creates 'cached_find_by_client_guid_and_user_guid_and_derp'" do
42
+ FindMethodClass.must_respond_to("cached_find_by_client_guid_and_user_guid_and_derp")
43
+ end
44
+ end
45
+
46
+ describe "#cached_find_by_guid" do
47
+ before do
48
+ ::ActiveRemote::Cached.cache(HashCache.new)
49
+ ::ActiveRemote::Cached.default_options(:expires_in => 100)
50
+ end
51
+
52
+ after do
53
+ ::ActiveRemote::Cached.default_options({})
54
+ end
55
+
56
+ it "executes the fetch block if not present in cache" do
57
+ FindMethodClass.stub(:find, :hello) do
58
+ FindMethodClass.cached_find_by_guid(:guid).must_equal(:hello)
59
+ end
60
+ end
61
+
62
+ it "merges the default options in for the fetch call" do
63
+ ::ActiveRemote::Cached.cache.expects(:fetch).with([FindMethodClass.name, :guid], :expires_in => 100).returns(:hello)
64
+
65
+ FindMethodClass.stub(:find, :hello) do
66
+ FindMethodClass.cached_find_by_guid(:guid).must_equal(:hello)
67
+ end
68
+ end
69
+
70
+ it "overrides the default options with local options for the fetch call" do
71
+ ::ActiveRemote::Cached.cache.expects(:fetch).with([FindMethodClass.name, :guid], :expires_in => 200).returns(:hello)
72
+
73
+ FindMethodClass.stub(:find, :hello) do
74
+ FindMethodClass.cached_find_by_guid(:guid, :expires_in => 200).must_equal(:hello)
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,78 @@
1
+ require 'spec_helper'
2
+
3
+ class SearchMethodClass
4
+ include ::ActiveRemote::Cached
5
+
6
+ def self.find; nil; end
7
+ def self.search; nil; end
8
+
9
+ cached_finders_for :guid
10
+ cached_finders_for :guid, :user_guid
11
+ cached_finders_for [:user_guid, :client_guid]
12
+ cached_finders_for [:derp, :user_guid, :client_guid]
13
+ end
14
+
15
+ describe SearchMethodClass do
16
+ describe "API" do
17
+ it "creates 'cached_search_by_guid'" do
18
+ SearchMethodClass.must_respond_to("cached_search_by_guid")
19
+ end
20
+
21
+ it "creates 'cached_search_by_user_guid'" do
22
+ SearchMethodClass.must_respond_to("cached_search_by_user_guid")
23
+ end
24
+
25
+ it "creates 'cached_search_by_user_guid_and_client_guid'" do
26
+ SearchMethodClass.must_respond_to("cached_search_by_user_guid_and_client_guid")
27
+ end
28
+
29
+ it "creates 'cached_search_by_client_guid_and_user_guid'" do
30
+ SearchMethodClass.must_respond_to("cached_search_by_client_guid_and_user_guid")
31
+ end
32
+
33
+ it "creates 'cached_search_by_derp_and_user_guid_and_client_guid'" do
34
+ SearchMethodClass.must_respond_to("cached_search_by_derp_and_user_guid_and_client_guid")
35
+ end
36
+
37
+ it "creates 'cached_search_by_client_guid_and_derp_and_user_guid'" do
38
+ SearchMethodClass.must_respond_to("cached_search_by_client_guid_and_derp_and_user_guid")
39
+ end
40
+
41
+ it "creates 'cached_search_by_client_guid_and_user_guid_and_derp'" do
42
+ SearchMethodClass.must_respond_to("cached_search_by_client_guid_and_user_guid_and_derp")
43
+ end
44
+ end
45
+
46
+ describe "#cached_search_by_guid" do
47
+ before do
48
+ ::ActiveRemote::Cached.cache(HashCache.new)
49
+ ::ActiveRemote::Cached.default_options(:expires_in => 100)
50
+ end
51
+
52
+ after do
53
+ ::ActiveRemote::Cached.default_options({})
54
+ end
55
+
56
+ it "executes the fetch block if not present in cache" do
57
+ SearchMethodClass.stub(:search, :hello) do
58
+ SearchMethodClass.cached_search_by_guid(:guid).must_equal(:hello)
59
+ end
60
+ end
61
+
62
+ it "merges the default options in for the fetch call" do
63
+ ::ActiveRemote::Cached.cache.expects(:fetch).with([SearchMethodClass.name, :guid], :expires_in => 100).returns(:hello)
64
+
65
+ SearchMethodClass.stub(:search, :hello) do
66
+ SearchMethodClass.cached_search_by_guid(:guid).must_equal(:hello)
67
+ end
68
+ end
69
+
70
+ it "overrides the default options with local options for the fetch call" do
71
+ ::ActiveRemote::Cached.cache.expects(:fetch).with([SearchMethodClass.name, :guid], :expires_in => 200).returns(:hello)
72
+
73
+ SearchMethodClass.stub(:search, :hello) do
74
+ SearchMethodClass.cached_search_by_guid(:guid, :expires_in => 200).must_equal(:hello)
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,32 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.require(:default, :development, :test)
4
+
5
+ require 'minitest/mock'
6
+ require 'minitest/spec'
7
+ require 'minitest/autorun'
8
+ require 'minitest/pride'
9
+
10
+ class HashCache < Hash
11
+ def exist?(key)
12
+ self.has_key?(key)
13
+ end
14
+
15
+ def fetch(key, options = {}, &blk)
16
+ if self.has_key?(key)
17
+ return self[key]
18
+ end
19
+
20
+ self[key] = yield
21
+ end
22
+
23
+ def read(key)
24
+ self[key]
25
+ end
26
+
27
+ def write(key, value)
28
+ self[key] = value
29
+ end
30
+ end
31
+
32
+ require 'mocha/api'
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_remote-cached
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Brandon Dewitt
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: active_remote
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mocha
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: ' Provides "cached" finders and a DSL to enumerate which finders should
98
+ have cached versions '
99
+ email:
100
+ - brandonsdewitt@gmail.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - .gitignore
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - active_remote-cached.gemspec
111
+ - lib/active_remote-cached.rb
112
+ - lib/active_remote-cached/cache.rb
113
+ - lib/active_remote-cached/version.rb
114
+ - spec/cache_spec.rb
115
+ - spec/cached_delete_methods_spec.rb
116
+ - spec/cached_exist_methods_spec.rb
117
+ - spec/cached_find_methods_spec.rb
118
+ - spec/cached_search_methods_spec.rb
119
+ - spec/spec_helper.rb
120
+ homepage: ''
121
+ licenses: []
122
+ metadata: {}
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - '>='
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubyforge_project:
139
+ rubygems_version: 2.0.3
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: Provides a configuration for caching mechanisms and finders on ActiveRemote
143
+ models that are cached/cacheable
144
+ test_files:
145
+ - spec/cache_spec.rb
146
+ - spec/cached_delete_methods_spec.rb
147
+ - spec/cached_exist_methods_spec.rb
148
+ - spec/cached_find_methods_spec.rb
149
+ - spec/cached_search_methods_spec.rb
150
+ - spec/spec_helper.rb