Commit | Line | Data |
443f4253 |
1 | package MooseX::Singleton; |
ede8dce0 |
2 | |
51e6046b |
3 | use Moose 0.94 (); |
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 | |
1148f62f |
9 | our $VERSION = '0.23'; |
ede8dce0 |
10 | $VERSION = eval $VERSION; |
443f4253 |
11 | |
ede8dce0 |
12 | Moose::Exporter->setup_import_methods( also => 'Moose' ); |
443f4253 |
13 | |
ede8dce0 |
14 | sub 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 |
43 | 1; |
44 | |
b375b147 |
45 | __END__ |
46 | |
47 | =pod |
48 | |
49 | =head1 NAME |
50 | |
51 | MooseX::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 | |
72 | A singleton is a class that has only one instance in an application. |
73 | C<MooseX::Singleton> lets you easily upgrade (or downgrade, as it were) your |
74 | L<Moose> class to a singleton. |
75 | |
76 | All you should need to do to transform your class is to change C<use Moose> to |
0b3df906 |
77 | C<use MooseX::Singleton>. This module uses metaclass roles to do its magic, so |
78 | it should cooperate with most other C<MooseX> modules. |
b375b147 |
79 | |
0b3df906 |
80 | =head1 METHODS |
b375b147 |
81 | |
0b3df906 |
82 | A singleton class will have the following additional methods: |
b375b147 |
83 | |
0b3df906 |
84 | =head2 Singleton->instance |
03e1b8df |
85 | |
0b3df906 |
86 | This returns the singleton instance for the given package. This method does |
87 | I<not> accept any arguments. If the instance does not yet exist, it is created |
88 | with its defaults values. This means that if your singleton requires |
89 | arguments, calling C<instance> will die if the object has not already been |
90 | initialized. |
b375b147 |
91 | |
0b3df906 |
92 | =head2 Singleton->initialize(%args) |
b375b147 |
93 | |
0b3df906 |
94 | This method can be called I<only once per class>. It explicitly initializes |
95 | the singleton object with the given arguments. |
b375b147 |
96 | |
0b3df906 |
97 | =head2 Singleton->_clear_instance |
b375b147 |
98 | |
0b3df906 |
99 | This clears the existing singleton instance for the class. Obviously, this is |
100 | meant for use only inside the class itself. |
b375b147 |
101 | |
e97ef8b2 |
102 | =head2 Singleton->new |
103 | |
104 | This method currently works like a hybrid of C<initialize> and |
105 | C<instance>. However, calling C<new> directly will probably be deprecated in a |
106 | future release. Instead, call C<initialize> or C<instance> as appropriate. |
107 | |
b375b147 |
108 | =head1 BUGS |
109 | |
0b3df906 |
110 | Please report any bugs or feature requests to |
111 | C<bug-moosex-singleton@rt.cpan.org>, or through the web interface at |
112 | L<http://rt.cpan.org>. We will be notified, and then you'll automatically be |
113 | notified of progress on your bug as we make changes. |
b375b147 |
114 | |
060d6e6b |
115 | =head1 AUTHORS |
b375b147 |
116 | |
117 | Shawn M Moore E<lt>sartak@gmail.comE<gt> |
118 | |
060d6e6b |
119 | Dave Rolsky E<lt>autarch@urth.orgE<gt> |
120 | |
b375b147 |
121 | =head1 SOME CODE STOLEN FROM |
122 | |
123 | Anders Nor Berle E<lt>debolaz@gmail.comE<gt> |
124 | |
d64d5811 |
125 | =head1 AND PATCHES FROM |
126 | |
127 | Ricardo SIGNES E<lt>rjbs@cpan.orgE<gt> |
128 | |
b375b147 |
129 | =head1 COPYRIGHT AND LICENSE |
130 | |
0b3df906 |
131 | Copyright 2007-2009 Infinity Interactive |
b375b147 |
132 | |
133 | This program is free software; you can redistribute it and/or modify it under |
134 | the same terms as Perl itself. |
135 | |
136 | =cut |
137 | |