Prancer/Core.pm

filter: Statementstmt 63.2% 31 / 49 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 75.0% 9 / 12 Podpod 66.6% 2 / 3 totaltotal 56.7% 42 / 74 CC 6 SCAR 15.9
CC
6
Cov
53%
new
14.1
config
6.0
initialized
3.0
CRAP 4.9
« Prancer/Config.pm ↑ Summary Prancer/Response.pm »
1 package Prancer::Core;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 our $VERSION = '1.05';
8  
9 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Prancer::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use parent qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 our @EXPORT_OK = qw(config);
16  
17 # even though this *should* work automatically, it was not
18 1 our @CARP_NOT = qw(Prancer Try::Tiny);
19  
20 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=26.4
Pod undocumented
21 0 my ($class, $configuration_file) = @_;
22  
23 # already got an object
24 0 ▶ return $class if ref($class);
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 0 0
25  
26 # this is a singleton
27 0 my $instance = undef;
28 {
29 0 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
30 0 $instance = \${"${class}::_instance"};
31 0 ▶ return $$instance if defined($$instance);
Branch: 1 branch
BranchTrueFalseTotal
if defined $$instance 0 0 0
32 }
33  
34 # ok so the singleton doesn't exist so create an instance
35 0 my $self = bless({}, $class);
36  
37 # load configuration options if we were given a config file
38 0 ▶ if (defined($configuration_file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $configuration_file) 0 0 0
39 0 $self->{'_config'} = Prancer::Config->load($configuration_file);
40 }
41  
42 0 $$instance = $self;
43 0 return $self;
44 }
45  
46 0 ▶sub initialized {
Subroutine initialized: not called CC=2 SCAR=10.9
Pod documented
47 0 my $class = shift;
48 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
49 0 ▶ return (${"${class}::_instance"} ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
${"${class}::_instance";} ? : 0 0 0
50 }
51  
52 0 ▶sub config {
Subroutine config: not called CC=2 SCAR=17.9
Pod documented
53 0 ▶ die "core has not been initialized\n" unless Prancer::Core->initialized();
Branch: 1 branch
BranchTrueFalseTotal
unless "Prancer::Core"->initialized 0 0 0
54  
55 # because this method takes no arguments we don't spend any effort trying
56 # to figure out if the first argument is an instance of the package or the
57 # name of the package or anything like that. and because the previous
58 # statement guarantees that we've already been initialized then we'll just
59 # get an instance of ourselves and use that. no muss, no fuss.
60 0 my $self = Prancer::Core->new();
61  
62 0 return $self->{'_config'};
63 }
64  
65 1 1;
66  
67 =head1 NAME
68  
69 Prancer::Core
70  
71 =head1 SYNOPSIS
72  
73 use Prancer::Core qw(config);
74  
75 my $core = Prancer::Core->new('/path/to/config.yml');
76 my $foo = $core->config->get('foo');
77 my $bar = Prancer::Core->new->config->get('bar');
78 my $baz = config->get('baz');
79  
80 =head1 DESCRIPTION
81  
82 This class is a singleton that contains some core methods for L<Prancer> to
83 more easily function. This package can be initialized and used on its own if
84 you want to use L<Prancer> outside of a PSGI application.
85  
86 =head1 METHODS
87  
88 =over
89  
90 =item initialized
91  
92 Since this package is a singleton, it might happen that you have a place in
93 your code where you try to use a method from this package before you are able
94 to initialize it with the necessary arguments to C<new>. This will tell you if
95 this package has been initialized.
96  
97 die "core has not been initialized" unless Prancer::Core->initialized();
98 print Prancer::Core->new->config->get('foo');
99  
100 =item config
101  
102 Returns the configuration options parsed when this package was initialized. See
103 L<Prancer::Config> for more details on how to load and use the configuration
104 data.
105  
106 =back
107  
108 =cut
« Prancer/Config.pm ↑ Summary Prancer/Response.pm »