File Coverage

blib/lib/Prancer/Core.pm
Criterion Covered Total %
statement 27 45 60.0
branch 0 10 0.0
condition n/a
subroutine 9 12 75.0
pod 2 3 66.6
total 38 70 54.2


line stmt bran cond sub pod time code
1             package Prancer::Core;
2              
3 1     1   4 use strict;
  1         2  
  1         31  
4 1     1   3 use warnings FATAL => 'all';
  1         1  
  1         24  
5              
6 1     1   3 use version;
  1         1  
  1         5  
7             our $VERSION = '1.04';
8              
9 1     1   63 use Try::Tiny;
  1         1  
  1         46  
10 1     1   3 use Carp;
  1         1  
  1         47  
11              
12 1     1   374 use Prancer::Config;
  1         2  
  1         34  
13              
14 1     1   6 use parent qw(Exporter);
  1         1  
  1         6  
15             our @EXPORT_OK = qw(config);
16              
17             # even though this *should* work automatically, it was not
18             our @CARP_NOT = qw(Prancer Try::Tiny);
19              
20             sub new {
21 0     0 0   my ($class, $configuration_file) = @_;
22              
23             # already got an object
24 0 0         return $class if ref($class);
25              
26             # this is a singleton
27 0           my $instance = undef;
28             {
29 1     1   94 no strict 'refs';
  1         1  
  1         105  
  0            
30 0           $instance = \${"${class}::_instance"};
  0            
31 0 0         return $$instance if defined($$instance);
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 0         if (defined($configuration_file)) {
39 0           $self->{'_config'} = Prancer::Config->load($configuration_file);
40             }
41              
42 0           $$instance = $self;
43 0           return $self;
44             }
45              
46             sub initialized {
47 0     0 1   my $class = shift;
48 1     1   5 no strict 'refs';
  1         1  
  1         99  
49 0 0         return (${"${class}::_instance"} ? 1 : 0);
  0            
50             }
51              
52             sub config {
53 0 0   0 1   die "core has not been initialized\n" unless Prancer::Core->initialized();
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;
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 to
83             more easily function. This package can be initialized and used on its own if
84             you want to use L 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. 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 for more details on how to load and use the configuration
104             data.
105              
106             =back
107              
108             =cut