Module: Aliasable

Defined in:
lib/classy/aliasable.rb

Overview

Aliasable allows you to assign aliases to a class (usually symbols, but any unique objects would work) and look it up again later with that alias. This alias-to-class hash is kept in a class variable, so each mapping is unique to a given class hierarchy. Possible uses for this include friendlier DSLs or additional layers of dynamic abstraction when specifying classes.

Example

class ParentClass
  include Aliasable
  aka :pop
end

class AliasedSubclass < ParentClass
  aka :kid
end

Parent.find(:pop)   # => ParentClass
Parent.find(:kid)   # => AliasedSubclass

It is also possible to include Aliasable from a model, which will then track aliases of classes which include that module.

Example

module Meta
  include Aliasable
end

class AliasedClass
  include Meta

  aka :klass
end

Meta.find(:klass)  # => AliasedClass

More complex usage examples can be found in the spec file.

NOTE: This defines a class variable, @@classy_aliases, on any class or module that includes Aliasable (or any class that includes a module including Aliasable).

ANOTHER NOTE: As always, if you define your own .included methods, be sure to call super.

Defined Under Namespace

Modules: AliasingClassMethods, ControllingClassMethods, UniversalClassMethods

Class Method Summary collapse

Class Method Details

.included(mod) ⇒ Object

Handle a module or class including Aliasable.

:nodoc:



52
53
54
55
56
57
58
# File 'lib/classy/aliasable.rb', line 52

def self.included( mod )
  mod.extend ControllingClassMethods
  mod.extend UniversalClassMethods
  mod.extend AliasingClassMethods if mod.kind_of? Class       # If mod is a Class, the aliased classes get the class methods via inheritance.
  mod.send :class_variable_set, :@@classy_aliases, Hash.new
  super
end