Yes, what you are looking at here is a code sample of what is possible
with Jackbox closures. You see normally decorators in ruby either suffer
from class identity loss or limit the times they can be applied to an
object to 1(one) time.
Here a a couple of articles on that:
http://nithinbekal.com/posts/ruby-decorators/
http://robots.thoughtbot.com/evaluating-alternative-decorator-implementations-in
So with our closures you can apply the same pattern to an object over
and over,
cup = Coffee.new.enrich milk, sprinkles, sprinkles
but the instance remains being what it is.
cup.should be_instance_of(Coffee)
What's more you can have the class introspect on the decorators it
possesses.
cup.injectors.should == [:milk, :sprinkles, :sprinkles ]
Furthermore you can add new facets to your decorators:
user_input = 'extra red sprinkles'
sprinkles do
define_method :appearance do
user_input
end
end
cup.enrich(sprinkles)
cup.appearance.should == 'extra red sprinkles'
cup.cost.should == 2.25
And then latter:
user_input = 'cold milk'
milk do
define_method :temp do
user_input.split.first
end
end
cup.enrich milk
cup.temp.should == 'cold'
cup.cost.should == 2.55
Decorators are useful in graphical environments including HTML, in steam
processing, command processors to name a few.
Thank you, kindly.
lha
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/7e0e756625f2e7171498dd39299bad80%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment