Skip to content

Commit 5c4180a

Browse files
committed
allow class/singleton methods to be profiled
1 parent 20ff9b2 commit 5c4180a

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

lib/mini_profiler/profiling_methods.rb

+12
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@ def profile_method(klass, method, type = :profile, &blk)
109109
klass.send :alias_method, method, with_profiling
110110
end
111111

112+
def profile_singleton_method(klass, method, type = :profile, &blk)
113+
profile_method(singleton_class(klass), method, type, &blk)
114+
end
115+
116+
def unprofile_singleton_method(klass, method)
117+
unprofile_method(singleton_class(klass), method)
118+
end
119+
112120
# Add a custom timing. These are displayed similar to SQL/query time in
113121
# columns expanding to the right.
114122
#
@@ -134,6 +142,10 @@ def counter(type, duration_ms=nil)
134142

135143
private
136144

145+
def singleton_class(klass)
146+
class << klass; self; end
147+
end
148+
137149
def clean_method_name(method)
138150
method.to_s.gsub(/[\?\!]/, "")
139151
end

spec/components/profiler_spec.rb

+10
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ class TestClass
5656
def foo(bar,baz)
5757
return [bar, baz, yield]
5858
end
59+
60+
def self.bar(baz,boo)
61+
return [baz, boo, yield]
62+
end
5963
end
6064
end
6165

@@ -65,6 +69,12 @@ def foo(bar,baz)
6569
Rack::MiniProfiler.unprofile_method TestClass, :foo
6670
end
6771

72+
it 'should not destroy a singleton method' do
73+
Rack::MiniProfiler.profile_singleton_method TestClass, :bar
74+
TestClass.bar("a", "b"){"c"}.should == ["a","b","c"]
75+
Rack::MiniProfiler.unprofile_singleton_method TestClass, :bar
76+
end
77+
6878
end
6979

7080
describe 'step' do

0 commit comments

Comments
 (0)