From: Shawn M Moore Date: Sun, 16 Dec 2007 16:18:54 +0000 (+0000) Subject: Rename Meta::Instance->instantiate to Meta::Instance->get_singleton_instance. X-Git-Tag: 0.09_02~28 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=7afceec34ad9f9882ac0c9dd6fbe4d55896bff62;p=gitmo%2FMooseX-Singleton.git Rename Meta::Instance->instantiate to Meta::Instance->get_singleton_instance. Test that default values are set. Meta::Instance->clone_instance (which is more or less a no-op) --- diff --git a/lib/MooseX/Singleton/Meta/Instance.pm b/lib/MooseX/Singleton/Meta/Instance.pm index d308120..e9c2522 100644 --- a/lib/MooseX/Singleton/Meta/Instance.pm +++ b/lib/MooseX/Singleton/Meta/Instance.pm @@ -5,41 +5,46 @@ use Scalar::Util 'weaken'; extends 'Moose::Meta::Instance'; -sub instantiate { +sub get_singleton_instance { my ($self, $instance) = @_; return $instance if blessed $instance; return $instance->instance; } +sub clone_instance { + my ($self, $instance) = @_; + $self->get_singleton_instance($instance); +} + sub get_slot_value { my ($self, $instance, $slot_name) = @_; - $self->is_slot_initialized($instance, $slot_name) ? $self->instantiate($instance)->{$slot_name} : undef; + $self->is_slot_initialized($instance, $slot_name) ? $self->get_singleton_instance($instance)->{$slot_name} : undef; } sub set_slot_value { my ($self, $instance, $slot_name, $value) = @_; - $self->instantiate($instance)->{$slot_name} = $value; + $self->get_singleton_instance($instance)->{$slot_name} = $value; } sub deinitialize_slot { my ( $self, $instance, $slot_name ) = @_; - delete $self->instantiate($instance)->{$slot_name}; + delete $self->get_singleton_instance($instance)->{$slot_name}; } sub is_slot_initialized { my ($self, $instance, $slot_name, $value) = @_; - exists $self->instantiate($instance)->{$slot_name} ? 1 : 0; + exists $self->get_singleton_instance($instance)->{$slot_name} ? 1 : 0; } sub weaken_slot_value { my ($self, $instance, $slot_name) = @_; - weaken $self->instantiate($instance)->{$slot_name}; + weaken $self->get_singleton_instance($instance)->{$slot_name}; } sub inline_slot_access { my ($self, $instance, $slot_name) = @_; - sprintf "%s->meta->instance_metaclass->instantiate(%s)->{%s}", $instance, $instance, $slot_name; + sprintf "%s->meta->instance_metaclass->get_singleton_instance(%s)->{%s}", $instance, $instance, $slot_name; } 1; diff --git a/t/001-basic.t b/t/001-basic.t index 99ed3de..78caa63 100644 --- a/t/001-basic.t +++ b/t/001-basic.t @@ -9,7 +9,7 @@ BEGIN { has bag => ( is => 'rw', isa => 'HashRef[Int]', - default => sub { {} }, + default => sub { { default => 42 } }, ); sub distinct_keys { @@ -34,35 +34,35 @@ BEGIN { my $mst = MooseX::Singleton::Test->instance; isa_ok($mst, 'MooseX::Singleton::Test', 'Singleton->instance returns a real instance'); -is($mst->distinct_keys, 0, "no keys yet"); +is($mst->distinct_keys, 1, "default keys"); $mst->add(foo => 10); -is($mst->distinct_keys, 1, "one key"); +is($mst->distinct_keys, 2, "added key"); $mst->add(bar => 5); -is($mst->distinct_keys, 2, "two keys"); +is($mst->distinct_keys, 3, "added another key"); my $mst2 = MooseX::Singleton::Test->instance; isa_ok($mst2, 'MooseX::Singleton::Test', 'Singleton->instance returns a real instance'); -is($mst2->distinct_keys, 2, "two keys, from before"); +is($mst2->distinct_keys, 3, "keys from before"); $mst->add(baz => 2); -is($mst->distinct_keys, 3, "three keys"); -is($mst2->distinct_keys, 3, "attributes are shared even after ->instance"); +is($mst->distinct_keys, 4, "attributes are shared even after ->instance"); +is($mst2->distinct_keys, 4, "attributes are shared even after ->instance"); -is(MooseX::Singleton::Test->distinct_keys, 3, "three keys even when Package->distinct_keys"); +is(MooseX::Singleton::Test->distinct_keys, 4, "Package->reader works"); MooseX::Singleton::Test->add(quux => 9000); -is($mst->distinct_keys, 4, "Package->add works fine"); -is($mst2->distinct_keys, 4, "Package->add works fine"); -is(MooseX::Singleton::Test->distinct_keys, 4, "Package->add works fine"); +is($mst->distinct_keys, 5, "Package->add works"); +is($mst2->distinct_keys, 5, "Package->add works"); +is(MooseX::Singleton::Test->distinct_keys, 5, "Package->add works"); MooseX::Singleton::Test->clear; -is($mst->distinct_keys, 0, "Package->clear works fine"); -is($mst2->distinct_keys, 0, "Package->clear works fine"); -is(MooseX::Singleton::Test->distinct_keys, 0, "Package->clear works fine"); +is($mst->distinct_keys, 0, "Package->clear works"); +is($mst2->distinct_keys, 0, "Package->clear works"); +is(MooseX::Singleton::Test->distinct_keys, 0, "Package->clear works");