lazy_methods 1.0.5 → 1.0.6
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.
- data/VERSION +1 -1
- data/lazy_methods.gemspec +2 -2
- data/lib/lazy_methods/lazy_methods.rb +13 -8
- data/spec/lazy_method_spec.rb +10 -0
- metadata +4 -4
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0.
|
1
|
+
1.0.6
|
data/lazy_methods.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{lazy_methods}
|
8
|
-
s.version = "1.0.
|
8
|
+
s.version = "1.0.6"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Brian Durand"]
|
12
|
-
s.date = %q{2011-01-
|
12
|
+
s.date = %q{2011-01-24}
|
13
13
|
s.description = %q{Gem that adds lazy method wrapping to every object. Preceding any method with lazy_ will defer the invocation until the result is actually needed. This pattern is useful when used with caching systems.}
|
14
14
|
s.email = %q{brian@embellishedvisions.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -14,7 +14,8 @@ module LazyMethods
|
|
14
14
|
method = method.to_s
|
15
15
|
if method[0, 5] == 'lazy_'
|
16
16
|
method = method.to_s
|
17
|
-
|
17
|
+
called_method = method[5, method.length]
|
18
|
+
return Proxy.new(self, called_method, args, &block)
|
18
19
|
else
|
19
20
|
# Keep track of the current missing method calls to keep out of an infinite loop
|
20
21
|
stack = Thread.current[:lazy_method_missing_methods] ||= []
|
@@ -23,6 +24,10 @@ module LazyMethods
|
|
23
24
|
begin
|
24
25
|
stack.push(sig)
|
25
26
|
return method_missing_without_lazy(method, *args, &block)
|
27
|
+
rescue Exception => e
|
28
|
+
# Strip this method from the stack trace as it adds confusion
|
29
|
+
e.backtrace.reject!{|line| line.include?(__FILE__)}
|
30
|
+
raise e
|
26
31
|
ensure
|
27
32
|
stack.pop
|
28
33
|
end
|
@@ -41,7 +46,7 @@ module LazyMethods
|
|
41
46
|
end
|
42
47
|
|
43
48
|
def eql? (sig)
|
44
|
-
sig.kind_of(MethodSignature)
|
49
|
+
sig.kind_of(MethodSignature) && sig.object == @object && sig.method == @method
|
45
50
|
end
|
46
51
|
|
47
52
|
end
|
@@ -49,18 +54,18 @@ module LazyMethods
|
|
49
54
|
# The proxy object does all the heavy lifting.
|
50
55
|
class Proxy
|
51
56
|
# These methods we don't want to override. All other existing methods will be redefined.
|
52
|
-
PROTECTED_METHODS = %w(initialize __proxy_result__ __proxy_loaded__
|
57
|
+
PROTECTED_METHODS = %w(initialize method_missing __proxy_result__ __proxy_loaded__ __send__ __id__ object_id)
|
58
|
+
|
59
|
+
# Override already defined methods on Object to proxy them to the result object
|
60
|
+
instance_methods.each do |m|
|
61
|
+
undef_method(m) unless PROTECTED_METHODS.include?(m.to_s)
|
62
|
+
end
|
53
63
|
|
54
64
|
def initialize (obj, method, args = nil, &block)
|
55
65
|
@object = obj
|
56
66
|
@method = method
|
57
67
|
@args = args || []
|
58
68
|
@block = block
|
59
|
-
|
60
|
-
# Override already defined methods on Object to proxy them to the result object
|
61
|
-
methods.each do |m|
|
62
|
-
eval "def self.#{m} (*args, &block); __proxy_result__.send(:#{m}, *args, &block); end" unless PROTECTED_METHODS.include?(m.to_s)
|
63
|
-
end
|
64
69
|
end
|
65
70
|
|
66
71
|
# Get the result of the original method call. The original method will only be called once.
|
data/spec/lazy_method_spec.rb
CHANGED
@@ -17,6 +17,16 @@ describe LazyMethods::InstanceMethods do
|
|
17
17
|
proxy.__proxy_loaded__.should == false
|
18
18
|
end
|
19
19
|
|
20
|
+
it "should remove itself from stacktraces thrown in method_missing" do
|
21
|
+
begin
|
22
|
+
"object".call_a_missing_method_that_does_not_exist
|
23
|
+
raise "should not get here"
|
24
|
+
rescue => e
|
25
|
+
async_source_file = File.expand_path("../../lib/lazy_methods/lazy_methods.rb", __FILE__)
|
26
|
+
File.exist?(async_source_file).should == true
|
27
|
+
e.backtrace.join("\n").should_not include(async_source_file)
|
28
|
+
end
|
29
|
+
end
|
20
30
|
end
|
21
31
|
|
22
32
|
describe LazyMethods::Proxy do
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lazy_methods
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 1.0.
|
9
|
+
- 6
|
10
|
+
version: 1.0.6
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Brian Durand
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-01-
|
18
|
+
date: 2011-01-24 00:00:00 -06:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|