Re-implementation. This uses a bit of Moose meta magic to get real singletons.
[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
8sub instantiate {
9 my ($self, $instance) = @_;
10
11 return $instance if blessed $instance;
12 return $instance->instance;
13}
14
15sub 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
20sub set_slot_value {
21 my ($self, $instance, $slot_name, $value) = @_;
22 $self->instantiate($instance)->{$slot_name} = $value;
23}
24
25sub deinitialize_slot {
26 my ( $self, $instance, $slot_name ) = @_;
27 delete $self->instantiate($instance)->{$slot_name};
28}
29
30sub is_slot_initialized {
31 my ($self, $instance, $slot_name, $value) = @_;
32 exists $self->instantiate($instance)->{$slot_name} ? 1 : 0;
33}
34
35sub weaken_slot_value {
36 my ($self, $instance, $slot_name) = @_;
37 weaken $self->instantiate($instance)->{$slot_name};
38}
39
40sub inline_slot_access {
41 my ($self, $instance, $slot_name) = @_;
42 sprintf "%s->meta->instance_metaclass->instantiate(%s)->{%s}", $instance, $instance, $slot_name;
43}
44
451;
46