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 88     88   24492248 use v5.26;
  88         308  
3 88     88   340 use strict;
  88         101  
  88         1687  
4 88     88   381 use warnings;
  88         202  
  88         4802  
5             use Class::Accessor::Lite (
6 88         697 '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 88     88   31300 );
  88         90760  
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 751     751 1 908633 my $class = shift;
22 751         1368 my $argv1 = shift;
23 751         1487 my $klass = undef;
24 751         1577 my $loads = 'Sisimai/Mail/';
25 751         4070 my $param = {'kind' => '', 'data' => undef, 'path' => $argv1};
26              
27             # The argumenet is a mailbox or a Maildir/.
28 751 100       18324 if( -f $argv1 ) {
    100          
29             # The argument is a file, it is an mbox or email file in Maildir/
30 740         1717 $klass = __PACKAGE__.'::Mbox';
31 740         1502 $loads .= 'Mbox.pm';
32 740         1867 $param->{'kind'} = 'mailbox';
33 740         1595 $param->{'path'} = $argv1;
34              
35             } elsif( -d $argv1 ) {
36             # The agument is not a file, it is a Maildir/
37 7         21 $klass = __PACKAGE__.'::Maildir';
38 7         19 $loads .= 'Maildir.pm';
39 7         26 $param->{'kind'} = 'maildir';
40              
41             } else {
42             # The argumen1 neither a mailbox nor a Maildir/.
43 4 100 66     83 if( ref($argv1) eq 'GLOB' || $argv1 eq 'STDIN' ) {
    50          
44             # Read from STDIN
45 1         4 $klass = __PACKAGE__.'::STDIN';
46 1         3 $loads .= 'STDIN.pm';
47 1         4 $param->{'kind'} = 'stdin';
48              
49             } elsif( ref($argv1) eq 'SCALAR' ) {
50             # Read from a variable as a scalar reference
51 3         5 $klass = __PACKAGE__.'::Memory';
52 3         8 $loads .= 'Memory.pm';
53 3         9 $param->{'kind'} = 'memory';
54 3         9 $param->{'path'} = 'MEMORY';
55             }
56             }
57 751 50       2330 return undef unless $klass;
58              
59 751         42133 require $loads;
60 751         4807 $param->{'data'} = $klass->new($argv1);
61              
62 751         3337 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       6 return "" unless ref $self->{'data'};
70 1         2 return $self->{'data'}->read;
71             }
72              
73             1;
74             __END__