bump version to 0.25
[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
0a71b1e6 5our $VERSION = '0.25';
5a0f3fa6 6$VERSION = eval $VERSION;
109b110b 7
109b110b 8sub instance { shift->new }
9
d928ce3a 10sub initialize {
4c256923 11 my ( $class, @args ) = @_;
d928ce3a 12
4c256923 13 my $existing = $class->meta->existing_singleton;
14 confess "Singleton is already initialized" if $existing;
d928ce3a 15
4c256923 16 return $class->new(@args);
d928ce3a 17}
18
8eec3c69 19override new => sub {
4c256923 20 my ( $class, @args ) = @_;
1de95613 21
4c256923 22 my $existing = $class->meta->existing_singleton;
23 confess "Singleton is already initialized" if $existing and @args;
1de95613 24
4c256923 25 # Otherwise BUILD will be called repeatedly on the existing instance.
26 # -- rjbs, 2008-02-03
27 return $existing if $existing and !@args;
963b26bd 28
4c256923 29 return super();
8eec3c69 30};
1de95613 31
03e1b8df 32sub _clear_instance {
4c256923 33 my ($class) = @_;
34 $class->meta->clear_singleton;
03e1b8df 35}
36
2cb90d53 37no Moose::Role;
2b4ce4bd 38
109b110b 391;
40
b375b147 41__END__
42
43=pod
44
45=head1 NAME
46
e2391860 47MooseX::Singleton::Role::Object - Object class role for MooseX::Singleton
b375b147 48
49=head1 DESCRIPTION
50
51This just adds C<instance> as a shortcut for C<new>.
52
53=cut
54