update ChangeLog, bump version to 0.20
[gitmo/MooseX-Singleton.git] / lib / MooseX / Singleton.pm
CommitLineData
443f4253 1package MooseX::Singleton;
ede8dce0 2
494ee6d2 3use Moose 0.82 ();
ede8dce0 4use Moose::Exporter;
109b110b 5use MooseX::Singleton::Object;
6use MooseX::Singleton::Meta::Class;
443f4253 7
6e529236 8our $VERSION = '0.20';
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
b375b147 50=head1 SYNOPSIS
51
52 package MyApp;
53 use MooseX::Singleton;
54
55 has env => (
56 is => 'rw',
57 isa => 'HashRef[Str]',
58 default => sub { \%ENV },
59 );
60
61 package main;
62
63 delete MyApp->env->{PATH};
64 my $instance = MyApp->instance;
65 my $same = MyApp->instance;
66
67=head1 DESCRIPTION
68
69A singleton is a class that has only one instance in an application.
70C<MooseX::Singleton> lets you easily upgrade (or downgrade, as it were) your
71L<Moose> class to a singleton.
72
73All you should need to do to transform your class is to change C<use Moose> to
74C<use MooseX::Singleton>. This module uses a new class metaclass and instance
75metaclass, so if you're doing metamagic you may not be able to use this.
76
03e1b8df 77C<MooseX::Singleton> gives your class an C<instance> method that can be used to
78get a handle on the singleton. It's actually just an alias for C<new>.
b375b147 79
80Alternatively, C<< YourPackage->method >> should just work. This includes
81accessors.
82
03e1b8df 83If you need to reset your class's singleton object for some reason (e.g.
84tests), you can call C<< YourPackage->_clear_instance >>.
85
b375b147 86=head1 TODO
87
88=over
89
90=item Always more tests and doc
91
92=item Fix speed boost
93
94C<instance> invokes C<new> every time C<< Package->method >> is called, which
95incurs a nontrivial runtime cost. I've implemented a short-circuit for this
96case, which does eliminate nearly all of the runtime cost. However, it's ugly
97and should be fixed in a more elegant way.
98
99=back
100
101=head1 BUGS
102
103All complex software has bugs lurking in it, and this module is no
104exception. If you find a bug please either email me, or add the bug
105to cpan-RT.
106
060d6e6b 107=head1 AUTHORS
b375b147 108
109Shawn M Moore E<lt>sartak@gmail.comE<gt>
110
060d6e6b 111Dave Rolsky E<lt>autarch@urth.orgE<gt>
112
b375b147 113=head1 SOME CODE STOLEN FROM
114
115Anders Nor Berle E<lt>debolaz@gmail.comE<gt>
116
d64d5811 117=head1 AND PATCHES FROM
118
119Ricardo SIGNES E<lt>rjbs@cpan.orgE<gt>
120
b375b147 121=head1 COPYRIGHT AND LICENSE
122
aee76780 123Copyright 2007, 2008 Infinity Interactive
b375b147 124
125This program is free software; you can redistribute it and/or modify it under
126the same terms as Perl itself.
127
128=cut
129