Recent News
Interceptor Methods
Let's say we have a class in a plugin or module somewhere, and we want to override one of its methods to handle more cases, without losing the original functionality. For example, maybe we've got a login module that checks for the user record in a database. We want to make it check an LDAP first, then check the database if the user is not found in the LDAP.
Using an "interceptor" pattern can help here. We'll alias the original method, storing a copy of it under a new name. Then we redefine the method to handle the new feature and conditionally call the old method.
Here's an example:
class Unicorn
attr_accessor :climate
def frolic
puts 'Frolic in a meadow, enchant other forest creatures with majesty'
end
end
# this class definition is locked away somewheres
# now let's reopen the class and add the interceptor
class Unicorn
# copy the original method so we can still call it
alias_method :old_frolic, :frolic
# let's intercept calls to frolic method
def frolic
if @climate == 'tundra' # new functionality
puts 'Frolic on a glittering glacier, under the Aurora Borealis'
else # conditionally default to old method definition
old_frolic
end
end
end
written by jjones on May 7th, 2008 @ 07:28 PM
0 comments
Comments are closed
cementhorses.com
Recent
- Engineering happiness (August 15th)
- The future with gestures (August 14th)
- Debugging Kentico sites in vs.net 2008/05 improve compilation time (August 14th)
- Resistance ...is a good thing? (August 13th)
- Managing dashboard widgets on various external displays (August 1st)
- Counting in CSS (July 28th)
- Take control of your server configuration with Capistrano (July 23rd)
- Time Parseable (July 23rd)
- An SSH one-liner to authorize via public-key (June 23rd)
- MicroApps (or little ditties) with Sinatra (June 17th)
- RoR: Schedulable (May 29th)
- Less work, more be-ing with quick shell aliasing (May 8th)
- Interceptor Methods (May 7th)
Comments
- Colton Sanches on Engineering happiness
- Caleb Mathews on Engineering happiness
- Collins on Engineering happiness
- Struye on Engineering happiness
- Jordan on Engineering happiness
- Aidan McDonough on Engineering happiness
- Tremonti on Engineering happiness
- Mathews on Engineering happiness
- Aiden Jones on Engineering happiness
- Rachman on Engineering happiness
- Perle on Engineering happiness
- Veer on Engineering happiness
- Robinson on Engineering happiness
- Emma Nascimento on Engineering happiness
- Martins on Engineering happiness
Categories
- Stuff You Already Know (6)
- Open Source (2)
Cement Horses Pics
Archives
- August 2008 (5)
- July 2008 (3)
- June 2008 (2)
- May 2008 (3)
