File Coverage

lib/Sisimai/Mail.pm
Criterion Covered Total %
statement 37 40 92.5
branch 8 12 66.6
condition 2 3 66.6
subroutine 5 6 83.3
pod 2 2 100.0
total 54 63 85.7


line stmt bran cond sub pod time code
1             package Sisimai::Mail;
2 80     80   254531 use feature ':5.10';
  80         181  
  80         8658  
3 80     80   500 use strict;
  80         151  
  80         1486  
4 80     80   390 use warnings;
  80         142  
  80         3338  
5             use Class::Accessor::Lite (
6 80         589 '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 80     80   34659 );
  80         90140  
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 570     570 1 502377 my $class = shift;
22 570         1165 my $argv1 = shift;
23 570         988 my $klass = undef;
24 570         1050 my $loads = 'Sisimai/Mail/';
25 570         2191 my $param = { 'kind' => '', 'data' => undef, 'path' => $argv1 };
26              
27             # The argumenet is a mailbox or a Maildir/.
28 570 100       9677 if( -f $argv1 ) {
    100          
29             # The argument is a file, it is an mbox or email file in Maildir/
30 557         1763 $klass = __PACKAGE__.'::Mbox';
31 557         1182 $loads .= 'Mbox.pm';
32 557         1255 $param->{'kind'} = 'mailbox';
33 557         1171 $param->{'path'} = $argv1;
34              
35             } elsif( -d $argv1 ) {
36             # The agument is not a file, it is a Maildir/
37 9         31 $klass = __PACKAGE__.'::Maildir';
38 9         28 $loads .= 'Maildir.pm';
39 9         54 $param->{'kind'} = 'maildir';
40              
41             } else {
42             # The argumen1 neither a mailbox nor a Maildir/.
43 4 100 66     52 if( ref($argv1) eq 'GLOB' || $argv1 eq 'STDIN' ) {
    50          
44             # Read from STDIN
45 1         3 $klass = __PACKAGE__.'::STDIN';
46 1         2 $loads .= 'STDIN.pm';
47 1         3 $param->{'kind'} = 'stdin';
48              
49             } elsif( ref($argv1) eq 'SCALAR' ) {
50             # Read from a variable as a scalar reference
51 3         7 $klass = __PACKAGE__.'::Memory';
52 3         10 $loads .= 'Memory.pm';
53 3         7 $param->{'kind'} = 'memory';
54 3         6 $param->{'path'} = 'MEMORY';
55             }
56             }
57 570 50       1641 return undef unless $klass;
58              
59 570         36373 require $loads;
60 570         3308 $param->{'data'} = $klass->new($argv1);
61              
62 570         2583 return bless($param, __PACKAGE__);
63             }
64              
65             sub read {
66             # Alias method of Sisimai::Mail::*->read()
67             # @return [String] Contents of mbox/Maildir
68 0     0 1   my $self = shift;
69 0 0         return undef unless ref $self->{'data'};
70 0           return $self->{'data'}->read;
71             }
72              
73             1;
74             __END__