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 = '1.0.0';
4 166     166   69348 use strict;
  166         379  
  166         4789  
5 166     166   907 use warnings;
  166         339  
  166         5162  
6 166     166   81268 use Type::Library -base;
  166         6292661  
  166         1843  
7 166     166   133412 use Type::Utils -all;
  166         2018130  
  166         1821  
8 166     166   475793 use Sub::Quote 'quote_sub';
  166         223689  
  166         10897  
9              
10 166     166   1176 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__
82              
83             =pod
84              
85             =encoding UTF-8
86              
87             =head1 NAME
88              
89             Dancer2::Core::Types - Type::Tiny types for Dancer2 core.
90              
91             =head1 VERSION
92              
93             version 1.0.0
94              
95             =head1 DESCRIPTION
96              
97             L<Type::Tiny> definitions for Moo attributes. These are defined as subroutines.
98              
99             =head1 MOO TYPES
100              
101             =head2 ReadableFilePath($value)
102              
103             A readable file path.
104              
105             =head2 WritableFilePath($value)
106              
107             A writable file path.
108              
109             =head2 Dancer2Prefix($value)
110              
111             A proper Dancer2 prefix, which is basically a prefix that starts with a I</>
112             character.
113              
114             =head2 Dancer2AppName($value)
115              
116             A proper Dancer2 application name.
117              
118             Currently this only checks for I<\w+>.
119              
120             =head2 Dancer2Method($value)
121              
122             An acceptable method supported by Dancer2.
123              
124             Currently this includes: I<get>, I<head>, I<post>, I<put>, I<delete> and
125             I<options>.
126              
127             =head2 Dancer2HTTPMethod($value)
128              
129             An acceptable HTTP method supported by Dancer2.
130              
131             Current this includes: I<GET>, I<HEAD>, I<POST>, I<PUT>, I<DELETE>
132             and I<OPTIONS>.
133              
134             =head1 SEE ALSO
135              
136             L<Types::Standard> for more available types
137              
138             =head1 AUTHOR
139              
140             Dancer Core Developers
141              
142             =head1 COPYRIGHT AND LICENSE
143              
144             This software is copyright (c) 2023 by Alexis Sukrieh.
145              
146             This is free software; you can redistribute it and/or modify it under
147             the same terms as the Perl 5 programming language system itself.
148              
149             =cut