blib/lib/Dancer2/Core/Role/DSL.pm

filter: Statementstmt 93.4% 57 / 61 Branchbran 68.7% 11 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 3 totaltotal 87.2% 82 / 94 CC 11 SCAR 8.0
CC
11
Cov
88%
register
4.1
export_symbols_to
3.0
_construct_export_map
3.0
CRAP 2.2
« blib/lib/Dancer2/Core/Role/ConfigReader.pm ↑ Summary blib/lib/Dancer2/Core/Role/Logger.pm »
1 package Dancer2::Core::Role::DSL;
2 # ABSTRACT: Role for DSL
3 160 $Dancer2::Core::Role::DSL::VERSION = '2.2.1';
4 160 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 160 ▶use Dancer2::Core::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 160 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 160 ▶use Scalar::Util qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 160 with 'Dancer2::Core::Role::Hookable';
10  
11 160 has app => ( is => 'ro', required => 1 );
12  
13 160 has keywords => (
14 is => 'rw',
15 isa => HashRef,
16 lazy => 1,
17 builder => '_build_dsl_keywords',
18 );
19  
20 sub _build_dsl_keywords {
21 257 ▶ my ($self) = @_;
Subroutine _build_dsl_keywords: called CC=2 SCAR=7.2
22 257 ▶ $self->can('dsl_keywords')
Branch: 1 branch
BranchTrueFalseTotal
$self->can('dsl_keywords') ? : 257 0 257
23 ? $self->dsl_keywords
24 : {};
25 }
26  
27 sub register {
28 2 ▶ my ( $self, $keyword, $is_global ) = @_;
Subroutine register: called CC=3 SCAR=14.2
Pod undocumented
29 2 my $keywords = $self->keywords;
30 2 my $pkg = ref($self);
31 2 $pkg =~ s/__WITH__.+$//;
32  
33 2 ▶ if ( exists $keywords->{$keyword} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $keywords->{$keyword}) 0 2 2
34 0 my $reg_pkg = $keywords->{$keyword}{'pkg'};
35 0 $reg_pkg =~ s/__WITH__.+$//;
36 0 ▶ $reg_pkg eq $pkg and return;
Branch: 1 branch
BranchTrueFalseTotal
$reg_pkg eq $pkg and return 0 0 0
37  
38 0 croak "[$pkg] Keyword $keyword already registered by $reg_pkg";
39 }
40  
41 2 $keywords->{$keyword} = { is_global => $is_global, pkg => $pkg };
42 }
43  
44 20 ▶sub dsl { $_[0] }
Subroutine dsl: called CC=1 SCAR=0.0
Pod undocumented
45  
46 # exports new symbol to caller
47 sub export_symbols_to {
48 257 ▶ my ( $self, $caller, $args ) = @_;
Subroutine export_symbols_to: called CC=3 SCAR=11.0
Pod undocumented
49 257 my $exports = $self->_construct_export_map($args);
50  
51 ## no critic
52 257 foreach my $export ( keys %{$exports} ) {
53 160 ▶ no strict 'refs'; ## no critic (TestingAndDebugging::ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
54 20564 my $existing = *{"${caller}::${export}"}{CODE};
55  
56 20564 ▶ next if defined $existing;
Branch: 1 branch
BranchTrueFalseTotal
if defined $existing 418 20146 20564
57  
58 20146 *{"${caller}::${export}"} = $exports->{$export};
59 }
60 ## use critic
61  
62 257 return keys %{$exports};
63 }
64  
65 # private
66  
67 sub _compile_keyword {
68 20564 ▶ my ( $self, $keyword, $opts ) = @_;
Subroutine _compile_keyword: called CC=2 SCAR=7.0
69  
70 my $code = $opts->{is_global}
71 1189 ▶ ? sub { $self->$keyword(@_) }
Subroutine __ANON__: called CC=1 SCAR=0.0
72 : sub {
73 646 ▶ croak "Function '$keyword' must be called from a route handler"
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Dancer2::Core::Route::REQUEST 0 646 646
74 unless defined $Dancer2::Core::Route::REQUEST;
75  
76 646 $self->$keyword(@_)
77 20564 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'is_global'} ? : 10542 10022 20564
78  
79 20564 return $self->_apply_prototype($code, $opts);
80 }
81  
82 sub _apply_prototype {
83 20564 ▶ my ($self, $code, $opts) = @_;
Subroutine _apply_prototype: called CC=2 SCAR=6.9
84  
85 # set prototype if one is defined for the keyword. undef => no prototype
86 20564 my $prototype;
87 20564 ▶ exists $opts->{'prototype'} and $prototype = $opts->{'prototype'};
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{"prototype"} and $prototype = $opts->{"prototype"} 517 20047 20564
88 20564 return Scalar::Util::set_prototype( \&$code, $prototype );
89 }
90  
91 sub _construct_export_map {
92 257 ▶ my ( $self, $args ) = @_;
Subroutine _construct_export_map: called CC=3 SCAR=11.0
93 257 my $keywords = $self->keywords;
94 257 my %map;
95 257 foreach my $keyword ( keys %$keywords ) {
96 # check if the keyword were excluded from importation
97 20569 ▶ $args->{ '!' . $keyword } and next;
Branch: 1 branch
BranchTrueFalseTotal
$$args{'!' . $keyword} and next 5 20564 20569
98 20564 $map{$keyword} = $self->_compile_keyword( $keyword, $keywords->{$keyword} );
99 }
100 257 return \%map;
101 }
102  
103 160 1;
104  
105 __END__
« blib/lib/Dancer2/Core/Role/ConfigReader.pm ↑ Summary blib/lib/Dancer2/Core/Role/Logger.pm »