Commit | Line | Data |
443f4253 |
1 | package MooseX::Singleton; |
ede8dce0 |
2 | |
8eec3c69 |
3 | use Moose 0.89_02 (); |
ede8dce0 |
4 | use Moose::Exporter; |
109b110b |
5 | use MooseX::Singleton::Object; |
6 | use MooseX::Singleton::Meta::Class; |
443f4253 |
7 | |
b1882d9b |
8 | our $VERSION = '0.19'; |
ede8dce0 |
9 | $VERSION = eval $VERSION; |
443f4253 |
10 | |
ede8dce0 |
11 | Moose::Exporter->setup_import_methods( also => 'Moose' ); |
443f4253 |
12 | |
ede8dce0 |
13 | sub init_meta { |
14 | shift; |
8eec3c69 |
15 | my %p = @_; |
16 | |
17 | Moose->init_meta(%p); |
18 | |
19 | my $caller = $p{for_class}; |
20 | |
21 | Moose::Util::MetaRole::apply_metaclass_roles( |
22 | for_class => $caller, |
23 | metaclass_roles => ['MooseX::Singleton::Role::Meta::Class'], |
24 | instance_metaclass_roles => |
25 | ['MooseX::Singleton::Role::Meta::Instance'], |
26 | constructor_class_roles => |
27 | ['MooseX::Singleton::Role::Meta::Method::Constructor'], |
ede8dce0 |
28 | ); |
8eec3c69 |
29 | |
30 | Moose::Util::MetaRole::apply_base_class_roles( |
31 | for_class => $caller, |
32 | roles => |
33 | ['MooseX::Singleton::Role::Object'], |
34 | ); |
35 | |
36 | return $caller->meta(); |
443f4253 |
37 | } |
38 | |
8eec3c69 |
39 | |
443f4253 |
40 | 1; |
41 | |
b375b147 |
42 | __END__ |
43 | |
44 | =pod |
45 | |
46 | =head1 NAME |
47 | |
48 | MooseX::Singleton - turn your Moose class into a singleton |
49 | |
50 | =head1 VERSION |
51 | |
ade9ece0 |
52 | Version 0.18, released 24 May 08 |
b375b147 |
53 | |
54 | =head1 SYNOPSIS |
55 | |
56 | package MyApp; |
57 | use MooseX::Singleton; |
58 | |
59 | has env => ( |
60 | is => 'rw', |
61 | isa => 'HashRef[Str]', |
62 | default => sub { \%ENV }, |
63 | ); |
64 | |
65 | package main; |
66 | |
67 | delete MyApp->env->{PATH}; |
68 | my $instance = MyApp->instance; |
69 | my $same = MyApp->instance; |
70 | |
71 | =head1 DESCRIPTION |
72 | |
73 | A singleton is a class that has only one instance in an application. |
74 | C<MooseX::Singleton> lets you easily upgrade (or downgrade, as it were) your |
75 | L<Moose> class to a singleton. |
76 | |
77 | All you should need to do to transform your class is to change C<use Moose> to |
78 | C<use MooseX::Singleton>. This module uses a new class metaclass and instance |
79 | metaclass, so if you're doing metamagic you may not be able to use this. |
80 | |
03e1b8df |
81 | C<MooseX::Singleton> gives your class an C<instance> method that can be used to |
82 | get a handle on the singleton. It's actually just an alias for C<new>. |
b375b147 |
83 | |
84 | Alternatively, C<< YourPackage->method >> should just work. This includes |
85 | accessors. |
86 | |
03e1b8df |
87 | If you need to reset your class's singleton object for some reason (e.g. |
88 | tests), you can call C<< YourPackage->_clear_instance >>. |
89 | |
b375b147 |
90 | =head1 TODO |
91 | |
92 | =over |
93 | |
94 | =item Always more tests and doc |
95 | |
96 | =item Fix speed boost |
97 | |
98 | C<instance> invokes C<new> every time C<< Package->method >> is called, which |
99 | incurs a nontrivial runtime cost. I've implemented a short-circuit for this |
100 | case, which does eliminate nearly all of the runtime cost. However, it's ugly |
101 | and should be fixed in a more elegant way. |
102 | |
103 | =back |
104 | |
105 | =head1 BUGS |
106 | |
107 | All complex software has bugs lurking in it, and this module is no |
108 | exception. If you find a bug please either email me, or add the bug |
109 | to cpan-RT. |
110 | |
060d6e6b |
111 | =head1 AUTHORS |
b375b147 |
112 | |
113 | Shawn M Moore E<lt>sartak@gmail.comE<gt> |
114 | |
060d6e6b |
115 | Dave Rolsky E<lt>autarch@urth.orgE<gt> |
116 | |
b375b147 |
117 | =head1 SOME CODE STOLEN FROM |
118 | |
119 | Anders Nor Berle E<lt>debolaz@gmail.comE<gt> |
120 | |
d64d5811 |
121 | =head1 AND PATCHES FROM |
122 | |
123 | Ricardo SIGNES E<lt>rjbs@cpan.orgE<gt> |
124 | |
b375b147 |
125 | =head1 COPYRIGHT AND LICENSE |
126 | |
aee76780 |
127 | Copyright 2007, 2008 Infinity Interactive |
b375b147 |
128 | |
129 | This program is free software; you can redistribute it and/or modify it under |
130 | the same terms as Perl itself. |
131 | |
132 | =cut |
133 | |