Do a bit of fixing. instance and new are one and the same. More tests to ensure that...
[gitmo/MooseX-Singleton.git] / lib / MooseX / Singleton.pm
index 38f22ec..c0bb14d 100644 (file)
@@ -1,27 +1,24 @@
 package MooseX::Singleton;
-
 use Moose::Role;
 
-our $VERSION = 0.01;
+our $VERSION = 0.02;
 
 override new => sub {
-  my ($class) = @_;
-
-  no strict qw/refs/;
+    my ($class) = @_;
 
-  my $instance = super;
+    no strict 'refs';
 
-  ${"$class\::singleton"} = $instance;
+    # create our instance if we don't already have one
+    if (!defined ${"$class\::singleton"}) {
+        ${"$class\::singleton"} = super;
+    }
 
-  return $instance;
+    return ${"$class\::singleton"};
 };
 
+# instance really is the same as new. any ideas for a better implementation?
 sub instance {
-  my ($class) = @_;
-
-  no strict qw/refs/;
-
-  return ${"$class\::singleton"};
+    shift->new(@_);
 }
 
 1;