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.0.1';
4 224     224   451141 use strict;
  224         585  
  224         8768  
5 224     224   2013 use warnings;
  224         475  
  224         18924  
6 224     224   118676 use Type::Library -base;
  224         10457213  
  224         3059  
7 224     224   208014 use Type::Utils -all;
  224         3535176  
  224         3387  
8 224     224   770792 use Sub::Quote 'quote_sub';
  224         683123  
  224         22297  
9              
10 224     224   1572 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__