File Coverage

lib/Sisimai/Mail.pm
Criterion Covered Total %
statement 39 39 100.0
branch 9 12 75.0
condition 2 3 66.6
subroutine 6 6 100.0
pod 2 2 100.0
total 58 62 93.5


line stmt bran cond sub pod time code
1             package Sisimai::Mail;
2 86     86   35063236 use v5.26;
  86         334  
3 86     86   448 use strict;
  86         225  
  86         2517  
4 86     86   518 use warnings;
  86         202  
  86         6317  
5             use Class::Accessor::Lite (
6 86         738 'new' => 0,
7             'ro' => [
8             'path', # [String] path to mbox or Maildir/
9             'kind', # [String] Data type: mailbox, maildir, stdin, or memory
10             ],
11             'rw' => [
12             'data', # [Sisimai::Mail::[Mbox,Maildir,Memory,STDIO] Object
13             ]
14 86     86   44216 );
  86         114082  
15              
16             sub new {
17             # Constructor of Sisimai::Mail
18             # @param [String] argv1 Path to mbox or Maildir/
19             # @return [Sisimai::Mail] Object
20             # [Undef] The argument is wrong
21 747     747 1 910048 my $class = shift;
22 747         1678 my $argv1 = shift;
23 747         1696 my $klass = undef;
24 747         1845 my $loads = 'Sisimai/Mail/';
25 747         4650 my $param = {'kind' => '', 'data' => undef, 'path' => $argv1};
26              
27             # The argumenet is a mailbox or a Maildir/.
28 747 100       18332 if( -f $argv1 ) {
    100          
29             # The argument is a file, it is an mbox or email file in Maildir/
30 736         2024 $klass = __PACKAGE__.'::Mbox';
31 736         1826 $loads .= 'Mbox.pm';
32 736         2070 $param->{'kind'} = 'mailbox';
33 736         2122 $param->{'path'} = $argv1;
34              
35             } elsif( -d $argv1 ) {
36             # The agument is not a file, it is a Maildir/
37 7         30 $klass = __PACKAGE__.'::Maildir';
38 7         20 $loads .= 'Maildir.pm';
39 7         22 $param->{'kind'} = 'maildir';
40              
41             } else {
42             # The argumen1 neither a mailbox nor a Maildir/.
43 4 100 66     51 if( ref($argv1) eq 'GLOB' || $argv1 eq 'STDIN' ) {
    50          
44             # Read from STDIN
45 1         77 $klass = __PACKAGE__.'::STDIN';
46 1         3 $loads .= 'STDIN.pm';
47 1         22 $param->{'kind'} = 'stdin';
48              
49             } elsif( ref($argv1) eq 'SCALAR' ) {
50             # Read from a variable as a scalar reference
51 3         6 $klass = __PACKAGE__.'::Memory';
52 3         8 $loads .= 'Memory.pm';
53 3         10 $param->{'kind'} = 'memory';
54 3         8 $param->{'path'} = 'MEMORY';
55             }
56             }
57 747 50       2701 return undef unless $klass;
58              
59 747         66923 require $loads;
60 747         5949 $param->{'data'} = $klass->new($argv1);
61              
62 747         4388 return bless($param, __PACKAGE__);
63             }
64              
65             sub read {
66             # Alias method of Sisimai::Mail::*->read()
67             # @return [String] Contents of mbox/Maildir
68 1     1 1 4 my $self = shift;
69 1 50       5 return "" unless ref $self->{'data'};
70 1         3 return $self->{'data'}->read;
71             }
72              
73             1;
74             __END__