File Coverage

blib/lib/Dancer2/Core/Role/DSL.pm
Criterion Covered Total %
statement 52 56 92.8
branch 11 16 68.7
condition n/a
subroutine 15 15 100.0
pod 0 3 0.0
total 78 90 86.6


line stmt bran cond sub pod time code
1             # ABSTRACT: Role for DSL
2             $Dancer2::Core::Role::DSL::VERSION = '0.400000';
3             use Moo::Role;
4 135     135   82237 use Dancer2::Core::Types;
  135         331  
  135         907  
5 135     135   57486 use Carp 'croak';
  135         307  
  135         1137  
6 135     135   1101976 use Scalar::Util qw();
  135         324  
  135         7913  
7 135     135   866  
  135         284  
  135         44096  
8             with 'Dancer2::Core::Role::Hookable';
9              
10             has app => ( is => 'ro', required => 1 );
11              
12             has keywords => (
13             is => 'rw',
14             isa => HashRef,
15             lazy => 1,
16             builder => '_build_dsl_keywords',
17             );
18              
19             my ($self) = @_;
20             $self->can('dsl_keywords')
21 208     208   1878 ? $self->dsl_keywords
22 208 50       1541 : {};
23             }
24              
25             my ( $self, $keyword, $is_global ) = @_;
26             my $keywords = $self->keywords;
27             my $pkg = ref($self);
28 2     2 0 31 $pkg =~ s/__WITH__.+$//;
29 2         34  
30 2         51 if ( exists $keywords->{$keyword} ) {
31 2         3 my $reg_pkg = $keywords->{$keyword}{'pkg'};
32             $reg_pkg =~ s/__WITH__.+$//;
33 2 50       8 $reg_pkg eq $pkg and return;
34 0         0  
35 0         0 croak "[$pkg] Keyword $keyword already registered by $reg_pkg";
36 0 0       0 }
37              
38 0         0 $keywords->{$keyword} = { is_global => $is_global, pkg => $pkg };
39             }
40              
41 2         16  
42             # exports new symbol to caller
43             my ( $self, $caller, $args ) = @_;
44 26     26 0 67 my $exports = $self->_construct_export_map($args);
45              
46             ## no critic
47             foreach my $export ( keys %{$exports} ) {
48 208     208 0 630 no strict 'refs'; ## no critic (TestingAndDebugging::ProhibitNoStrict)
49 208         748 my $existing = *{"${caller}::${export}"}{CODE};
50              
51             next if defined $existing;
52 208         528  
  208         2270  
53 135     135   2725 *{"${caller}::${export}"} = $exports->{$export};
  135         301  
  135         50758  
54 16852         17108 }
  16852         43850  
55             ## use critic
56 16852 100       25180  
57             return keys %{$exports};
58 16430         17667 }
  16430         29188  
59              
60             # private
61              
62 208         1149 my ( $self, $keyword, $opts ) = @_;
  208         15917390  
63              
64             my $code = $opts->{is_global}
65             ? sub { $self->$keyword(@_) }
66             : sub {
67             croak "Function '$keyword' must be called from a route handler"
68 16852     16852   23087 unless defined $Dancer2::Core::Route::REQUEST;
69              
70             $self->$keyword(@_)
71 837     837   329489 };
        826      
72              
73 471 50   471   36143 return $self->_apply_prototype($code, $opts);
74             }
75              
76 471         2225 my ($self, $code, $opts) = @_;
77 16852 100       56107  
78             # set prototype if one is defined for the keyword. undef => no prototype
79 16852         27276 my $prototype;
80             exists $opts->{'prototype'} and $prototype = $opts->{'prototype'};
81             return Scalar::Util::set_prototype( \&$code, $prototype );
82             }
83 16852     16852   21075  
84             my ( $self, $args ) = @_;
85             my $keywords = $self->keywords;
86 16852         16620 my %map;
87 16852 100       23689 foreach my $keyword ( keys %$keywords ) {
88 16852         48271 # check if the keyword were excluded from importation
89             $args->{ '!' . $keyword } and next;
90             $map{$keyword} = $self->_compile_keyword( $keyword, $keywords->{$keyword} );
91             }
92 208     208   448 return \%map;
93 208         3154 }
94 208         5304  
95 208         2692 1;
96              
97 16857 100       31099  
98 16852         23139 =pod
99              
100 208         1356 =encoding UTF-8
101              
102             =head1 NAME
103              
104             Dancer2::Core::Role::DSL - Role for DSL
105              
106             =head1 VERSION
107              
108             version 0.400000
109              
110             =head1 AUTHOR
111              
112             Dancer Core Developers
113              
114             =head1 COPYRIGHT AND LICENSE
115              
116             This software is copyright (c) 2022 by Alexis Sukrieh.
117              
118             This is free software; you can redistribute it and/or modify it under
119             the same terms as the Perl 5 programming language system itself.
120              
121             =cut