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