This post originated from an RSS feed registered with Ruby Buzz
by Jay Fields.
Original Post: Ruby: State pattern using Modules and Facets
Feed Title: Jay Fields Thoughts
Feed URL: http://blog.jayfields.com/rss.xml
Feed Description: Thoughts on Software Development
While browsing the docs for Facets I noticed the Kernel.as method. Then, a day later I needed a solution that added behavior using the state pattern; however, it was more elegant for our solution if the state instances could call methods on the class containing the state object. Our first solution was to delegate to the containing class via the method_missing method of the state instances. While this did work, debugging became problematic when things went wrong.
I did a quick spike to see if Kernel.as could provide a solution.
member =FamilyMember.new member.person =James member.name # => "James Holbrook" member.person =Lynn member.name # => "Lynn Holbrook" member.first_name rescue"first_name is not defined"# => "first_name is not defined"
There's a few interesting things about this solution. I can change the behavior at runtime by changing the person attribute. Also, the behavior is actually on the class instead of living on an independent state object that delegates back to the class. Lastly, each method is protected so they must be accessed via the delegations, and an error will occur if they are accessed directly.