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 CC 4 SCAR 4.8
CC
4
Cov
100%
__ANON__
3.0
instance
2.0
BEGIN
1.0
CRAP 1.6
« lib/Dancer/MIME.pm ↑ Summary blib/lib/Dancer/Handler/Debug.pm »
1 package Dancer::Object::Singleton;
2 249 our $AUTHORITY = 'cpan:SUKRIA';
3 #ABSTRACT: Singleton base class for Dancer
4 249 $Dancer::Object::Singleton::VERSION = '1.3522';
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 249 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 249 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 249 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 249 ▶use Dancer::Exception qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 249 ▶use base qw(Dancer::Object);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # pool of instances (only one per package name)
16 249 my %instances;
17  
18 # constructor
19 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
20 1 my ($class) = @_;
21 1 raise core => "you can't call 'new' on $class, as it's a singleton. Try to call 'instance'";
22 }
23  
24 1 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
25 1 my ($class) = @_;
26 1 raise core => "you can't call 'clone' on $class, as it's a singleton. Try to call 'instance'";
27 }
28  
29 4420 ▶sub instance {
Subroutine instance: called CC=2 SCAR=6.9
Pod documented
30 4420 my ($class) = @_;
31 4420 my $instance = $instances{$class};
32  
33 # if exists already
34 4420 ▶ defined $instance
Branch: 1 branch
BranchTrueFalseTotal
defined $instance and return $instance 4156 264 4420
35 and return $instance;
36  
37 # create the instance
38 264 $instance = bless {}, $class;
39 264 $class->init($instance);
40  
41 # save and return it
42 264 $instances{$class} = $instance;
43 264 return $instance;
44 }
45  
46 # accessor code for singleton objects
47 # (overloaded from Dancer::Object)
48 957 ▶sub _setter_code {
Subroutine _setter_code: called CC=1 SCAR=0.0
49 957 my ($class, $attr) = @_;
50 12040 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
51 12040 my ($class_or_instance, $value) = @_;
52 12040 ▶ my $instance = ref $class_or_instance ?
Branch: 1 branch
BranchTrueFalseTotal
ref $class_or_instance ? : 10392 1648 12040
53 $class_or_instance : $class_or_instance->instance;
54 12040 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 11512 528 12040
55 11512 return $instance->{$attr};
56 }
57 else {
58 528 return $instance->{$attr} = $value;
59 }
60 957 };
61 }
62  
63 249 1;
64  
65 __END__
« lib/Dancer/MIME.pm ↑ Summary blib/lib/Dancer/Handler/Debug.pm »