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