Skip to content

Commit

Permalink
added method_missing support
Browse files Browse the repository at this point in the history
  • Loading branch information
joshbuddy authored and nakajima committed Dec 4, 2009
1 parent e600b29 commit 75b1622
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
27 changes: 22 additions & 5 deletions lib/proxen.rb
Expand Up @@ -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

Expand All @@ -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)
Expand All @@ -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|
Expand Down Expand Up @@ -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
Expand Down
28 changes: 27 additions & 1 deletion spec/proxen_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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

Expand All @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit 75b1622

Please sign in to comment.