blib/lib/Dancer/Object/Singleton.pm

filter: Statementstmt 100% 37 / 37 Branchbran 100% 6 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 100% 56 / 56 SCAR 13.9
CC
4
Cov
100%
__ANON__
3.0
instance
2.0
BEGIN
1.0
CRAP 4.0
« blib/lib/Dancer/Handler/Debug.pm ↑ Summary blib/lib/Dancer/Serializer/Dumper.pm »
1 package Dancer::Object::Singleton;
2 240 our $AUTHORITY = 'cpan:SUKRIA';
3 #ABSTRACT: Singleton base class for Dancer
4 240 $Dancer::Object::Singleton::VERSION = '1.3520';
5 # This class is a root class for singleton objects in Dancer.
6 # It provides basic OO singleton tools for Perl5 without being... MooseX::Singleton ;-)
7  
8 240 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 240 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 240 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 240 ▶use Dancer::Exception qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 240 ▶use base qw(Dancer::Object);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # pool of instances (only one per package name)
16 240 my %instances;
17  
18 # constructor
19 sub new {
20 1 ▶ my ($class) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
21 1 raise core => "you can't call 'new' on $class, as it's a singleton. Try to call 'instance'";
22 }
23  
24 sub clone {
25 1 ▶ my ($class) = @_;
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
26 1 raise core => "you can't call 'clone' on $class, as it's a singleton. Try to call 'instance'";
27 }
28  
29 sub instance {
30 4359 ▶ my ($class) = @_;
Subroutine instance: called CC=2 SCAR=6.9
Pod documented
31 4359 my $instance = $instances{$class};
32  
33 # if exists already
34 4359 ▶ defined $instance
Branch: 1 branch
BranchTrueFalseTotal
defined $instance and return $instance 4104 255 4359
35 and return $instance;
36  
37 # create the instance
38 255 $instance = bless {}, $class;
39 255 $class->init($instance);
40  
41 # save and return it
42 255 $instances{$class} = $instance;
43 255 return $instance;
44 }
45  
46 # accessor code for singleton objects
47 # (overloaded from Dancer::Object)
48 sub _setter_code {
49 921 ▶ my ($class, $attr) = @_;
Subroutine _setter_code: called CC=1 SCAR=0.0
50 sub {
51 11702 ▶ my ($class_or_instance, $value) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
52 11702 ▶ my $instance = ref $class_or_instance ?
Branch: 1 branch
BranchTrueFalseTotal
ref $class_or_instance ? : 10054 1648 11702
53 $class_or_instance : $class_or_instance->instance;
54 11702 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 11192 510 11702
55 11192 return $instance->{$attr};
56 }
57 else {
58 510 return $instance->{$attr} = $value;
59 }
60 921 };
61 }
62  
63 240 1;
64  
65 __END__
« blib/lib/Dancer/Handler/Debug.pm ↑ Summary blib/lib/Dancer/Serializer/Dumper.pm »