Super simple ruby decorators
I wanted to have model decorators, accessible from anywhere but in separate namespace.
https://gist.github.com/dux/581c860c143ffb17da0037e14159cb43
Made a library? Written a blog post? Found a useful tutorial? Share it with the Ruby community here or just enjoy what everyone else has found!
I wanted to have model decorators, accessible from anywhere but in separate namespace.
https://gist.github.com/dux/581c860c143ffb17da0037e14159cb43
Comments
if somebody uses https://github.com/drapergem/draper or something similar I would appreciate comment when and why alternate approach better, if it is.
I personally prefer plain old decorators:
https://gist.github.com/yukas/7d68738c33a5aece045885fbe6394466
— No magic thus no bugs — Easy to test — We can have number of decorators for different purposes
I use https://ruby-doc.org/stdlib-2.2.1/libdoc/delegate/rdoc/SimpleDelegator.html
One thing I found lacking with various decorator implementations is that they always fall back to method missing for every method call.
The following example attempts optimizes the class by defining the method the first time method missing is called for a method that the decorated object responds to.
https://gist.github.com/dkolath/c306b6e72f6109dda49e91c7323382af
https://bogdanvlviv.github.io/posts/ruby/patterns/decorator-pattern-in-ruby.html
Post a comment