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             # ABSTRACT: Type::Tiny types for Dancer2 core.
2             $Dancer2::Core::Types::VERSION = '0.400000';
3             use strict;
4 163     163   55403 use warnings;
  163         293  
  163         4381  
5 163     163   758 use Type::Library -base;
  163         288  
  163         4405  
6 163     163   65858 use Type::Utils -all;
  163         4676950  
  163         1845  
7 163     163   130138 use Sub::Quote 'quote_sub';
  163         1142897  
  163         1754  
8 163     163   342002  
  163         185941  
  163         10800  
9             BEGIN { extends "Types::Standard" };
10 163     163   885  
11             our %supported_http_methods = map +( $_ => 1 ), qw<
12             GET HEAD POST PUT DELETE OPTIONS PATCH
13             >;
14              
15             my $single_part = qr/
16             [A-Za-z] # must start with letter
17             (?: [A-Za-z0-9_]+ )? # can continue with letters, numbers or underscore
18             /x;
19              
20             my $namespace = qr/
21             ^
22             $single_part # first part
23             (?: (?: \:\: $single_part )+ )? # optional part starting with double colon
24             $
25             /x;
26              
27             declare 'ReadableFilePath', constraint => quote_sub q{ -e $_ && -r $_ };
28              
29             declare 'WritableFilePath', constraint => quote_sub q{ -e $_ && -w $_ };
30              
31             declare 'Dancer2Prefix', as 'Str', where {
32             # a prefix must start with the char '/'
33             # index is much faster than =~ /^\//
34             index($_, '/') == 0
35             };
36              
37             declare 'Dancer2AppName', as 'Str', where {
38             # TODO need a real check of valid app names
39             $_ =~ $namespace;
40             }, message {
41             sprintf("%s is not a Dancer2AppName",
42             ($_ && length($_)) ? $_ : 'Empty string')
43             };
44              
45             declare 'Dancer2Method', as Enum [map +(lc), keys %supported_http_methods];
46              
47             declare 'Dancer2HTTPMethod', as Enum [keys %supported_http_methods];
48              
49             # generate abbreviated class types for core dancer objects
50             for my $type (
51             qw/
52             App
53             Context
54             Cookie
55             DSL
56             Dispatcher
57             Error
58             Hook
59             MIME
60             Request
61             Response
62             Role
63             Route
64             Runner
65             Server
66             Session
67             Types
68             /
69             )
70             {
71             declare $type,
72             as InstanceOf[ 'Dancer2::Core::' . $type ];
73             }
74              
75             # Export everything by default.
76             our @EXPORT = __PACKAGE__->type_names;
77              
78             1;
79              
80              
81             =pod
82              
83             =encoding UTF-8
84              
85             =head1 NAME
86              
87             Dancer2::Core::Types - Type::Tiny types for Dancer2 core.
88              
89             =head1 VERSION
90              
91             version 0.400000
92              
93             =head1 DESCRIPTION
94              
95             L<Type::Tiny> definitions for Moo attributes. These are defined as subroutines.
96              
97             =head1 MOO TYPES
98              
99             =head2 ReadableFilePath($value)
100              
101             A readable file path.
102              
103             =head2 WritableFilePath($value)
104              
105             A writable file path.
106              
107             =head2 Dancer2Prefix($value)
108              
109             A proper Dancer2 prefix, which is basically a prefix that starts with a I</>
110             character.
111              
112             =head2 Dancer2AppName($value)
113              
114             A proper Dancer2 application name.
115              
116             Currently this only checks for I<\w+>.
117              
118             =head2 Dancer2Method($value)
119              
120             An acceptable method supported by Dancer2.
121              
122             Currently this includes: I<get>, I<head>, I<post>, I<put>, I<delete> and
123             I<options>.
124              
125             =head2 Dancer2HTTPMethod($value)
126              
127             An acceptable HTTP method supported by Dancer2.
128              
129             Current this includes: I<GET>, I<HEAD>, I<POST>, I<PUT>, I<DELETE>
130             and I<OPTIONS>.
131              
132             =head1 SEE ALSO
133              
134             L<Types::Standard> for more available types
135              
136             =head1 AUTHOR
137              
138             Dancer Core Developers
139              
140             =head1 COPYRIGHT AND LICENSE
141              
142             This software is copyright (c) 2022 by Alexis Sukrieh.
143              
144             This is free software; you can redistribute it and/or modify it under
145             the same terms as the Perl 5 programming language system itself.
146              
147             =cut