File Coverage

blib/lib/Courriel/Types/Internal.pm
Criterion Covered Total %
statement 24 24 100.0
branch n/a
condition n/a
subroutine 8 8 100.0
pod n/a
total 32 32 100.0


line stmt bran cond sub pod time code
1             package Courriel::Types::Internal;
2              
3 10     10   5975639 use strict;
  10         27  
  10         524  
4 10     10   98 use warnings;
  10         19  
  10         409  
5 10     10   65 use namespace::autoclean;
  10         18  
  10         104  
6              
7             our $VERSION = '0.43';
8              
9 10     10   3677 use List::AllUtils qw( all );
  10         42410  
  10         1106  
10 10     10   77 use Scalar::Util qw( blessed );
  10         19  
  10         816  
11              
12 10         115 use MooseX::Types -declare => [
13             qw(
14             Body
15             EmailAddressStr
16             HeaderArray
17             Headers
18             Part
19             Printable
20             Streamable
21             StringRef
22             )
23 10     10   77 ];
  10         20  
24 10     10   83085 use MooseX::Types::Common::String qw( NonEmptyStr );
  10         25  
  10         110  
25             use MooseX::Types::Moose
26 10     10   32509 qw( ArrayRef CodeRef FileHandle HashRef Object ScalarRef Str );
  10         32  
  10         89  
27              
28             #<<<
29             subtype Body,
30             as role_type('Courriel::Role::Body');
31              
32             subtype Headers,
33             as class_type('Courriel::Headers');
34              
35             subtype EmailAddressStr,
36             as NonEmptyStr;
37              
38             coerce EmailAddressStr,
39             from class_type('Email::Address'),
40             via { $_->format };
41              
42             my $_check_header_array = sub {
43             return 0 unless @{$_} % 2 == 0;
44              
45             my ( @even, @odd );
46             for my $i ( 0 .. $#{$_} ) {
47             if ( $i % 2 ) {
48             push @odd, $i;
49             }
50             else {
51             push @even, $i;
52             }
53             }
54              
55             ## no critic (ControlStructures::ProhibitNegativeExpressionsInUnlessAndUntilConditions)
56             return 0 unless all { defined $_ && length $_ && !ref $_ } @{$_}[@even];
57             ## use critic;
58             return 0
59             unless all { blessed($_) && $_->isa('Courriel::Header') } @{$_}[@odd];
60              
61             return 1;
62             };
63              
64             subtype HeaderArray,
65             as ArrayRef,
66             # prototype wants an actual block, not a ref to a sub
67             &where($_check_header_array),
68             message { 'The array reference must contain an even number of elements' };
69              
70             subtype Part,
71             as role_type('Courriel::Role::Part');
72              
73             subtype Printable,
74             as Object,
75             where { $_->can('print') };
76              
77             subtype Streamable,
78             as CodeRef;
79              
80             coerce Streamable,
81             from FileHandle,
82             via sub {
83             my $fh = $_;
84             return sub { print {$fh} @_ or die $! };
85             };
86              
87             coerce Streamable,
88             from Printable,
89             via sub {
90             my $obj = $_;
91             return sub { $obj->print(@_) };
92             };
93              
94             subtype StringRef,
95             as ScalarRef[Str];
96              
97             coerce StringRef,
98             from Str,
99             via { my $str = $_; \$str };
100             #>>>
101             1;