Skip to content
Browse files

use define_method rather than eval for delegation

  • Loading branch information...
1 parent 3c99033 commit d35d44a308ef544b9125922929046bbad4c47459 @rkh rkh committed Apr 30, 2011
Showing with 11 additions and 7 deletions.
  1. +5 −7 lib/sinatra/base.rb
  2. +6 −0 test/delegator_test.rb
View
12 lib/sinatra/base.rb
@@ -1485,13 +1485,11 @@ def self.register(*extensions, &block) #:nodoc:
module Delegator #:nodoc:
def self.delegate(*methods)
methods.each do |method_name|
- eval <<-RUBY, binding, '(__DELEGATE__)', 1
- def #{method_name}(*args, &b)
- return super if respond_to? #{method_name.inspect}
- ::Sinatra::Delegator.target.send(#{method_name.inspect}, *args, &b)
- end
- private #{method_name.inspect}
- RUBY
+ define_method(method_name) do |*args, &block|
+ return super(*args, &block) if respond_to? method_name
+ Delegator.target.send(method_name, *args, &block)
+ end
+ private method_name
end
end
View
6 test/delegator_test.rb
@@ -127,6 +127,12 @@ def method_missing(method, *args, &block)
assert_equal({:some => :option}, value)
end
+ it "delegates crazy method names" do
+ Sinatra::Delegator.delegate "foo:bar:"
+ method = mirror { send "foo:bar:" }.last_call.first
+ assert_equal "foo:bar:", method
+ end
+
delegates 'get'
delegates 'patch'
delegates 'put'

0 comments on commit d35d44a

Please sign in to comment.
Something went wrong with that request. Please try again.