Commit | Line | Data |
---|---|---|
8eec3c69 | 1 | package MooseX::Singleton::Role::Object; |
2 | use Moose::Role; | |
5a0f3fa6 | 3 | use Carp qw( carp ); |
4 | ||
1148f62f | 5 | our $VERSION = '0.23'; |
5a0f3fa6 | 6 | $VERSION = eval $VERSION; |
109b110b | 7 | |
109b110b | 8 | sub instance { shift->new } |
9 | ||
d928ce3a | 10 | sub 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 | 19 | override 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 | 32 | sub _clear_instance { |
4c256923 | 33 | my ($class) = @_; |
34 | $class->meta->clear_singleton; | |
03e1b8df | 35 | } |
36 | ||
2cb90d53 | 37 | no Moose::Role; |
2b4ce4bd | 38 | |
109b110b | 39 | 1; |
40 | ||
b375b147 | 41 | __END__ |
42 | ||
43 | =pod | |
44 | ||
45 | =head1 NAME | |
46 | ||
e2391860 | 47 | MooseX::Singleton::Role::Object - Object class role for MooseX::Singleton |
b375b147 | 48 | |
49 | =head1 DESCRIPTION | |
50 | ||
51 | This just adds C<instance> as a shortcut for C<new>. | |
52 | ||
53 | =cut | |
54 |