| 1 | package Message::Stack; | ||||||||||||||
| 2 | 4 | ▶ | BEGIN { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 4 | $Message::Stack::VERSION = '0.22'; | |||||||||||||
| 4 | } | ||||||||||||||
| 5 | 4 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | # ABSTRACT: Deal with a "stack" of messages | ||||||||||||||
| 8 | |||||||||||||||
| 9 | 4 | ▶ | use Carp qw(croak); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 4 | ▶ | use MooseX::Storage; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 4 | ▶ | use MooseX::Types::Moose qw(HashRef); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 4 | ▶ | use Message::Stack::Message; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | 4 | ▶ | use Message::Stack::Types qw(MessageStackMessage); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | |||||||||||||||
| 15 | 4 | with 'MooseX::Storage::Deferred'; | |||||||||||||
| 16 | |||||||||||||||
| 17 | |||||||||||||||
| 18 | has messages => ( | ||||||||||||||
| 19 | traits => [ 'Array' ], | ||||||||||||||
| 20 | is => 'rw', | ||||||||||||||
| 21 | isa => 'ArrayRef[Message::Stack::Message]', | ||||||||||||||
| 22 | 6 | ▶ | default => sub { [] }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 23 | 4 | handles => { | |||||||||||||
| 24 | reset => 'clear', | ||||||||||||||
| 25 | count => 'count', | ||||||||||||||
| 26 | has_messages => 'count', | ||||||||||||||
| 27 | first => [ get => 0 ], | ||||||||||||||
| 28 | first_message => [ get => 0 ], | ||||||||||||||
| 29 | _grep_messages => 'grep', | ||||||||||||||
| 30 | get_message => 'get', | ||||||||||||||
| 31 | last => [ get => -1 ], | ||||||||||||||
| 32 | last_message => [ get => -1 ], | ||||||||||||||
| 33 | } | ||||||||||||||
| 34 | ); | ||||||||||||||
| 35 | |||||||||||||||
| 36 | |||||||||||||||
| 37 | 12 | ▶ | sub add { | ||||||||||||
|
Subroutine
add: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||
| 38 | 12 | my ($self, $message) = @_; | |||||||||||||
| 39 | |||||||||||||||
| 40 | 12 | ▶ | return unless defined($message); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 41 | |||||||||||||||
| 42 | 12 | ▶ | if(is_MessageStackMessage($message)) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 43 | 9 | push(@{ $self->messages }, $message); | |||||||||||||
| 44 | } elsif(is_HashRef($message)) { | ||||||||||||||
| 45 | 3 | my $mess = Message::Stack::Message->new($message); | |||||||||||||
| 46 | 3 | push(@{ $self->messages }, $mess); | |||||||||||||
| 47 | } else { | ||||||||||||||
| 48 | 0 | croak('Message must be either a Message::Stack::Message or hashref'); | |||||||||||||
| 49 | } | ||||||||||||||
| 50 | } | ||||||||||||||
| 51 | |||||||||||||||
| 52 | 1 | ▶ | sub for_id { | ||||||||||||
|
Subroutine
for_id: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 53 | 1 | my $self = shift; | |||||||||||||
| 54 | 1 | $self->for_msgid(@_); | |||||||||||||
| 55 | } | ||||||||||||||
| 56 | |||||||||||||||
| 57 | |||||||||||||||
| 58 | 6 | ▶ | sub for_msgid { | ||||||||||||
|
Subroutine
for_msgid: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 59 | 6 | my ($self, $msgid) = @_; | |||||||||||||
| 60 | |||||||||||||||
| 61 | 6 | ▶ | return $self->search(sub { $_->msgid eq $msgid if $_->has_msgid }); | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||
| 62 | } | ||||||||||||||
| 63 | |||||||||||||||
| 64 | |||||||||||||||
| 65 | 3 | ▶ | sub for_level { | ||||||||||||
|
Subroutine
for_level: called CC=1 SCAR=0.1
Pod
documented
| |||||||||||||||
| 66 | 3 | my ($self, $level) = @_; | |||||||||||||
| 67 | |||||||||||||||
| 68 | 3 | ▶ | return $self->search(sub { $_->level eq $level if $_->has_level }); | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||||||
| 69 | } | ||||||||||||||
| 70 | |||||||||||||||
| 71 | |||||||||||||||
| 72 | 3 | ▶ | sub for_scope { | ||||||||||||
|
Subroutine
for_scope: called CC=1 SCAR=0.1
Pod
documented
| |||||||||||||||
| 73 | 3 | my ($self, $scope) = @_; | |||||||||||||
| 74 | |||||||||||||||
| 75 | 3 | ▶ | return $self->search(sub { $_->scope eq $scope if $_->has_scope }); | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||||||
| 76 | } | ||||||||||||||
| 77 | |||||||||||||||
| 78 | |||||||||||||||
| 79 | 3 | ▶ | sub for_subject { | ||||||||||||
|
Subroutine
for_subject: called CC=1 SCAR=0.1
Pod
documented
| |||||||||||||||
| 80 | 3 | my ($self, $subject) = @_; | |||||||||||||
| 81 | |||||||||||||||
| 82 | 3 | ▶ | return $self->search(sub { $_->subject eq $subject if $_->has_subject }); | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||||||
| 83 | } | ||||||||||||||
| 84 | |||||||||||||||
| 85 | 2 | ▶ | sub has_id { | ||||||||||||
|
Subroutine
has_id: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 86 | 2 | my $self = shift; | |||||||||||||
| 87 | 2 | $self->has_msgid(@_); | |||||||||||||
| 88 | } | ||||||||||||||
| 89 | |||||||||||||||
| 90 | |||||||||||||||
| 91 | 4 | ▶ | sub has_msgid { | ||||||||||||
|
Subroutine
has_msgid: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||
| 92 | 4 | my ($self, $msgid) = @_; | |||||||||||||
| 93 | |||||||||||||||
| 94 | 4 | ▶ | return 0 unless $self->has_messages; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 95 | |||||||||||||||
| 96 | 4 | ▶ | return $self->for_msgid($msgid)->count ? 1 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 97 | } | ||||||||||||||
| 98 | |||||||||||||||
| 99 | |||||||||||||||
| 100 | 2 | ▶ | sub has_level { | ||||||||||||
|
Subroutine
has_level: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||
| 101 | 2 | my ($self, $level) = @_; | |||||||||||||
| 102 | |||||||||||||||
| 103 | 2 | ▶ | return 0 unless $self->has_messages; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 104 | |||||||||||||||
| 105 | 2 | ▶ | return $self->for_level($level)->count ? 1 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 106 | } | ||||||||||||||
| 107 | |||||||||||||||
| 108 | |||||||||||||||
| 109 | 2 | ▶ | sub has_scope { | ||||||||||||
|
Subroutine
has_scope: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||
| 110 | 2 | my ($self, $scope) = @_; | |||||||||||||
| 111 | |||||||||||||||
| 112 | 2 | ▶ | return 0 unless $self->has_messages; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 113 | |||||||||||||||
| 114 | 2 | ▶ | return $self->for_scope($scope)->count ? 1 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 115 | } | ||||||||||||||
| 116 | |||||||||||||||
| 117 | |||||||||||||||
| 118 | 2 | ▶ | sub has_subject { | ||||||||||||
|
Subroutine
has_subject: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||
| 119 | 2 | my ($self, $subject) = @_; | |||||||||||||
| 120 | |||||||||||||||
| 121 | 2 | ▶ | return 0 unless $self->has_messages; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 122 | |||||||||||||||
| 123 | 2 | ▶ | return $self->for_subject($subject)->count ? 1 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 124 | } | ||||||||||||||
| 125 | |||||||||||||||
| 126 | |||||||||||||||
| 127 | 16 | ▶ | sub search { | ||||||||||||
|
Subroutine
search: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 128 | 16 | my ($self, $coderef) = @_; | |||||||||||||
| 129 | |||||||||||||||
| 130 | 16 | my @messages = $self->_grep_messages($coderef); | |||||||||||||
| 131 | 16 | return Message::Stack->new(messages => \@messages); | |||||||||||||
| 132 | } | ||||||||||||||
| 133 | |||||||||||||||
| 134 | |||||||||||||||
| 135 | 1 | ▶ | sub reset_scope { | ||||||||||||
|
Subroutine
reset_scope: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||
| 136 | 1 | my ($self, $scope) = @_; | |||||||||||||
| 137 | |||||||||||||||
| 138 | 1 | ▶ | return 0 unless $self->has_messages; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 139 | |||||||||||||||
| 140 | 1 | my $filtered = []; | |||||||||||||
| 141 | 1 | foreach my $message (@{$self->messages}) { | |||||||||||||
| 142 | 2 | ▶ | next if($message->scope eq $scope); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 143 | 1 | push @{$filtered}, $message; | |||||||||||||
| 144 | } | ||||||||||||||
| 145 | |||||||||||||||
| 146 | 1 | $self->messages($filtered); | |||||||||||||
| 147 | } | ||||||||||||||
| 148 | |||||||||||||||
| 149 | |||||||||||||||
| 150 | 1 | ▶ | sub reset_level { | ||||||||||||
|
Subroutine
reset_level: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||
| 151 | 1 | my ($self, $level) = @_; | |||||||||||||
| 152 | |||||||||||||||
| 153 | 1 | ▶ | return 0 unless $self->has_messages; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 154 | |||||||||||||||
| 155 | 1 | my $filtered = []; | |||||||||||||
| 156 | 1 | foreach my $message (@{$self->messages}) { | |||||||||||||
| 157 | 2 | ▶ | next if($message->level eq $level); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 158 | 1 | push @{$filtered}, $message; | |||||||||||||
| 159 | } | ||||||||||||||
| 160 | |||||||||||||||
| 161 | 1 | $self->messages($filtered); | |||||||||||||
| 162 | } | ||||||||||||||
| 163 | |||||||||||||||
| 164 | |||||||||||||||
| 165 | 2 | ▶ | sub reset_msgid { | ||||||||||||
|
Subroutine
reset_msgid: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||
| 166 | 2 | my ($self, $msgid) = @_; | |||||||||||||
| 167 | |||||||||||||||
| 168 | 2 | ▶ | return 0 unless $self->has_messages; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 169 | |||||||||||||||
| 170 | 2 | my $filtered = []; | |||||||||||||
| 171 | 2 | foreach my $message (@{$self->messages}) { | |||||||||||||
| 172 | 3 | ▶ | next if($message->msgid eq $msgid); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 173 | 1 | push @{$filtered}, $message; | |||||||||||||
| 174 | } | ||||||||||||||
| 175 | |||||||||||||||
| 176 | 2 | $self->messages($filtered); | |||||||||||||
| 177 | } | ||||||||||||||
| 178 | |||||||||||||||
| 179 | |||||||||||||||
| 180 | 1 | ▶ | sub reset_subject { | ||||||||||||
|
Subroutine
reset_subject: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||
| 181 | 1 | my ($self, $subject) = @_; | |||||||||||||
| 182 | |||||||||||||||
| 183 | 1 | ▶ | return 0 unless $self->has_messages; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 184 | |||||||||||||||
| 185 | 1 | my $filtered = []; | |||||||||||||
| 186 | 1 | foreach my $message (@{$self->messages}) { | |||||||||||||
| 187 | 2 | ▶ | next if($message->subject eq $subject); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 188 | 1 | push @{$filtered}, $message; | |||||||||||||
| 189 | } | ||||||||||||||
| 190 | |||||||||||||||
| 191 | 1 | $self->messages($filtered); | |||||||||||||
| 192 | } | ||||||||||||||
| 193 | |||||||||||||||
| 194 | |||||||||||||||
| 195 | 4 | __PACKAGE__->meta->make_immutable; | |||||||||||||
| 196 | 4 | ▶ | no Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 197 | 4 | 1; | |||||||||||||
| 198 | |||||||||||||||
| 199 | __END__ | ||||||||||||||