basiccache 0.0.21 → 0.0.22

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: 01928e29f4f24f46565978f288a68f8188680696
4
- data.tar.gz: a6d587ec58409e3b516ef5d08dc72bb822c11765
3
+ metadata.gz: 22d2709c9524354f74da72c0b334aa8eb8f41b1b
4
+ data.tar.gz: 395bc1e79ca4a535e7bfd209e156684d9bb1adca
5
5
  SHA512:
6
- metadata.gz: f51d6a8627626a3facdfd62b376ad0aa3b2ee3ae8492b5221933cb9dc4a579dba1cbf042124bbab48a930da6f4074bc425d0287e373b2b860727c49247510de0
7
- data.tar.gz: 37c5521e200019da014db4943cc86f8d7a1ec5ea8b2edef82644868c3bf9177e2242c508376b4aecea277c9af8a28043b3c5165d460920555801fe833a140d2e
6
+ metadata.gz: b4184929bde760bfbc19555318fd9186ae3a1728291cebc49e859c48752448bdb8e288b47e00688d984e0a9044f84b764118081ec6766f7b0f580c46f055f85f
7
+ data.tar.gz: 51df167bc6fee497e09d1d77751d3b850b474136a54464fa12234653b4201ea52ba0d82c19670abe8909c4694b0a67097b0220df877a71cad38ef79e7f80353d
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- basiccache (0.0.21)
4
+ basiccache (0.0.22)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -56,6 +56,54 @@ my_cache.clear!
56
56
 
57
57
  The .clear! method can be passed a key name to clear just a specific key, as well.
58
58
 
59
+ ### MethodCacher
60
+
61
+ MethodCacher provides a helper module for caching method calls inside a class.
62
+
63
+ To cache the results of Foo.bar and Foo.act, for instance, you'd do the following:
64
+
65
+ ```
66
+ require 'basiccache'
67
+
68
+ class Foo
69
+ include MethodCacher
70
+
71
+ def initialize
72
+ enable_caching [:bar, :act]
73
+ end
74
+
75
+ def bar
76
+ # deep computation here
77
+ end
78
+
79
+ def act
80
+ # more super deep calculations
81
+ end
82
+
83
+ def other
84
+ # this method isn't cached
85
+ end
86
+ end
87
+ ```
88
+
89
+ You can also provide a second argument to enable_caching to specify a custom cache object (like TimeCache):
90
+
91
+ ```
92
+ require 'basiccache'
93
+
94
+ class Foo
95
+ include MethodCacher
96
+
97
+ def initialize
98
+ enable_caching [:act], BasicCache::TimeCache.new
99
+ end
100
+
101
+ def act
102
+ # super deep time-sensitive calculations
103
+ end
104
+ end
105
+ ```
106
+
59
107
  ## Subclasses
60
108
 
61
109
  ### TimeCache
data/basiccache.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'basiccache'
3
- s.version = '0.0.21'
3
+ s.version = '0.0.22'
4
4
  s.date = Time.now.strftime("%Y-%m-%d")
5
5
  s.summary = 'Provides a minimal key/value caching layer'
6
6
  s.description = "Allows an application to dynamically cache values and retrieve them later"
data/lib/basiccache.rb CHANGED
@@ -26,6 +26,6 @@ module BasicCache
26
26
  end
27
27
  end
28
28
 
29
- require 'version'
30
29
  require 'caches/cache'
31
30
  require 'caches/timecache'
31
+ require 'methodcacher'
@@ -0,0 +1,17 @@
1
+ ##
2
+ # Helper module for caching methods inside a class
3
+ # To use, extend your class with MethodCacher
4
+ # Then, in initialize, call enable_caching
5
+
6
+ module MethodCacher
7
+ def enable_caching(methods, cache = nil)
8
+ cache ||= BasicCache.new
9
+ methods.each do |name|
10
+ uncached_name = "#{name}_uncached".to_sym
11
+ (class << self; self; end).class_eval do
12
+ alias_method uncached_name, name
13
+ define_method(name) { |*a| cache.cache { send uncached_name, *a } }
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,12 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe BasicCache do
4
- describe '::VERSION' do
5
- it 'follows the semantic version scheme' do
6
- expect(BasicCache::VERSION).to match /\d+\.\d+\.\d+/
7
- end
8
- end
9
-
10
4
  describe '.new' do
11
5
  it 'creates cache objects' do
12
6
  expect(BasicCache.new).to be_an_instance_of BasicCache::Cache
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ ##
4
+ # Example class for testing method caching
5
+
6
+ class Example
7
+ include MethodCacher
8
+
9
+ def initialize(skip_cache = false)
10
+ return if skip_cache
11
+ enable_caching [:repeat]
12
+ enable_caching [:time_repeat], BasicCache::TimeCache.new(1)
13
+ end
14
+
15
+ def repeat(input)
16
+ input
17
+ end
18
+
19
+ def time_repeat(input)
20
+ input
21
+ end
22
+
23
+ def not_cached(input)
24
+ input
25
+ end
26
+ end
27
+
28
+ describe MethodCacher do
29
+ let(:test_object) { Example.new }
30
+ let(:uncached_object) { Example.new(skip_cache: true) }
31
+
32
+ describe '#enable_caching' do
33
+ it 'wraps methods with the cache' do
34
+ expect(test_object.repeat 2).to eql 2
35
+ expect(test_object.repeat 3).to eql 2
36
+ end
37
+ it "doesn't mess with other instances" do
38
+ expect(uncached_object.repeat 5).to eql 5
39
+ expect(uncached_object.repeat 6).to eql 6
40
+ end
41
+ it 'allows a user-supplied cache object' do
42
+ expect(test_object.time_repeat 2).to eql 2
43
+ expect(test_object.time_repeat 3).to eql 2
44
+ sleep 2
45
+ expect(test_object.time_repeat 4).to eql 4
46
+ end
47
+ it 'does not override other methods' do
48
+ expect(test_object.not_cached 7).to eql 7
49
+ expect(test_object.not_cached 8).to eql 8
50
+ end
51
+ it 'aliases the uncached methods' do
52
+ expect(test_object.repeat_uncached 5).to eql 5
53
+ expect(test_object.repeat_uncached 4).to eql 4
54
+ end
55
+ end
56
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: basiccache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.21
4
+ version: 0.0.22
5
5
  platform: ruby
6
6
  authors:
7
7
  - Les Aker
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-24 00:00:00.000000000 Z
11
+ date: 2013-11-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -127,10 +127,11 @@ files:
127
127
  - lib/basiccache.rb
128
128
  - lib/caches/cache.rb
129
129
  - lib/caches/timecache.rb
130
- - lib/version.rb
130
+ - lib/methodcacher.rb
131
131
  - spec/basiccache_spec.rb
132
132
  - spec/caches/cache_spec.rb
133
133
  - spec/caches/timecache_spec.rb
134
+ - spec/methodcacher_spec.rb
134
135
  - spec/spec_helper.rb
135
136
  - vendor/cache/ast-1.1.0.gem
136
137
  - vendor/cache/coveralls-0.7.0.gem
@@ -187,4 +188,5 @@ test_files:
187
188
  - spec/basiccache_spec.rb
188
189
  - spec/caches/cache_spec.rb
189
190
  - spec/caches/timecache_spec.rb
191
+ - spec/methodcacher_spec.rb
190
192
  - spec/spec_helper.rb
data/lib/version.rb DELETED
@@ -1,6 +0,0 @@
1
- ##
2
- # Define the version
3
-
4
- module BasicCache
5
- VERSION = '0.0.20'
6
- end