shallow 0.0.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/lib/shallow.rb +94 -0
- data/shallow.gemspec +51 -0
- data/test/test_shallow.rb +44 -2
- metadata +5 -4
data/Rakefile
CHANGED
@@ -6,7 +6,7 @@ begin
|
|
6
6
|
Jeweler::Tasks.new do |gem|
|
7
7
|
gem.name = "shallow"
|
8
8
|
gem.summary = %Q{A gem for creating cached response for expensive method calls}
|
9
|
-
gem.description = %Q{
|
9
|
+
gem.description = %Q{A gem for creating cached response for expensive method calls}
|
10
10
|
gem.email = "jeremy.n.friesen@gmail.com"
|
11
11
|
gem.homepage = "http://github.com/jeremyf/shallow"
|
12
12
|
gem.authors = ["Jeremy Friesen"]
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.1.1
|
data/lib/shallow.rb
CHANGED
@@ -0,0 +1,94 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'singleton'
|
3
|
+
module Shallow
|
4
|
+
CACHE_FOLDER = 'shallow'
|
5
|
+
module Method
|
6
|
+
def self.with_cache_clearing_for(context)
|
7
|
+
yield and return unless context.respond_to?(:returning_value_cache_caller)
|
8
|
+
old_method = context.method(:returning_value_cache_caller)
|
9
|
+
context.meta_def(:returning_value_cache_caller) do
|
10
|
+
Shallow::Caller::Clear
|
11
|
+
end
|
12
|
+
yield
|
13
|
+
ensure
|
14
|
+
context.meta_def(:returning_value_cache_caller, &old_method)
|
15
|
+
end
|
16
|
+
|
17
|
+
def shallow_method(method_name, options = {})
|
18
|
+
scope = options[:scope] || options['scope'] || lambda { nil }
|
19
|
+
class_eval do
|
20
|
+
define_method(:returning_value_cache_caller) do
|
21
|
+
Shallow::Caller::Base
|
22
|
+
end
|
23
|
+
old_method = instance_method(method_name.to_sym)
|
24
|
+
define_method(method_name.to_sym) do |*args, &block|
|
25
|
+
returning_value_cache_caller.new(
|
26
|
+
:args => args,
|
27
|
+
:keys => [instance_eval(&scope).to_s, method_name],
|
28
|
+
:capture => old_method.bind(self)
|
29
|
+
).call(&block)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class Cache
|
36
|
+
def initialize
|
37
|
+
@data_store = {}
|
38
|
+
end
|
39
|
+
include Singleton
|
40
|
+
def exist?(key)
|
41
|
+
@data_store.key?(key)
|
42
|
+
end
|
43
|
+
def fetch(key)
|
44
|
+
if @data_store.key?(key)
|
45
|
+
@data_store.fetch(key)
|
46
|
+
else
|
47
|
+
@data_store[key] = yield
|
48
|
+
end
|
49
|
+
end
|
50
|
+
def delete(key)
|
51
|
+
@data_store.delete(key)
|
52
|
+
end
|
53
|
+
def read(key)
|
54
|
+
@data_store.fetch(key)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
module Caller
|
59
|
+
class Base
|
60
|
+
attr_reader :cache_key, :captured_method
|
61
|
+
def initialize(options = {})
|
62
|
+
keys = options[:keys]
|
63
|
+
@args = options[:args]
|
64
|
+
@cache_key = File.join(keys.inject([CACHE_FOLDER]){|m,v| m << v.to_s})
|
65
|
+
@captured_method = options[:capture]
|
66
|
+
end
|
67
|
+
|
68
|
+
def call(&context)
|
69
|
+
if cache.exist?(cache_key)
|
70
|
+
returning_value = YAML.load(cache.read(cache_key))
|
71
|
+
context.call(*returning_value, &context) if block_given? && !returning_value.nil?
|
72
|
+
return *returning_value
|
73
|
+
else
|
74
|
+
returning_value = nil
|
75
|
+
cache.fetch(cache_key) {
|
76
|
+
returning_value = captured_method.call(*@args, &lambda {|args| context.call(args); args })
|
77
|
+
returning_value ? [returning_value].to_yaml : nil.to_yaml
|
78
|
+
}
|
79
|
+
return *returning_value
|
80
|
+
end
|
81
|
+
end
|
82
|
+
protected
|
83
|
+
def cache
|
84
|
+
Cache.instance
|
85
|
+
end
|
86
|
+
end
|
87
|
+
class Clear < Base
|
88
|
+
def call(&context)
|
89
|
+
cache.delete(cache_key) if cache.exist?(cache_key)
|
90
|
+
super(&context)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
data/shallow.gemspec
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{shallow}
|
8
|
+
s.version = "0.1.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Jeremy Friesen"]
|
12
|
+
s.date = %q{2010-04-01}
|
13
|
+
s.description = %q{A gem for creating cached response for expensive method calls}
|
14
|
+
s.email = %q{jeremy.n.friesen@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"lib/shallow.rb",
|
27
|
+
"shallow.gemspec",
|
28
|
+
"test/helper.rb",
|
29
|
+
"test/test_shallow.rb"
|
30
|
+
]
|
31
|
+
s.homepage = %q{http://github.com/jeremyf/shallow}
|
32
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
33
|
+
s.require_paths = ["lib"]
|
34
|
+
s.rubygems_version = %q{1.3.6}
|
35
|
+
s.summary = %q{A gem for creating cached response for expensive method calls}
|
36
|
+
s.test_files = [
|
37
|
+
"test/helper.rb",
|
38
|
+
"test/test_shallow.rb"
|
39
|
+
]
|
40
|
+
|
41
|
+
if s.respond_to? :specification_version then
|
42
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
43
|
+
s.specification_version = 3
|
44
|
+
|
45
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
46
|
+
else
|
47
|
+
end
|
48
|
+
else
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
data/test/test_shallow.rb
CHANGED
@@ -1,7 +1,49 @@
|
|
1
1
|
require 'helper'
|
2
2
|
|
3
3
|
class TestShallow < Test::Unit::TestCase
|
4
|
-
def
|
5
|
-
|
4
|
+
def setup
|
5
|
+
@klass = Class.new {
|
6
|
+
attr_accessor :value
|
7
|
+
extend Shallow::Method
|
8
|
+
def foo(msg)
|
9
|
+
msg
|
10
|
+
end
|
11
|
+
shallow_method(:foo)
|
12
|
+
def bar(*args)
|
13
|
+
yield(*args)
|
14
|
+
end
|
15
|
+
shallow_method(:bar)
|
16
|
+
|
17
|
+
def with_value
|
18
|
+
return nil if value.nil?
|
19
|
+
yield(value)
|
20
|
+
end
|
21
|
+
shallow_method(:with_value)
|
22
|
+
}
|
23
|
+
@object = @klass.new
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_cache_return_value
|
27
|
+
assert_equal 'message', @object.foo('message')
|
28
|
+
assert_equal 'message', @object.foo('bob_message')
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_cache_yielded_values
|
32
|
+
@yielded_value = nil
|
33
|
+
@object.bar(1,2,3) { |a,b,c| @yielded_value = [a,b,c] }
|
34
|
+
assert_equal [1,2,3], @yielded_value
|
35
|
+
@object.bar(2,3,4) { |a,b,c| @yielded_value = [a,b,c] }
|
36
|
+
assert_equal [1,2,3], @yielded_value
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_cache_return_value_and_yield_value
|
40
|
+
@yielded_value = :false
|
41
|
+
@object.value = nil
|
42
|
+
assert_equal nil, @object.with_value { |val| @yielded_value = val }
|
43
|
+
assert_equal :false, @yielded_value
|
44
|
+
|
45
|
+
@object.value = 1
|
46
|
+
assert_equal nil, @object.with_value { |val| @yielded_value = val }
|
47
|
+
assert_equal :false, @yielded_value
|
6
48
|
end
|
7
49
|
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 0.
|
7
|
+
- 1
|
8
|
+
- 1
|
9
|
+
version: 0.1.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Jeremy Friesen
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-04-01 00:00:00 -04:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
@@ -35,6 +35,7 @@ files:
|
|
35
35
|
- Rakefile
|
36
36
|
- VERSION
|
37
37
|
- lib/shallow.rb
|
38
|
+
- shallow.gemspec
|
38
39
|
- test/helper.rb
|
39
40
|
- test/test_shallow.rb
|
40
41
|
has_rdoc: true
|