Skip to content

Instantly share code, notes, and snippets.

@jrochkind
Created December 20, 2012 03:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrochkind/4342677 to your computer and use it in GitHub Desktop.
Save jrochkind/4342677 to your computer and use it in GitHub Desktop.
ultra simple ruby Decorator via SimpleDelegator
require 'delegate'
class Base
def foo
"foo"
end
def bar
"bar"
end
end
class MyDecorator < SimpleDelegator
def new_method
"new_method"
end
def foo
"Overridden #{super}"
end
end
class DecoratorTest < Test::Unit::TestCase
def setup
@base = Base.new
@decorated = MyDecorator.new(@base)
end
def test_pass_through_methods
assert_equal "bar", @decorated.bar
end
def test_decorator_can_add_method
assert_equal "new_method", @decorated.new_method
end
def test_override_with_super
assert_equal "Overridden foo", @decorated.foo
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment