Skip to content
/ alias2 Public

Make classes, modules, and constants accessible via a different namespace.

Notifications You must be signed in to change notification settings

sshaw/alias2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

alias2

Make classes, modules, and constants accessible via a different namespace.

Build Status

Usage

require "alias2"
require "some/very/long/name/here" # If not already loaded (or autoloaded)

An Array of constant names to alias can be provided:

alias2 Some::Very::Long::Name::Here, %w[Foo Bar Baz]

This is the same as:

Foo = Some::Very::Long::Name::Here::Foo
Bar = Some::Very::Long::Name::Here::Bar
Baz = Some::Very::Long::Name::Here::Baz

The namespace can also be a String.

To alias everything in the namespace to the top-level:

alias2 Some::Very::Long::Name::Here, "*"

Same as above:

Foo = Some::Very::Long::Name::Here::Foo
Bar = Some::Very::Long::Name::Here::Bar
Baz = Some::Very::Long::Name::Here::Baz

If you'd like to alias them using a different name you can:

alias2 Some::Very::Long::Name::Here, :Foo => "FooHoo", :Bar => "BarHar", :Baz => "Bazzzz"

Same as:

FooHoo = Some::Very::Long::Name::Here::Foo
BarHar = Some::Very::Long::Name::Here::Bar
Bazzzz = Some::Very::Long::Name::Here::Baz

Keys can also be Strings.

You can filter the classes/modules to alias. Here, only subclasses of ActiveRecord::Base will be aliased:

alias2 Some::Very::Long::Name::Here, "*" do |klass|
  klass < ActiveRecord::Base
end

The above is the same as:

Foo = Some::Very::Long::Name::Here::Foo if Some::Very::Long::Name::Here::Foo.is_a?(ActiveRecord::Base)
Bar = Some::Very::Long::Name::Here::Bar if Some::Very::Long::Name::Here::Bar.is_a?(ActiveRecord::Base)
Baz = Some::Very::Long::Name::Here::Baz if Some::Very::Long::Name::Here::Baz.is_a?(ActiveRecord::Base)

When a block is given you can omit the alias. It will default to "*".

The block can also return an alias:

alias2 Some::Very::Long::Name::Here do |klass|
  klass.name.end_with?("Foo") ? "Foo_X" : klass.name.split("::")[-1]
end

This is the same as:

Foo_X = Some::Very::Long::Name::Here::Foo if Some::Very::Long::Name::Here::Foo.name.end_with?("Foo")
Bar = Some::Very::Long::Name::Here::Bar
Baz = Some::Very::Long::Name::Here::Baz

An alias' target can also be a namespace you want created:

alias2 Some::Very::Long::Name::Here, :Foo => "New::Namespace::SameFoo", :Bar => "BarHar"

Same as:

module New
  module Namespace
    SameFoo = Some::Very::Long::Name::Here::Foo
  end
end

BarHar = Some::Very::Long::Name::Here::Bar

Or it can be an existing namespace:

alias2 Some::Very::Long::Name::Here, :Foo => "Existing::Namespace::Foo", :Bar => "BarHar"

Same as:

Existing::Namespace::Foo = Some::Very::Long::Name::Here::Foo
BarHar = Some::Very::Long::Name::Here::Bar

In all cases the original namespace is not modified and remains in scope.

Errors

  • NameError - raised when a constant cannot be found or when a constant is already defined
  • ArgumentError - raised when an alias list or block is not provided

See Also

  • aliased - The Perl module that served as inspiration
  • require3 - Kernel#require something and make it accessible via a different namespace
  • class2 - Easily create hierarchies that support nested attributes, type conversion, serialization and more

Author

Skye Shaw [skye.shaw AT gmail]

License

Released under the MIT License: http://www.opensource.org/licenses/MIT

About

Make classes, modules, and constants accessible via a different namespace.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages