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