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