Don't use the instance method directly from Object -- use meta->construct_instance
[gitmo/MooseX-Singleton.git] / lib / MooseX / Singleton / Meta / Instance.pm
CommitLineData
109b110b 1#!/usr/bin/env perl
2package MooseX::Singleton::Meta::Instance;
3use Moose;
4use Scalar::Util 'weaken';
5
6extends 'Moose::Meta::Instance';
7
7afceec3 8sub get_singleton_instance {
109b110b 9 my ($self, $instance) = @_;
10
11 return $instance if blessed $instance;
ef266b1f 12
13 # optimization: it's really slow to go through new_object for every access
14 # so return the singleton if we see it already exists, which it will every
15 # single except the first.
16 no strict 'refs';
17 return ${"$instance\::singleton"} if defined ${"$instance\::singleton"};
18
68213909 19 return $instance->meta->construct_instance;
109b110b 20}
21
7afceec3 22sub clone_instance {
23 my ($self, $instance) = @_;
24 $self->get_singleton_instance($instance);
25}
26
109b110b 27sub get_slot_value {
28 my ($self, $instance, $slot_name) = @_;
7afceec3 29 $self->is_slot_initialized($instance, $slot_name) ? $self->get_singleton_instance($instance)->{$slot_name} : undef;
109b110b 30}
31
32sub set_slot_value {
33 my ($self, $instance, $slot_name, $value) = @_;
7afceec3 34 $self->get_singleton_instance($instance)->{$slot_name} = $value;
109b110b 35}
36
37sub deinitialize_slot {
38 my ( $self, $instance, $slot_name ) = @_;
7afceec3 39 delete $self->get_singleton_instance($instance)->{$slot_name};
109b110b 40}
41
42sub is_slot_initialized {
43 my ($self, $instance, $slot_name, $value) = @_;
7afceec3 44 exists $self->get_singleton_instance($instance)->{$slot_name} ? 1 : 0;
109b110b 45}
46
47sub weaken_slot_value {
48 my ($self, $instance, $slot_name) = @_;
7afceec3 49 weaken $self->get_singleton_instance($instance)->{$slot_name};
109b110b 50}
51
52sub inline_slot_access {
53 my ($self, $instance, $slot_name) = @_;
7afceec3 54 sprintf "%s->meta->instance_metaclass->get_singleton_instance(%s)->{%s}", $instance, $instance, $slot_name;
109b110b 55}
56
571;
58