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
Methods
Public Class methods
extend_object( mod ) [ source ]

Object-extension callback — installs aliases for any currently-extant class or instance methods, and installs callbacks that will create aliases for any subsequently-defined methods. Raises an error if any object except a Class or Module is extended.

findTargetMethods( *methodList ) {|meth, transformedName| ...} [ source ]

Find methods in the given methodList which are candidates for aliasing.

included( mod ) [ source ]

The inclusion callback — uses the Ouroboros trick to extend including classes.

installAlias( mod, meth, aliasName ) [ source ]

Install an alias aliasName for the given instance method meth of the Class or Module mod.

installClassAlias( mod, meth, aliasName ) [ source ]

Install an alias aliasName for the given class method meth of the Class or Module mod.

installMethodHooks( mod ) [ source ]

Install method_added and singleton_method_added hooks into the given Module mod which auto-generate aliases for new methods.

method_added( id ) [ source ]
singleton_method_added( id ) [ source ]
transform( mid ) [ source ]

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.

transformClassMethods( mod ) [ source ]

Search for and install aliases for either underbarred or camelCased class methods for mod (a Class or Module).

transformInstanceMethods( mod ) [ source ]

Search for and install aliases for either underbarred or camelCased instance methods for mod (a Class or Module).