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
« lib/Dancer2/Core/Role/Logger.pm ↑ Summary lib/Dancer2/Core/Role/HasEnvironment.pm »
1 package Dancer2::Core::Role::DSL;
2 # ABSTRACT: Role for DSL
3 163 $Dancer2::Core::Role::DSL::VERSION = '2.3.0';
4 163 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 163 ▶use Dancer2::Core::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 163 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 163 ▶use Scalar::Util qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 163 with 'Dancer2::Core::Role::Hookable';
10  
11 163 has app => ( is => 'ro', required => 1 );
12  
13 163 has keywords => (
14 is => 'rw',
15 isa => HashRef,
16 lazy => 1,
17 builder => '_build_dsl_keywords',
18 );
19  
20 261 ▶sub _build_dsl_keywords {
Subroutine _build_dsl_keywords: called CC=2 SCAR=7.2
21 261 my ($self) = @_;
22 261 ▶ $self->can('dsl_keywords')
Branch: 1 branch
BranchTrueFalseTotal
$self->can('dsl_keywords') ? : 261 0 261
23 ? $self->dsl_keywords
24 : {};
25 }
26  
27 2 ▶sub register {
Subroutine register: called CC=3 SCAR=14.2
Pod undocumented
28 2 my ( $self, $keyword, $is_global ) = @_;
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 261 ▶sub export_symbols_to {
Subroutine export_symbols_to: called CC=3 SCAR=11.0
Pod undocumented
48 261 my ( $self, $caller, $args ) = @_;
49 261 my $exports = $self->_construct_export_map($args);
50  
51 ## no critic
52 261 foreach my $export ( keys %{$exports} ) {
53 163 ▶ no strict 'refs'; ## no critic (TestingAndDebugging::ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
54 21145 my $existing = *{"${caller}::${export}"}{CODE};
55  
56 21145 ▶ next if defined $existing;
Branch: 1 branch
BranchTrueFalseTotal
if defined $existing 423 20722 21145
57  
58 20722 *{"${caller}::${export}"} = $exports->{$export};
59 }
60 ## use critic
61  
62 261 return keys %{$exports};
63 }
64  
65 # private
66  
67 21145 ▶sub _compile_keyword {
Subroutine _compile_keyword: called CC=2 SCAR=7.0
68 21145 my ( $self, $keyword, $opts ) = @_;
69  
70 my $code = $opts->{is_global}
71 1205 ▶ ? sub { $self->$keyword(@_) }
Subroutine __ANON__: called CC=1 SCAR=0.0
72 651 ▶ : sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
73 651 ▶ croak "Function '$keyword' must be called from a route handler"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Dancer2::Core::Route::REQUEST 0 651 651
74 unless defined $Dancer2::Core::Route::REQUEST;
75  
76 651 $self->$keyword(@_)
77 21145 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'is_global'} ? : 10967 10178 21145
78  
79 21145 return $self->_apply_prototype($code, $opts);
80 }
81  
82 21145 ▶sub _apply_prototype {
Subroutine _apply_prototype: called CC=2 SCAR=6.9
83 21145 my ($self, $code, $opts) = @_;
84  
85 # set prototype if one is defined for the keyword. undef => no prototype
86 21145 my $prototype;
87 21145 ▶ exists $opts->{'prototype'} and $prototype = $opts->{'prototype'};
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{"prototype"} and $prototype = $opts->{"prototype"} 525 20620 21145
88 21145 return Scalar::Util::set_prototype( \&$code, $prototype );
89 }
90  
91 261 ▶sub _construct_export_map {
Subroutine _construct_export_map: called CC=3 SCAR=11.0
92 261 my ( $self, $args ) = @_;
93 261 my $keywords = $self->keywords;
94 261 my %map;
95 261 foreach my $keyword ( keys %$keywords ) {
96 # check if the keyword were excluded from importation
97 21150 ▶ $args->{ '!' . $keyword } and next;
Branch: 1 branch
BranchTrueFalseTotal
$$args{'!' . $keyword} and next 5 21145 21150
98 21145 $map{$keyword} = $self->_compile_keyword( $keyword, $keywords->{$keyword} );
99 }
100 261 return \%map;
101 }
102  
103 163 1;
104  
105 __END__
« lib/Dancer2/Core/Role/Logger.pm ↑ Summary lib/Dancer2/Core/Role/HasEnvironment.pm »