Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added method_missing support

  • Loading branch information...
commit 75b1622b22411ca42e086995dc8b03ba79667a73 1 parent e600b29
Joshua Hull joshbuddy authored committed
Showing with 49 additions and 6 deletions.
  1. +22 −5 lib/proxen.rb
  2. +27 −1 spec/proxen_spec.rb
27 lib/proxen.rb
View
@@ -5,10 +5,17 @@ def add(klass, *args)
store[klass] = new(klass, *args)
end
- def handle(instance, sym, *args, &block)
+ def handle_method_missing(instance, sym, *args, &block)
klass = Object.instance_method(:class).bind(instance).call
if proxy = store[klass]
- proxy.handle(instance, sym, *args, &block)
+ proxy.handle_method_missing(instance, sym, *args, &block)
+ end
+ end
+
+ def handle_respond_to(instance, method, include_private)
+ klass = Object.instance_method(:class).bind(instance).call
+ if proxy = store[klass]
+ proxy.handle_respond_to(instance, method, include_private)
end
end
@@ -27,7 +34,7 @@ def initialize(klass, *args)
blankify! if @options[:blank_slate]
end
- def handle(instance, sym, *args, &block)
+ def handle_method_missing(instance, sym, *args, &block)
if target = target_for(instance, sym)
if @options[:compile]
compile(target, sym)
@@ -38,6 +45,12 @@ def handle(instance, sym, *args, &block)
end
end
+ def handle_respond_to(instance, method, include_private = false)
+ if target = target_for(instance, method)
+ instance.__send__(target).respond_to?(method, include_private)
+ end
+ end
+
def blankify!
@klass.class_eval do
instance_methods.each do |sym|
@@ -94,10 +107,14 @@ def target_for(instance, sym)
def proxy_to(*targets)
Proxen::Proxy.add(self, *targets)
-
+
class_eval(<<-END, __FILE__, __LINE__)
def method_missing(sym, *args, &block)
- Proxen::Proxy.handle(self, sym, *args, &block) || super
+ Proxen::Proxy.handle_method_missing(self, sym, *args, &block) || super
+ end
+
+ def respond_to?(method, include_private = false)
+ Proxen::Proxy.handle_respond_to(self, method, include_private) || super
end
END
end
28 spec/proxen_spec.rb
View
@@ -10,8 +10,9 @@ def foo
Class.new { def bar; :proxied end }.new
end
end
-
+
@klass.new.bar.should == :proxied
+ @klass.new.respond_to?(:bar).should be_true
end
it "respects other method missings" do
@@ -58,6 +59,10 @@ def should?(sym)
end
end
+
+ @klass.new.respond_to?(:fizz).should be_true
+ @klass.new.respond_to?(:buzz).should be_false
+
@klass.new.fizz.should == :proxied
proc {
@@ -81,6 +86,9 @@ def should_not?(sym)
end
end
+ @klass.new.respond_to?(:fizz).should be_true
+ @klass.new.respond_to?(:buzz).should be_false
+
@klass.new.fizz.should == :proxied
proc {
@@ -100,6 +108,9 @@ def buzz; :proxied end
end
end
+ @klass.new.respond_to?(:fizz).should be_true
+ @klass.new.respond_to?(:buzz).should be_false
+
@klass.new.fizz.should == :proxied
proc {
@@ -119,6 +130,9 @@ def buzz; :proxied end
end
end
+ @klass.new.respond_to?(:fizz).should be_true
+ @klass.new.respond_to?(:buzz).should be_false
+
@klass.new.fizz.should == :proxied
proc {
@@ -139,6 +153,11 @@ def buzz; :proxied end
end
end
+
+ @klass.new.respond_to?(:fizz).should be_true
+ @klass.new.respond_to?(:buzz).should be_true
+ @klass.new.respond_to?(:baz).should be_false
+
@klass.new.fizz.should == :proxied
@klass.new.buzz.should == :proxied
@@ -160,6 +179,10 @@ def buzz; :err end
end
end
+ @klass.new.respond_to?(:fizz).should be_false
+ @klass.new.respond_to?(:buzz).should be_false
+ @klass.new.respond_to?(:baz).should be_true
+
@klass.new.baz.should == :proxied
proc {
@@ -181,6 +204,9 @@ def bar
end
end
+ @klass.new.respond_to?(:foo).should be_true
+ @klass.new.respond_to?(:bar).should be_true
+
@klass.new.fizz.should == :proxied
@klass.new.buzz.should == :proxied
end
Please sign in to comment.
Something went wrong with that request. Please try again.