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