migrate repository to https://github.com/moose/MooseX-Singleton
[gitmo/MooseX-Singleton.git] / lib / MooseX / Singleton / Role / Object.pm
1 package MooseX::Singleton::Role::Object;
2 use Moose::Role;
3 use Carp qw( carp );
4
5
6 sub instance { shift->new }
7
8 sub initialize {
9     my ( $class, @args ) = @_;
10
11     my $existing = $class->meta->existing_singleton;
12     confess "Singleton is already initialized" if $existing;
13
14     return $class->new(@args);
15 }
16
17 override new => sub {
18     my ( $class, @args ) = @_;
19
20     my $existing = $class->meta->existing_singleton;
21     confess "Singleton is already initialized" if $existing and @args;
22
23     # Otherwise BUILD will be called repeatedly on the existing instance.
24     # -- rjbs, 2008-02-03
25     return $existing if $existing and !@args;
26
27     return super();
28 };
29
30 sub _clear_instance {
31     my ($class) = @_;
32     $class->meta->clear_singleton;
33 }
34
35 no Moose::Role;
36
37 1;
38
39 # ABSTRACT: Object class role for MooseX::Singleton
40
41 __END__
42
43 =pod
44
45 =head1 DESCRIPTION
46
47 This just adds C<instance> as a shortcut for C<new>.
48
49 =cut
50