lib/Dancer/Config/Object.pm

filter: Statementstmt 98.4% 64 / 65 Branchbran 75.0% 12 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod 0.0% 0 / 1 totaltotal 93.6% 89 / 95 CC 11 SCAR 12.1
CC
11
Cov
94%
_add_methods
7.0
__ANON__
3.0
hashref_to_object
3.0
CRAP 3.4
« lib/Dancer/Hook/Properties.pm ↑ Summary blib/lib/Dancer/Session/Abstract.pm »
1 package Dancer::Config::Object;
2 243 our $AUTHORITY = 'cpan:SUKRIA';
3 #ABSTRACT: Access the config via methods instead of hashrefs
4 243 $Dancer::Config::Object::VERSION = '1.3522';
5 243 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 243 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 243 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 243 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 243 ▶use Dancer::Exception qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 243 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 243 register_exception('BadConfigMethod',
14 message_pattern =>
15 qq{Can't locate config attribute "%s".\nAvailable attributes: %s});
16  
17 243 our @EXPORT_OK = qw(hashref_to_object);
18  
19 {
20 243 my $index = 1;
21  
22 10 ▶ sub hashref_to_object {
Subroutine hashref_to_object: called CC=3 SCAR=11.1
Pod undocumented
23 10 my ($hashref) = @_;
24 10 my $class = __PACKAGE__;
25 10 my $target = "${class}::__ANON__$index";
26 10 $index++;
27 10 ▶ if ('HASH' ne ref $hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if ("HASH" ne ref $hashref) 1 9 10
28 1 ▶ if ( blessed $hashref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($hashref)) { } 1 0 1
29 # we have already converted this to an object. This can happen
30 # in cases where Dancer::Config->load is called more than
31 # once.
32 1 return $hashref;
33 }
34 else {
35 # should never happen
36 0 raise 'Core::Config' => "Argument to $class must be a hashref";
37 }
38 }
39 9 my $object = bless $hashref => $target;
40 9 _add_methods($object);
41  
42 9 return $object;
43 }
44 }
45  
46  
47 243 ▶sub _add_methods {
Subroutine _add_methods: called CC=7 SCAR=19.5
48 9 my ($object) = @_;
49 9 my $target = ref $object;
50  
51 9 foreach my $key ( keys %$object ) {
52 35 my $value = $object->{$key};
53 35 ▶ if ( 'HASH' eq ref $value ) {
Branch: 2 branches
BranchTrueFalseTotal
if ('HASH' eq ref $value) { } 6 29 35
elsif ('ARRAY' eq ref $value) { } 1 28 29
54 6 $value = hashref_to_object($value);
55 }
56 elsif ( 'ARRAY' eq ref $value ) {
57 1 foreach (@$value) {
58 2 ▶ $_ = 'HASH' eq ref($_) ? hashref_to_object($_) : $_;
Branch: 1 branch
BranchTrueFalseTotal
'HASH' eq ref $_ ? : 2 0 2
59 }
60 }
61  
62 # match a (more or less) valid identifier
63 35 ▶ next unless $key =~ qr/^[[:alpha:]_][[:word:]]*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ qr/^[[:alpha:]_][[:word:]]*$/ 2 33 35
64 33 my $method = "${target}::$key";
65 243 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
66 33 ▶ *$method = sub {$value};
Subroutine __ANON__: called CC=1 SCAR=0.0
67 }
68 9 _setup_bad_method_trap($target);
69 }
70  
71 # AUTOLOAD will only be called if a non-existent method is called. It's used
72 # to generate the list of available methods. It's slow, but we're going to
73 # die. Who wants to die quickly?
74 9 ▶sub _setup_bad_method_trap {
Subroutine _setup_bad_method_trap: called CC=1 SCAR=0.0
75 9 my ($target) = @_;
76 243 ▶ no strict; ## no critic (ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
77 9 ▶ *{"${target}::AUTOLOAD"} = sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
78 1 $AUTOLOAD =~ /.*::(.*)$/;
79  
80 # should never happen
81 1 ▶ my $bad_method = $1 ## no critic (ProhibitCaptureWithoutTest)
Branch: 1 branch
BranchTrueFalseTotal
my $bad_method = $1 or croak("Could not determine method called via $AUTOLOAD") 1 0 1
82 or croak "Could not determine method called via $AUTOLOAD";
83 1 ▶ return if 'DESTROY' eq $bad_method;
Branch: 1 branch
BranchTrueFalseTotal
if "DESTROY" eq $bad_method 0 1 1
84 1 my $symbol_table = "${target}::";
85  
86 # In these fake classes, we only have methods
87 my $methods =
88 1 join ', ' => grep { !/^(?:AUTOLOAD|DESTROY|$bad_method)$/ }
89 sort keys %$symbol_table;
90 1 raise BadConfigMethod => $bad_method, $methods;
91 9 };
92 }
93  
94 243 1;
95  
96 __END__
« lib/Dancer/Hook/Properties.pm ↑ Summary blib/lib/Dancer/Session/Abstract.pm »