Kernel#require
something and make its contents accessible via a different namespace.
require "require3" # Oh the irony
Now load the desired files and specify some rules:
require3 "some/very/long/name/here" => "*"
This will make everything accessible via the top-level namespace. It's the same as:
require "some/very/long/name/here"
include Some::Very::Long::Name::Here
Or access it as Foo
:
require3 "some/very/long/name/here" => "foo"
Same as:
require "some/very/long/name/here"
Foo = Some::Very::Long::Name::Here
If you only want to access Foo
and Bar
:
require3 "some/very/long/name/here" => %w[foo bar]
Same as:
require "some/very/long/name/here"
Foo = Some::Very::Long::Name::Here::Foo
Bar = Some::Very::Long::Name::Here::Bar
You can also provide the names as Symbol
s and/or using their proper case:
require3 "some/very/long/name/here" => [:Foo, "Bar"]
Use a Hash
to specify alternate names:
require3 "some/very/long/name/here" => { :foo => "foo_hoo", :bar => "BarHar" }
Same as:
require "some/very/long/name/here"
FooHoo = Some::Very::Long::Name::Here::Foo
BarHar = Some::Very::Long::Name::Here::Bar
Or:
require3 "some/very/long/name/here" => { "Some::Very::Foo" => "foo", :bar => "BarHar" }
Same as:
require "some/very/long/name/here"
Foo = Some::Very::Foo
BarHar = Some::Very::Long::Name::Here::Bar
require3
mostly behaves like Kernerl#require
but, if what you want to alias does not exist, a NameError
will be raised.
Path names are converted to class names using the same rules as Rails' String#camelize
(though this library is not a dependency). If this conversion fails you must explicitly provide the name. A convoluted example:
# This fails as we try to alias Net::Http
require3 "net/http" => "n"
# Do this instead
require3 "net/http" => { "Net::HTTP" => "n" }
- aliased - The Perl module that served as inspiration
- Modulation - Add explicit import and export declarations to your code
- class2 - Easily create hierarchies that support nested attributes, type conversion, serialization and more
- alias2 - Make classes, modules, and constants accessible via a different namespace.
Skye Shaw [skye.shaw AT gmail]
Released under the MIT License: http://www.opensource.org/licenses/MIT