From: Ricardo SIGNES Date: Sat, 26 Jan 2008 15:02:25 +0000 (+0000) Subject: initialize method X-Git-Tag: 0.09_02~21 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMooseX-Singleton.git;a=commitdiff_plain;h=d928ce3a26634463da667f6bdba903e94b31ebac initialize method --- diff --git a/lib/MooseX/Singleton/Object.pm b/lib/MooseX/Singleton/Object.pm index 942e149..8ad2a4d 100644 --- a/lib/MooseX/Singleton/Object.pm +++ b/lib/MooseX/Singleton/Object.pm @@ -7,6 +7,15 @@ extends 'Moose::Object'; sub instance { shift->new } +sub initialize { + my ($class, @args) = @_; + + my $existing = $class->meta->existing_singleton; + confess "Singleton is already initialized" if $existing; + + return $class->SUPER::new(@args); +} + sub new { my ($class, @args) = @_; diff --git a/t/002-init.t b/t/002-init.t index 2c25897..8b5f144 100644 --- a/t/002-init.t +++ b/t/002-init.t @@ -1,6 +1,6 @@ use strict; use warnings; -use Test::More tests => 8; +use Test::More tests => 9; my $i = 0; sub new_singleton_pkg { @@ -42,11 +42,14 @@ for my $pkg (new_singleton_pkg) { ); eval { $pkg->new(number => 3) }; - ok($@, "can't make new singleton with conflicting attributes"); + like($@, qr/already/, "can't make new singleton with conflicting attributes"); my $second = eval { $pkg->new }; ok(!$@, "...but a second ->new without args is okay"); is($second->number, 5, "...we get the originally inited number from it"); + + eval { $pkg->initialize }; + like($@, qr/already/, "...but ->initialize() is still an error"); }