migrate repository to https://github.com/moose/MooseX-Singleton
[gitmo/MooseX-Singleton.git] / lib / MooseX / Singleton.pm
CommitLineData
443f4253 1package MooseX::Singleton;
ede8dce0 2
d2c25b8e 3use Moose 1.10 ();
ede8dce0 4use Moose::Exporter;
a4e5ec1e 5use MooseX::Singleton::Role::Object;
6use MooseX::Singleton::Role::Meta::Class;
7use MooseX::Singleton::Role::Meta::Instance;
443f4253 8
443f4253 9
ede8dce0 10Moose::Exporter->setup_import_methods( also => 'Moose' );
443f4253 11
ede8dce0 12sub init_meta {
13 shift;
8eec3c69 14 my %p = @_;
15
16 Moose->init_meta(%p);
17
18 my $caller = $p{for_class};
19
0428be43 20 Moose::Util::MetaRole::apply_metaroles(
21 for => $caller,
22 class_metaroles => {
23 class => ['MooseX::Singleton::Role::Meta::Class'],
24 instance =>
25 ['MooseX::Singleton::Role::Meta::Instance'],
26 constructor =>
27 ['MooseX::Singleton::Role::Meta::Method::Constructor'],
28 },
ede8dce0 29 );
8eec3c69 30
31 Moose::Util::MetaRole::apply_base_class_roles(
32 for_class => $caller,
33 roles =>
34 ['MooseX::Singleton::Role::Object'],
35 );
36
37 return $caller->meta();
443f4253 38}
39
8eec3c69 40
443f4253 411;
42
4e4f795a 43# ABSTRACT: turn your Moose class into a singleton
44
b375b147 45__END__
46
47=pod
48
b375b147 49=head1 SYNOPSIS
50
51 package MyApp;
52 use MooseX::Singleton;
53
54 has env => (
55 is => 'rw',
56 isa => 'HashRef[Str]',
57 default => sub { \%ENV },
58 );
59
60 package main;
61
62 delete MyApp->env->{PATH};
63 my $instance = MyApp->instance;
64 my $same = MyApp->instance;
65
66=head1 DESCRIPTION
67
68A singleton is a class that has only one instance in an application.
69C<MooseX::Singleton> lets you easily upgrade (or downgrade, as it were) your
70L<Moose> class to a singleton.
71
72All you should need to do to transform your class is to change C<use Moose> to
0b3df906 73C<use MooseX::Singleton>. This module uses metaclass roles to do its magic, so
74it should cooperate with most other C<MooseX> modules.
b375b147 75
0b3df906 76=head1 METHODS
b375b147 77
0b3df906 78A singleton class will have the following additional methods:
b375b147 79
0b3df906 80=head2 Singleton->instance
03e1b8df 81
0b3df906 82This returns the singleton instance for the given package. This method does
83I<not> accept any arguments. If the instance does not yet exist, it is created
84with its defaults values. This means that if your singleton requires
85arguments, calling C<instance> will die if the object has not already been
86initialized.
b375b147 87
0b3df906 88=head2 Singleton->initialize(%args)
b375b147 89
0b3df906 90This method can be called I<only once per class>. It explicitly initializes
91the singleton object with the given arguments.
b375b147 92
0b3df906 93=head2 Singleton->_clear_instance
b375b147 94
0b3df906 95This clears the existing singleton instance for the class. Obviously, this is
96meant for use only inside the class itself.
b375b147 97
e97ef8b2 98=head2 Singleton->new
99
100This method currently works like a hybrid of C<initialize> and
101C<instance>. However, calling C<new> directly will probably be deprecated in a
102future release. Instead, call C<initialize> or C<instance> as appropriate.
103
b375b147 104=head1 BUGS
105
0b3df906 106Please report any bugs or feature requests to
107C<bug-moosex-singleton@rt.cpan.org>, or through the web interface at
108L<http://rt.cpan.org>. We will be notified, and then you'll automatically be
109notified of progress on your bug as we make changes.
b375b147 110
b375b147 111=head1 SOME CODE STOLEN FROM
112
113Anders Nor Berle E<lt>debolaz@gmail.comE<gt>
114
d64d5811 115=head1 AND PATCHES FROM
116
117Ricardo SIGNES E<lt>rjbs@cpan.orgE<gt>
118
b375b147 119=cut
120