File Coverage

blib/lib/MooseX/Singleton/Role/Meta/Instance.pm
Criterion Covered Total %
statement 18 18 100.0
branch 4 4 100.0
condition n/a
subroutine 5 5 100.0
pod 0 1 0.0
total 27 28 96.4


line stmt bran cond sub pod time code
1             package MooseX::Singleton::Role::Meta::Instance;
2 7     7   26 use Moose::Role;
  7         57  
  7         35  
3 7     7   22160 use Scalar::Util qw(weaken blessed);
  7         10  
  7         623  
4              
5             our $VERSION = '0.30';
6              
7             sub get_singleton_instance {
8 58     58 0 28179 my ( $self, $instance ) = @_;
9              
10 58 100       386 return $instance if blessed $instance;
11              
12             # optimization: it's really slow to go through new_object for every access
13             # so return the singleton if we see it already exists, which it will every
14             # single except the first.
15 7     7   29 no strict 'refs';
  7         9  
  7         2026  
16 18 100       19 return ${"$instance\::singleton"} if defined ${"$instance\::singleton"};
  15         121  
  18         88  
17              
18             # We need to go through ->new in order to make sure BUILD and
19             # BUILDARGS get called.
20 3         13 return $instance->meta->name->new;
21             }
22              
23             override clone_instance => sub {
24             my ( $self, $instance ) = @_;
25             $self->get_singleton_instance($instance);
26             };
27              
28             override get_slot_value => sub {
29             my ( $self, $instance, $slot_name ) = @_;
30             $self->is_slot_initialized( $instance, $slot_name )
31             ? $self->get_singleton_instance($instance)->{$slot_name}
32             : undef;
33             };
34              
35             override set_slot_value => sub {
36             my ( $self, $instance, $slot_name, $value ) = @_;
37             $self->get_singleton_instance($instance)->{$slot_name} = $value;
38             };
39              
40             override deinitialize_slot => sub {
41             my ( $self, $instance, $slot_name ) = @_;
42             delete $self->get_singleton_instance($instance)->{$slot_name};
43             };
44              
45             override is_slot_initialized => sub {
46             my ( $self, $instance, $slot_name, $value ) = @_;
47             exists $self->get_singleton_instance($instance)->{$slot_name} ? 1 : 0;
48             };
49              
50             override weaken_slot_value => sub {
51             my ( $self, $instance, $slot_name ) = @_;
52             weaken $self->get_singleton_instance($instance)->{$slot_name};
53             };
54              
55             override inline_slot_access => sub {
56             my ( $self, $instance, $slot_name ) = @_;
57             sprintf "%s->meta->instance_metaclass->get_singleton_instance(%s)->{%s}",
58             $instance, $instance, $slot_name;
59             };
60              
61 7     7   30 no Moose::Role;
  7         10  
  7         26  
62              
63             1;
64              
65             # ABSTRACT: Instance metaclass role for MooseX::Singleton
66              
67             __END__
68              
69             =pod
70              
71             =encoding UTF-8
72              
73             =head1 NAME
74              
75             MooseX::Singleton::Role::Meta::Instance - Instance metaclass role for MooseX::Singleton
76              
77             =head1 VERSION
78              
79             version 0.30
80              
81             =head1 DESCRIPTION
82              
83             =for Pod::Coverage *EVERYTHING*
84              
85             This role overrides all object access so that it gets the appropriate
86             singleton instance for the class.
87              
88             =head1 SUPPORT
89              
90             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-Singleton>
91             (or L<bug-MooseX-Singleton@rt.cpan.org|mailto:bug-MooseX-Singleton@rt.cpan.org>).
92              
93             There is also a mailing list available for users of this distribution, at
94             L<http://lists.perl.org/list/moose.html>.
95              
96             There is also an irc channel available for users of this distribution, at
97             L<C<#moose> on C<irc.perl.org>|irc://irc.perl.org/#moose>.
98              
99             =head1 AUTHOR
100              
101             Shawn M Moore <code@sartak.org>
102              
103             =head1 COPYRIGHT AND LICENSE
104              
105             This software is copyright (c) 2007 by Shawn M Moore.
106              
107             This is free software; you can redistribute it and/or modify it under
108             the same terms as the Perl 5 programming language system itself.
109              
110             =cut