Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring #method_missing to define a previously missing method in the... #89

Merged
merged 1 commit into from
Nov 30, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/draper/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,10 @@ def respond_to?(method, include_private = false)
def method_missing(method, *args, &block)
if allow?(method)
begin
model.send(method, *args, &block)
self.class.send :define_method, method do |*args, &block|
model.send(method, *args, &block)
end
self.send(method, *args, &block)
rescue NoMethodError
super
end
Expand Down
4 changes: 4 additions & 0 deletions performance/active_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module ActiveRecord
class Base
end
end
55 changes: 55 additions & 0 deletions performance/bechmark.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require 'rubygems'
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
Bundler.require(:default) if defined?(Bundler)

require "benchmark"
require "draper"
require "./performance/models"
require "./performance/decorators"

Benchmark.bm do |bm|
puts "\n[ Exclusivelly using #method_missing for model delegation ]"
[ 1_000, 10_000, 100_000 ].each do |i|
puts "\n[ #{i} ]"
bm.report("#new ") do
i.times do |n|
ProductDecorator.decorate(Product.new)
end
end

bm.report("#hello_world ") do
i.times do |n|
ProductDecorator.decorate(Product.new).hello_world
end
end

bm.report("#sample_class_method ") do
i.times do |n|
ProductDecorator.decorate(Product.new).class.sample_class_method
end
end
end

puts "\n[ Defining methods on method_missing first hit ]"
[ 1_000, 10_000, 100_000 ].each do |i|
puts "\n[ #{i} ]"
bm.report("#new ") do
i.times do |n|
FastProductDecorator.decorate(FastProduct.new)
end
end

bm.report("#hello_world ") do
i.times do |n|
FastProductDecorator.decorate(FastProduct.new).hello_world
end
end

bm.report("#sample_class_method ") do
i.times do |n|
FastProductDecorator.decorate(FastProduct.new).class.sample_class_method
end
end
end
end
47 changes: 47 additions & 0 deletions performance/decorators.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require "./performance/models"
class ProductDecorator < Draper::Base
decorates :product

def awesome_title
"Awesome Title"
end

# Original #method_missing
def method_missing(method, *args, &block)
if allow?(method)
begin
model.send(method, *args, &block)
rescue NoMethodError
super
end
else
super
end
end

end

class FastProductDecorator < Draper::Base
decorates :product

def awesome_title
"Awesome Title"
end

# Modified #method_missing
def method_missing(method, *args, &block)
if allow?(method)
begin
self.class.send :define_method, method do |*args, &block|
model.send(method, *args, &block)
end
self.send(method, *args, &block)
rescue NoMethodError
super
end
else
super
end
end

end
20 changes: 20 additions & 0 deletions performance/models.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require "./performance/active_record"
class Product < ActiveRecord::Base
def self.sample_class_method
"sample class method"
end

def hello_world
"Hello, World"
end
end

class FastProduct < ActiveRecord::Base
def self.sample_class_method
"sample class method"
end

def hello_world
"Hello, World"
end
end
17 changes: 17 additions & 0 deletions spec/draper/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -382,4 +382,21 @@ class DecoratorWithDeniesAndAllows < Draper::Base
subject.kind_of?(source.class).should == true
end
end

describe "#method_missing" do
context "when #hello_world is called for the first time" do
it "hits method missing" do
subject.should_receive(:method_missing)
subject.hello_world
end
end

context "when #hello_world is called again" do
before { subject.hello_world }
it "proxies method directly after first hit" do
subject.should_not_receive(:method_missing)
subject.hello_world
end
end
end
end