Converted this extension to use MetaRole
[gitmo/MooseX-Singleton.git] / lib / MooseX / Singleton.pm
CommitLineData
443f4253 1package MooseX::Singleton;
ede8dce0 2
8eec3c69 3use Moose 0.89_02 ();
ede8dce0 4use Moose::Exporter;
109b110b 5use MooseX::Singleton::Object;
6use MooseX::Singleton::Meta::Class;
443f4253 7
b1882d9b 8our $VERSION = '0.19';
ede8dce0 9$VERSION = eval $VERSION;
443f4253 10
ede8dce0 11Moose::Exporter->setup_import_methods( also => 'Moose' );
443f4253 12
ede8dce0 13sub 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 401;
41
b375b147 42__END__
43
44=pod
45
46=head1 NAME
47
48MooseX::Singleton - turn your Moose class into a singleton
49
50=head1 VERSION
51
ade9ece0 52Version 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
73A singleton is a class that has only one instance in an application.
74C<MooseX::Singleton> lets you easily upgrade (or downgrade, as it were) your
75L<Moose> class to a singleton.
76
77All you should need to do to transform your class is to change C<use Moose> to
78C<use MooseX::Singleton>. This module uses a new class metaclass and instance
79metaclass, so if you're doing metamagic you may not be able to use this.
80
03e1b8df 81C<MooseX::Singleton> gives your class an C<instance> method that can be used to
82get a handle on the singleton. It's actually just an alias for C<new>.
b375b147 83
84Alternatively, C<< YourPackage->method >> should just work. This includes
85accessors.
86
03e1b8df 87If you need to reset your class's singleton object for some reason (e.g.
88tests), 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
98C<instance> invokes C<new> every time C<< Package->method >> is called, which
99incurs a nontrivial runtime cost. I've implemented a short-circuit for this
100case, which does eliminate nearly all of the runtime cost. However, it's ugly
101and should be fixed in a more elegant way.
102
103=back
104
105=head1 BUGS
106
107All complex software has bugs lurking in it, and this module is no
108exception. If you find a bug please either email me, or add the bug
109to cpan-RT.
110
060d6e6b 111=head1 AUTHORS
b375b147 112
113Shawn M Moore E<lt>sartak@gmail.comE<gt>
114
060d6e6b 115Dave Rolsky E<lt>autarch@urth.orgE<gt>
116
b375b147 117=head1 SOME CODE STOLEN FROM
118
119Anders Nor Berle E<lt>debolaz@gmail.comE<gt>
120
d64d5811 121=head1 AND PATCHES FROM
122
123Ricardo SIGNES E<lt>rjbs@cpan.orgE<gt>
124
b375b147 125=head1 COPYRIGHT AND LICENSE
126
aee76780 127Copyright 2007, 2008 Infinity Interactive
b375b147 128
129This program is free software; you can redistribute it and/or modify it under
130the same terms as Perl itself.
131
132=cut
133