migrate repository to https://github.com/moose/MooseX-Singleton
[gitmo/MooseX-Singleton.git] / lib / MooseX / Singleton.pm
1 package MooseX::Singleton;
2
3 use Moose 1.10 ();
4 use Moose::Exporter;
5 use MooseX::Singleton::Role::Object;
6 use MooseX::Singleton::Role::Meta::Class;
7 use MooseX::Singleton::Role::Meta::Instance;
8
9
10 Moose::Exporter->setup_import_methods( also => 'Moose' );
11
12 sub init_meta {
13     shift;
14     my %p = @_;
15
16     Moose->init_meta(%p);
17
18     my $caller = $p{for_class};
19
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         },
29     );
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();
38 }
39
40
41 1;
42
43 # ABSTRACT: turn your Moose class into a singleton
44
45 __END__
46
47 =pod
48
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
68 A singleton is a class that has only one instance in an application.
69 C<MooseX::Singleton> lets you easily upgrade (or downgrade, as it were) your
70 L<Moose> class to a singleton.
71
72 All you should need to do to transform your class is to change C<use Moose> to
73 C<use MooseX::Singleton>. This module uses metaclass roles to do its magic, so
74 it should cooperate with most other C<MooseX> modules.
75
76 =head1 METHODS
77
78 A singleton class will have the following additional methods:
79
80 =head2 Singleton->instance
81
82 This returns the singleton instance for the given package. This method does
83 I<not> accept any arguments. If the instance does not yet exist, it is created
84 with its defaults values. This means that if your singleton requires
85 arguments, calling C<instance> will die if the object has not already been
86 initialized.
87
88 =head2 Singleton->initialize(%args)
89
90 This method can be called I<only once per class>. It explicitly initializes
91 the singleton object with the given arguments.
92
93 =head2 Singleton->_clear_instance
94
95 This clears the existing singleton instance for the class. Obviously, this is
96 meant for use only inside the class itself.
97
98 =head2 Singleton->new
99
100 This method currently works like a hybrid of C<initialize> and
101 C<instance>. However, calling C<new> directly will probably be deprecated in a
102 future release. Instead, call C<initialize> or C<instance> as appropriate.
103
104 =head1 BUGS
105
106 Please report any bugs or feature requests to
107 C<bug-moosex-singleton@rt.cpan.org>, or through the web interface at
108 L<http://rt.cpan.org>. We will be notified, and then you'll automatically be
109 notified of progress on your bug as we make changes.
110
111 =head1 SOME CODE STOLEN FROM
112
113 Anders Nor Berle E<lt>debolaz@gmail.comE<gt>
114
115 =head1 AND PATCHES FROM
116
117 Ricardo SIGNES E<lt>rjbs@cpan.orgE<gt>
118
119 =cut
120