File Coverage

blib/lib/Dancer2/Core/Types.pm
Criterion Covered Total %
statement 16 16 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 22 22 100.0


line stmt bran cond sub pod time code
1             package Dancer2::Core::Types;
2             # ABSTRACT: Type::Tiny types for Dancer2 core.
3             $Dancer2::Core::Types::VERSION = '2.1.0';
4 230     230   392426 use strict;
  230         671  
  230         9373  
5 230     230   2351 use warnings;
  230         504  
  230         18008  
6 230     230   123678 use Type::Library -base;
  230         10958254  
  230         3216  
7 230     230   218518 use Type::Utils -all;
  230         3555008  
  230         3496  
8 230     230   778064 use Sub::Quote 'quote_sub';
  230         702821  
  230         21717  
9              
10 230     230   1585 BEGIN { extends "Types::Standard" };
11              
12             our %supported_http_methods = map +( $_ => 1 ), qw<
13             GET HEAD POST PUT DELETE OPTIONS PATCH
14             >;
15              
16             my $single_part = qr/
17             [A-Za-z] # must start with letter
18             (?: [A-Za-z0-9_]+ )? # can continue with letters, numbers or underscore
19             /x;
20              
21             my $namespace = qr/
22             ^
23             $single_part # first part
24             (?: (?: \:\: $single_part )+ )? # optional part starting with double colon
25             $
26             /x;
27              
28             declare 'ReadableFilePath', constraint => quote_sub q{ -e $_ && -r $_ };
29              
30             declare 'WritableFilePath', constraint => quote_sub q{ -e $_ && -w $_ };
31              
32             declare 'Dancer2Prefix', as 'Str', where {
33             # a prefix must start with the char '/'
34             # index is much faster than =~ /^\//
35             index($_, '/') == 0
36             };
37              
38             declare 'Dancer2AppName', as 'Str', where {
39             # TODO need a real check of valid app names
40             $_ =~ $namespace;
41             }, message {
42             sprintf("%s is not a Dancer2AppName",
43             ($_ && length($_)) ? $_ : 'Empty string')
44             };
45              
46             declare 'Dancer2Method', as Enum [map +(lc), keys %supported_http_methods];
47              
48             declare 'Dancer2HTTPMethod', as Enum [keys %supported_http_methods];
49              
50             # generate abbreviated class types for core dancer objects
51             for my $type (
52             qw/
53             App
54             Context
55             Cookie
56             DSL
57             Dispatcher
58             Error
59             Hook
60             MIME
61             Request
62             Response
63             Role
64             Route
65             Runner
66             Server
67             Session
68             Types
69             /
70             )
71             {
72             declare $type,
73             as InstanceOf[ 'Dancer2::Core::' . $type ];
74             }
75              
76             # Export everything by default.
77             our @EXPORT = __PACKAGE__->type_names;
78              
79             1;
80              
81             __END__