CrossCase - a mixin for making methods look the way you like ‘em. Or for making them look the way other people like ‘em. Or something. Let’s try that again… CrossCase - A mixin for auto-generating under_barred aliases for camelCased methods, and vice-versa. Better.
Synopsis
class MyClass
include CrossCase
def underbarred_method; ...; end
def camelCasedMethod; ...; end
end
obj = MyClass::new
obj.underbarredMethod
obj.camel_cased_method
_-or-_
class MyClass
def underbarred_method; ...; end
def camelCasedMethod; ...; end
end
MyClass.extend( CrossCase )
obj = MyClass::new
obj.underbarredMethod
obj.camel_cased_method
Details
This module, when mixed into a Class or another Module, will provide under_barred aliases for class or instance methods with names which follow the camelCased naming conventions, and vice-versa. E.g., in a class which mixes in CrossCase, defining a method which is called foo_bar will also create an alias for that method called fooBar.
I wrote this module because I prefer camelCased method names, but also wish to respect the preferences of my fellow Rubyists for whom such practices are an abomination. And I’m too lazy to type
alias :twinkle_twinkle :twinkleTwinkle
for every method. It’s all about laziness. Or perhaps I’m catering to my hubris. Or both; I’ll shut up now.
Caveats
This module uses the method_added and singleton_method_added hooks to generate aliases for new methods. If either or both of these methods are already defined, they will be preserved as aliases when the Class or Module is extended. It’s up to you to return the favor should you create your own hook after this module is mixed in to your class.
Author(s)
- Michael Granger <ged@FaerieMUD.org>
Thanks
- The denizens of irc://irc.freenode.net/ruby-lang, and especially dblack, oGMo, and rubyhacker1 for name suggestions.
More Information
You can find more information about this module, including any updates, at its project page:
http://www.deveiate.org/code/CrossCase.shtml
- extend_object
- findTargetMethods
- included
- installAlias
- installClassAlias
- installMethodHooks
- method_added
- singleton_method_added
- transform
- transformClassMethods
- transformInstanceMethods
Find methods in the given methodList which are candidates for aliasing.
The inclusion callback — uses the Ouroboros trick to extend including classes.
Install method_added and singleton_method_added hooks into the given Module mod which auto-generate aliases for new methods.
Return an alternate name for the given method id mid. If the method id is an under_barred method, returns a camelCased version, and vice-versa. If no alternate is called for, returns nil.