Re-implementation. This uses a bit of Moose meta magic to get real singletons.
[gitmo/MooseX-Singleton.git] / lib / MooseX / Singleton / Meta / Instance.pm
1 #!/usr/bin/env perl
2 package MooseX::Singleton::Meta::Instance;
3 use Moose;
4 use Scalar::Util 'weaken';
5
6 extends 'Moose::Meta::Instance';
7
8 sub instantiate {
9     my ($self, $instance) = @_;
10
11     return $instance if blessed $instance;
12     return $instance->instance;
13 }
14
15 sub get_slot_value {
16     my ($self, $instance, $slot_name) = @_;
17     $self->is_slot_initialized($instance, $slot_name) ? $self->instantiate($instance)->{$slot_name} : undef;
18 }
19
20 sub set_slot_value {
21     my ($self, $instance, $slot_name, $value) = @_;
22     $self->instantiate($instance)->{$slot_name} = $value;
23 }
24
25 sub deinitialize_slot {
26     my ( $self, $instance, $slot_name ) = @_;
27     delete $self->instantiate($instance)->{$slot_name};
28 }
29
30 sub is_slot_initialized {
31     my ($self, $instance, $slot_name, $value) = @_;
32     exists $self->instantiate($instance)->{$slot_name} ? 1 : 0;
33 }
34
35 sub weaken_slot_value {
36     my ($self, $instance, $slot_name) = @_;
37     weaken $self->instantiate($instance)->{$slot_name};
38 }
39
40 sub inline_slot_access {
41     my ($self, $instance, $slot_name) = @_;
42     sprintf "%s->meta->instance_metaclass->instantiate(%s)->{%s}", $instance, $instance, $slot_name;
43 }
44
45 1;
46