PORO decorators with SimpleDelegator
An easy and PORO way to create decorators is using ruby’s SimpleDelegator.
SimpleDelegator
A concrete implementation of Delegator, this class provides the means to delegate all supported method calls to the object passed into the constructor and even to change the object being delegated to at a later time with #__setobj__.
I like the decorators to behave like they are the class that’s been decorated. To acheive this
let’s create a class Decorator
which inherits from SimpleDelegator
.
As you can see we created the method component
which will return the object it’s decorating
and the method class
which will return the component class.
Now let’s create a UserDecorator
that inherits from Decorator
.
Great! But how do we use this decorator?
So decorating objects allows you to avoid having view logic on your model.