Tools/Summary.pm

filter: Statementstmt 86.2% 50 / 58 Branchbran 43.7% 7 / 16 Conditioncond 57.1% 4 / 7 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 5 totaltotal 72.5% 74 / 102 CC 14 SCAR 14.2
CC
14
Cov
75%
save
9.5
load
5.0
to_hash
3.2
CRAP 4.1
« Tools/CLI/Command.pm ↑ Summary Tools/CLI/Config.pm »
1 #!/usr/bin/perl
2  
3 package Mail::Summary::Tools::Summary;
4 5 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use Mail::Summary::Tools::Summary::List;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 ▶use YAML::Syck ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 has title => (
12 isa => "Str",
13 is => "rw",
14 );
15  
16 has lists => (
17 isa => "ArrayRef",
18 is => "rw",
19 auto_deref => 1,
20 1 ▶ default => sub { [ ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
21 5 );
22  
23 5 has extra => (
24 isa => "HashRef",
25 is => "rw",
26 required => 0,
27 );
28  
29 has _message_id_index => (
30 isa => "HashRef",
31 is => "rw",
32 5 ▶ default => sub { return {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
33 5 );
34  
35 1 ▶sub add_lists {
Subroutine add_lists: called CC=1 SCAR=0.0
Pod undocumented
36 1 my ( $self, @lists ) = @_;
37 1 push @{ $self->lists }, @lists;
38 }
39  
40 3 ▶sub get_thread_by_id {
Subroutine get_thread_by_id: called CC=2 SCAR=8.1
Pod undocumented
41 3 my ( $self, $message_id ) = @_;
42  
43 3 ▶ $self->_message_id_index->{$message_id} ||= $self->_get_thread_by_id($message_id);
Condition33%1/3
$self->_message_id_index->{$message_id} ||= $self->_get_thread_by_id($message_id)
l!l&&r!l&&!r
030
MC/DC0%0/2
$self->_message_id_index->{$message_id} ||= $self->_get_thread_by_id($message_id)
$self->_message_id_index->{$message_id} $self->_get_thread_by_id($message_id)
Truth table33%1/3
A ||= B
A= $self->_message_id_index->{$message_id}B= $self->_get_thread_by_id($message_id)
ABresult
1X1
011
000
44 }
45  
46 3 ▶sub _get_thread_by_id {
Subroutine _get_thread_by_id: called CC=3 SCAR=11.1
47 3 my ( $self, $message_id ) = @_;
48  
49 3 foreach my $list ( $self->lists ) {
50 4 ▶ if ( my $thread = $list->get_thread_by_id($message_id) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $thread = $list->get_thread_by_id($message_id)) 3 1 4
51 3 return $thread;
52 }
53 }
54  
55 0 return;
56 }
57  
58 1 ▶sub load {
Subroutine load: called CC=5 SCAR=16.2
Pod undocumented
59 1 my ( $class, $thing, %options ) = @_;
60  
61 1 ▶ $options{$_} ||= {} for qw/summary list thread/;
Condition100%2/2
$options{$_} ||= {}
l!l
12
MC/DC100%1/1
$options{$_} ||= {}
$options{$_}
Truth table100%2/2
A
A= $options{$_}
Aresult
11
00
62  
63 1 local $YAML::Syck::ImplicitUnicode = 1;
64 1 ▶ my $hash = ref($thing) ? $thing : do {
Branch: 1 branch
BranchTrueFalseTotal
ref $thing ? : 0 1 1
65 1 ▶ die "Can't load YAML summary '$thing': no such file or directory\n" unless -e $thing;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $thing 0 1 1
66 1 YAML::Syck::LoadFile($thing);
67 };
68  
69 1 $hash->{lists} = [ map { Mail::Summary::Tools::Summary::List->load( $_, %options ) } @{ $hash->{lists} } ];
70  
71 1 $class->new( %{ $options{summary} }, %$hash );
72 }
73  
74 1 ▶sub save {
Subroutine save: called CC=5 SCAR=22.5
Pod undocumented
75 1 my ( $self, @args ) = @_;
76  
77 1 local $YAML::Syck::ImplicitUnicode = 1;
78  
79 # YAML.pm's output is prettier
80 1 ▶ my $dump = eval { require YAML; \&YAML::Dump } || \&YAML::Syck::Dump;
Condition50%1/2
eval { do { require YAML; \&YAML::Dump } } || \&YAML::Syck::Dump
l!l
10
MC/DC0%0/1
eval { do { require YAML; \&YAML::Dump } } || \&YAML::Syck::Dump
eval { do { require YAML; \&YAML::Dump } }
Truth table50%1/2
A
A= eval { do { require YAML; \&YAML::Dump } }
Aresult
11
00
81 1 my $yaml = $dump->( $self->to_hash );
82  
83 1 ▶ if ( @args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) { } 0 1 1
84 0 my $file = shift @args;
85  
86 # keep a backup
87 0 unlink "$file~";
88 0 rename $file, "$file~";
89  
90 # YAML doesn't set the handle to :utf8, so we're doing the heavy lifting on our own
91 0 ▶ open my $fh, ">:utf8", $file or die "open('$file'): $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:utf8", $file or die "open('${file}'): $!" 0 0 0
92 0 print $fh $yaml;
93 0 ▶ close $fh or die "close('$file'): $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "close('${file}'): $!" 0 0 0
94 0 return 1;
95 } else {
96 1 return $yaml;
97 }
98 }
99  
100 1 ▶sub to_hash {
Subroutine to_hash: called CC=3 SCAR=11.7
Pod undocumented
101 1 my $self = shift;
102  
103 return {
104 ( $self->title ? (title => $self->title) : () ),
105 ( $self->extra ? (extra => $self->extra) : () ),
106 1 ▶ lists => [ map { $_->to_hash } $self->lists ],
Branch: 2 branches
BranchTrueFalseTotal
$self->title ? : 0 1 1
$self->extra ? : 0 1 1
107 };
108 }
109  
110  
111  
112  
113 5 __PACKAGE__;
114  
115 __END__
« Tools/CLI/Command.pm ↑ Summary Tools/CLI/Config.pm »