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;
has bag => (
is => 'rw',
isa => 'HashRef[Int]',
- default => sub { {} },
+ default => sub { { default => 42 } },
);
sub distinct_keys {
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");