From 162634de2fa222f2d2c12adb52d4f68989096898 Mon Sep 17 00:00:00 2001 From: "Simeon F. Willbanks" Date: Tue, 18 Sep 2012 20:00:53 -0700 Subject: [PATCH] Refactor setting a method's decorators by removing Magic Number and cloning then clearing the Stack --- lib/ruby_decorators.rb | 5 ++++- spec/ruby_decorators_spec.rb | 11 +++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/ruby_decorators.rb b/lib/ruby_decorators.rb index a9b897f..ded4b2b 100644 --- a/lib/ruby_decorators.rb +++ b/lib/ruby_decorators.rb @@ -17,7 +17,10 @@ def method_added(method_name) return if RubyDecorators::Stack.all.empty? @methods[method_name] = instance_method(method_name) - @decorators[method_name] = RubyDecorators::Stack.all.pop(42) + + RubyDecorators::Stack.all.tap do |a| + @decorators[method_name] = a.clone + end.clear class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 #{method_visibility_for(method_name)} diff --git a/spec/ruby_decorators_spec.rb b/spec/ruby_decorators_spec.rb index ba259f6..f118d52 100644 --- a/spec/ruby_decorators_spec.rb +++ b/spec/ruby_decorators_spec.rb @@ -118,6 +118,17 @@ def hello_private end describe "a simple decorator" do + it "clones and clears the stack" do + arr = MiniTest::Mock.new + arr.expect(:tap, nil) + arr.expect(:clone, [DummyDecorator]) + arr.expect(:clear, nil) + RubyDecorators::Stack.stub :all, arr do + assert arr.clone == [DummyDecorator] + end + subject.hello_public.must_equal 'hello batman' + end + it "decorates a public method" do subject.hello_public.must_equal 'hello batman' end