Badger
view release on metacpan or search on metacpan
lib/Badger/Hub.pm view on Meta::CPAN
be loaded and instantiated automatically. The C<AUTOLOAD> method also
generates the missing method so that you can avoid the overhead of the
C<AUTOLOAD> method the next time you call it.
my $filesystem = $hub->filesystem;
You can add your own component to a hub and they will be available in the
same way.
$hub->components( fuzzbox => 'My::Module::Fuzzbox' );
my $fuzzbox = $hub->fuzzbox;
=head2 Delegates
As well as accessing components directly, you can also make use of delegate
methods that get forwarded onto a component. For example, the hub C<file()>
method is just a short cut to the C<file()> method of the C<filesystem>
component (implemented by L<Badger::Filesystem>).
$file = $hub->file('/path/to/file'); # the short cut
$file = $hub->filesystem->file('/path/to/file'); # the long way
You can easily define your own delegate methods.
$hub->delegates( warm_fuzz => 'fuzzbox' );
$fuzzed = $hub->warm_fuzz; # the short way
$fuzzed = $hub->fuzzbox->warm_fuzz; # the long way.
=head2 Subclassing Badger::Hub
You can subclass L<Badger::Hub> to define your own collection of components
and delegate methods, as shown in the example below.
package My::Hub;
use Badger::Class
version => 0.01,
debug => 0,
base => 'Badger::Hub';
our $COMPONENTS = {
fuzzbox => 'My::Module::Fuzzbox',
flanger => 'My::Module::Flanger',
};
our $DELEGATES = {
warm_fuzz => 'fuzzbox',
dirty_noise => 'fuzzbox',
wide_flange => 'flanger',
wet_flange => 'flanger',
};
=head2 Circular References are a Good Thing
In some cases, sub-systems instantiated by a L<Badger::Hub> will also
maintain a reference back to the hub. This allows them to access other
sub-systems and components that they require.
Note that this behaviour implicitly creates circular references between the
hub and its delegates. This is intentional. It ensures that the hub and
delegates keep each other alive until the hub is explicitly destroyed and the
references are freed. Having the hub stick around for as long as possible is
usually a Good Thing. It acts as a singleton providing a central point of
access to the resources that your application uses (which is a fancy way of
saying it's like a global variable).
+-----+ +-----------+
| HUB |----->| COMPONENT |
| |<-----| |
+-----+ +-----------+
If you manually create a hub for whatever reason (and the cases where you
would need to are few and far between) then you are responsible for calling
the L<destroy()> method when you're done with it. This will manually break
the circular references and free up any memory used by the hub and any
delegates it is using. If you don't call the L<destroy()> method then the
hub will remain alive until the end of the program when the memory will be
freed as usual. In most cases this is perfectly acceptable.
However, you generally don't need to worry about any of this because you
wouldn't normally create a hub manually. Instead, you would leave it up to the
L<Badger> façade (or I<"front-end">) module to do that behind the scenes. When
you create a L<Badger> module it implicitly creates a C<Badger::Hub> to use.
When the L<Badger> object goes out of scope its C<DESTROY> method
automatically calls the hub's L<destroy> method.
sub foo {
my $badger = Badger->new;
my $hub = $badger->hub;
# do something
# $badger object is freed here, that calls $hub->destroy
}
Because there is no reference from the hub back to the L<Badger> façade object
you don't have to worry about circular references. The L<Badger> object is
correctly freed and that ensures the hub gets cleaned up.
+--------+ +-----+ +-----------+
| BADGER |----->| HUB |----->| COMPONENT |
| | | |<-----| |
+--------+ +-----+ +-----------+
If you call C<Badger> methods as class methods then they are forwarded to
a L<prototype|Badger::Prototype> object (effectively a singleton object).
That in turn will use a L<prototype|Badger::Prototype> hub object. In this
case, both the C<Badger> and C<Badger::Hub> objects will exist until the
end of the program. This ensures that your class methods all I<Do the right
Thing> without you having to worry about creating a L<Badger> object.
# class method creates Badger prototype, which creates Badger::Hub
# prototype, which loads, instantiates and caches Badger::Filesystem
# which can then fetch the file
my $file = Badger->file('/path/to/file');
# later... reuse same Badger, Badger::Hub and Badger::Filesystem
my $dir = Badger->dir('/path/to/dir');
=head1 METHODS
=head2 new()
Constructor method used to create a new hub object.
$hub = Badger::Hub->new();
=head2 components()
This method can be used to get or set entries in the components table
for the hub. Components are other modules that the hub can delegate to.
# get components hash ref
my $comps = $hub->components;
# add new components
$hub->components({
fuzzbox => 'My::Module::Fuzzbox',
( run in 2.188 seconds using v1.01-cache-2.11-cpan-df04353d9ac )