Move actual singleton logic out of MooseX::Singleton::Object and into MooseX::Singlet...
[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;
12 return $instance->instance;
13}
14
7afceec3 15sub clone_instance {
16 my ($self, $instance) = @_;
17 $self->get_singleton_instance($instance);
18}
19
109b110b 20sub get_slot_value {
21 my ($self, $instance, $slot_name) = @_;
7afceec3 22 $self->is_slot_initialized($instance, $slot_name) ? $self->get_singleton_instance($instance)->{$slot_name} : undef;
109b110b 23}
24
25sub set_slot_value {
26 my ($self, $instance, $slot_name, $value) = @_;
7afceec3 27 $self->get_singleton_instance($instance)->{$slot_name} = $value;
109b110b 28}
29
30sub deinitialize_slot {
31 my ( $self, $instance, $slot_name ) = @_;
7afceec3 32 delete $self->get_singleton_instance($instance)->{$slot_name};
109b110b 33}
34
35sub is_slot_initialized {
36 my ($self, $instance, $slot_name, $value) = @_;
7afceec3 37 exists $self->get_singleton_instance($instance)->{$slot_name} ? 1 : 0;
109b110b 38}
39
40sub weaken_slot_value {
41 my ($self, $instance, $slot_name) = @_;
7afceec3 42 weaken $self->get_singleton_instance($instance)->{$slot_name};
109b110b 43}
44
45sub inline_slot_access {
46 my ($self, $instance, $slot_name) = @_;
7afceec3 47 sprintf "%s->meta->instance_metaclass->get_singleton_instance(%s)->{%s}", $instance, $instance, $slot_name;
109b110b 48}
49
501;
51