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.
Note: As mentioned, this module keeps its identity map in a class variable, @@classy_aliases, on the extending class. This could concievably lead to namespace conflicts and strange bugs in the unlikely event that this variable is used for anything else. Later versions may implement a hash of identity maps as a class variable on the Aliasable module itself, but for reasons of complexity and performance, that has not been done at this time.
Example
class ParentClass
extend Aliasable
aka :pop
end
class AliasedSubclass < ParentClass
aka :kid
end
Parent.find(:pop) # => ParentClass
Parent.find(:kid) # => AliasedSubclass
More complex usage examples can be found in the aliasable_spec.rb file.
Class Method Summary collapse
-
.extended(klass) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#aka(*names) ⇒ Object
Specifies a symbol (or several) that a given framework might be known by.
-
#aliases ⇒ Object
Return a hash of known aliases to Class objects.
-
#find(klass) ⇒ Object
When passed a class, just returns it.
-
#forget_aliases ⇒ Object
Forget all known aliases.
Class Method Details
.extended(klass) ⇒ Object
:nodoc:
32 33 34 35 36 |
# File 'lib/classy/aliasable.rb', line 32 def self.extended (klass) #:nodoc: klass.class_exec do class_variable_set(:@@classy_aliases, {}) end end |
Instance Method Details
#aka(*names) ⇒ Object
Specifies a symbol (or several) that a given framework might be known by.
class AnotherClass
aka :kid2, :chunky_bacon
...
end
63 64 65 66 67 68 |
# File 'lib/classy/aliasable.rb', line 63 def aka (*names) names.each do |name| raise ArgumentError, "Called aka with an alias that is already taken." if class_variable_get(:@@classy_aliases).include? name class_variable_get(:@@classy_aliases)[name] = self end end |
#aliases ⇒ Object
Return a hash of known aliases to Class objects
ParentClass.aliases # => { :pop => ParentClass, :kid => AliasedSubclass, :kid2 => AnotherClass, :chunky_bacon => AnotherClass }
74 75 76 |
# File 'lib/classy/aliasable.rb', line 74 def aliases class_variable_get(:@@classy_aliases).dup end |
#find(klass) ⇒ Object
When passed a class, just returns it. When passed a symbol that is an alias for a class, returns that class.
ParentClass.find(AliasedSubclass) # => AliasedSubclass
ParentClass.find(:kid) # => AliasedSubclass
44 45 46 47 |
# File 'lib/classy/aliasable.rb', line 44 def find (klass) return klass if klass.kind_of? Class class_variable_get(:@@classy_aliases)[klass] or raise ArgumentError, "Could not find alias #{klass}" end |
#forget_aliases ⇒ Object
Forget all known aliases. Mainly useful for testing purposes.
51 52 53 |
# File 'lib/classy/aliasable.rb', line 51 def forget_aliases class_variable_get(:@@classy_aliases).clear end |