Commit | Line | Data |
443f4253 |
1 | package MooseX::Singleton; |
ede8dce0 |
2 | |
d2c25b8e |
3 | use Moose 1.10 (); |
ede8dce0 |
4 | use Moose::Exporter; |
a4e5ec1e |
5 | use MooseX::Singleton::Role::Object; |
6 | use MooseX::Singleton::Role::Meta::Class; |
7 | use MooseX::Singleton::Role::Meta::Instance; |
443f4253 |
8 | |
443f4253 |
9 | |
ede8dce0 |
10 | Moose::Exporter->setup_import_methods( also => 'Moose' ); |
443f4253 |
11 | |
ede8dce0 |
12 | sub 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 |
41 | 1; |
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 | |
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 |
0b3df906 |
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. |
b375b147 |
75 | |
0b3df906 |
76 | =head1 METHODS |
b375b147 |
77 | |
0b3df906 |
78 | A singleton class will have the following additional methods: |
b375b147 |
79 | |
0b3df906 |
80 | =head2 Singleton->instance |
03e1b8df |
81 | |
0b3df906 |
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. |
b375b147 |
87 | |
0b3df906 |
88 | =head2 Singleton->initialize(%args) |
b375b147 |
89 | |
0b3df906 |
90 | This method can be called I<only once per class>. It explicitly initializes |
91 | the singleton object with the given arguments. |
b375b147 |
92 | |
0b3df906 |
93 | =head2 Singleton->_clear_instance |
b375b147 |
94 | |
0b3df906 |
95 | This clears the existing singleton instance for the class. Obviously, this is |
96 | meant for use only inside the class itself. |
b375b147 |
97 | |
e97ef8b2 |
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 | |
b375b147 |
104 | =head1 BUGS |
105 | |
0b3df906 |
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. |
b375b147 |
110 | |
b375b147 |
111 | =head1 SOME CODE STOLEN FROM |
112 | |
113 | Anders Nor Berle E<lt>debolaz@gmail.comE<gt> |
114 | |
d64d5811 |
115 | =head1 AND PATCHES FROM |
116 | |
117 | Ricardo SIGNES E<lt>rjbs@cpan.orgE<gt> |
118 | |
b375b147 |
119 | =cut |
120 | |