CachingPresenter has moved
on February 14, 2009 @ 11:03 PM
The new home for CachingPresenter is: http://github.com/mhs/caching_presenter
What is CachingPresenter?
CachingPresenter is a very small presenter pattern implementation in Ruby. In short, the presenter pattern is a technique used to separate presentation logic (aka display logic or view logic) from domain logic and from the view templates themselves. There are a number of reasons this pattern is beneficial when used:
- it stops presentation logic from creeping into domain objects and muddying up their implementation and their business logic
- it stops unnecessary logic from creeping into the views themselves which should be as simple, flexible, and changeable as possible
- it allows you to organize presentation logic in better ways than simply maintaining helper modules that are included everywhere
CachingPresenter seems to be benefit most web-based projects where there is benefit from a separation between the view templates and corresponding presentation logic. It works superbly with Rails, but it is not dependent on Rails. You can use it for any ruby project, web-based or not.
Recent Changes
:requiring is removed in favor of :accepts
The :requiring option has been removed in favor of :accepts. This allows all additional arguments to be optional. For example:
1 2 3 4 5 6 7 8 9 |
class NinjaPresenter < CachingPresenter presents :ninja, :accepts => [:sword, :agility] end # supplying all options ninja = NinjaPresenter.new :ninja => Ninja.new, :sword => Sword.new, :agility => 100.percent # supplying only some options, previously this would yell at you for not supplying :agility ninja = NinjaPresenter.new :ninja => Ninja.new, :sword => Sword.new |
Caches hash-reader methods
If a presenter defines the #[] method or if what is being presented on responds to the #[] methods all calls to the presenter using hash-like accessors will successfully be cached. Previously this would die.
1 2 3 4 5 6 7 8 9 10 |
class SamuraiPresenter < CachingPresenter presents :samurai, :accepts => [:stats] def [](key) @stats[key] end end samurai = SamuraiPresenter.new :samurai, :stats => { :dexterity => 80.percent } samurai[:dexterity] # => 80.percent |
Does not try to cache writer methods
Usually you don’t use writer methods on presenters, but every now and then there is a good reason. Previously, CachingPresenter would completely fail if you writer to define an writer method, now it doesn’t. And it will successfully not be cached.
1 2 3 4 5 6 7 8 |
class SamuraiPresenter < CachingPresenter presents :samurai, :accepts => [:stats] # previously this line would have failed miserably def sword=(sword) end end |
Questions / Docs / Contact / Etc
Any questions, checkout the github page and its corresponding wiki . If all else fails let me know, zach dot dennis at gmail dot com



0 comments
Jump to comment form | comments rss [?]