MooseX::Singleton - turn your Moose class into a singleton


MooseX-Singleton documentation Contained in the MooseX-Singleton distribution.

Index


Code Index:

NAME

Top

MooseX::Singleton - turn your Moose class into a singleton

VERSION

Top

version 0.27

SYNOPSIS

Top

    package MyApp;
    use MooseX::Singleton;

    has env => (
        is      => 'rw',
        isa     => 'HashRef[Str]',
        default => sub { \%ENV },
    );

    package main;

    delete MyApp->env->{PATH};
    my $instance = MyApp->instance;
    my $same = MyApp->instance;

DESCRIPTION

Top

A singleton is a class that has only one instance in an application. MooseX::Singleton lets you easily upgrade (or downgrade, as it were) your Moose class to a singleton.

All you should need to do to transform your class is to change use Moose to use MooseX::Singleton. This module uses metaclass roles to do its magic, so it should cooperate with most other MooseX modules.

METHODS

Top

A singleton class will have the following additional methods:

Singleton->instance

This returns the singleton instance for the given package. This method does not accept any arguments. If the instance does not yet exist, it is created with its defaults values. This means that if your singleton requires arguments, calling instance will die if the object has not already been initialized.

Singleton->initialize(%args)

This method can be called only once per class. It explicitly initializes the singleton object with the given arguments.

Singleton->_clear_instance

This clears the existing singleton instance for the class. Obviously, this is meant for use only inside the class itself.

Singleton->new

This method currently works like a hybrid of initialize and instance. However, calling new directly will probably be deprecated in a future release. Instead, call initialize or instance as appropriate.

BUGS

Top

Please report any bugs or feature requests to bug-moosex-singleton@rt.cpan.org, or through the web interface at http://rt.cpan.org. We will be notified, and then you'll automatically be notified of progress on your bug as we make changes.

SOME CODE STOLEN FROM

Top

Anders Nor Berle <debolaz@gmail.com>

AND PATCHES FROM

Top

Ricardo SIGNES <rjbs@cpan.org>

AUTHOR

Top

Shawn M Moore <sartak@gmail.com>

COPYRIGHT AND LICENSE

Top


MooseX-Singleton documentation Contained in the MooseX-Singleton distribution.

package MooseX::Singleton;
BEGIN {
  $MooseX::Singleton::AUTHORITY = 'cpan:SARTAK';
}
BEGIN {
  $MooseX::Singleton::VERSION = '0.27';
}

use Moose 1.10 ();
use Moose::Exporter;
use MooseX::Singleton::Role::Object;
use MooseX::Singleton::Role::Meta::Class;
use MooseX::Singleton::Role::Meta::Instance;


Moose::Exporter->setup_import_methods( also => 'Moose' );

sub init_meta {
    shift;
    my %p = @_;

    Moose->init_meta(%p);

    my $caller = $p{for_class};

    Moose::Util::MetaRole::apply_metaroles(
        for             => $caller,
        class_metaroles => {
            class => ['MooseX::Singleton::Role::Meta::Class'],
            instance =>
                ['MooseX::Singleton::Role::Meta::Instance'],
            constructor =>
                ['MooseX::Singleton::Role::Meta::Method::Constructor'],
        },
    );

    Moose::Util::MetaRole::apply_base_class_roles(
        for_class => $caller,
        roles =>
            ['MooseX::Singleton::Role::Object'],
    );

    return $caller->meta();
}


1;

# ABSTRACT: turn your Moose class into a singleton




__END__