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

Refactor setting a method's decorators by removing Magic Number and cloning then clearing the Stack #1

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/ruby_decorators.rb
Expand Up @@ -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)}
Expand Down
11 changes: 11 additions & 0 deletions spec/ruby_decorators_spec.rb
Expand Up @@ -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
Expand Down