MtPolicyd/AddressList.pm

filter: Statementstmt 100% 41 / 41 Branchbran 100% 2 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 100% 5 / 5 totaltotal 100% 58 / 58 CC 2 SCAR 1.7
CC
2
Cov
100%
match
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.2
« MtPolicyd/Connection/Sql.pm ↑ Summary MtPolicyd/Profiler.pm »
1 package Mail::MtPolicyd::AddressList;
2  
3 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 our $VERSION = '2.01'; # VERSION
7 # ABSTRACT: a class for IP address lists
8  
9 3 ▶use NetAddr::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 has '_localhost_addr' => ( is => 'ro', isa => 'ArrayRef[NetAddr::IP]',
12 lazy => 1,
13 default => sub {
14 2 ▶ return [ map { NetAddr::IP->new( $_ ) }
Subroutine __ANON__: called CC=1 SCAR=0.0
15 ( '127.0.0.0/8', '::ffff:127.0.0.0/104', '::1' ) ];
16 },
17 3 );
18  
19  
20 has 'list' => (
21 is => 'ro', isa => 'ArrayRef[NetAddr::IP]', lazy => 1,
22 2 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
23 3 traits => [ 'Array' ],
24 handles => {
25 'add' => 'push',
26 'is_empty' => 'is_empty',
27 'count' => 'count',
28 },
29 );
30  
31  
32 sub add_localhost {
33 2 ▶ my $self = shift;
Subroutine add_localhost: called CC=1 SCAR=0.0
Pod documented
34 2 $self->add( @{$self->_localhost_addr} );
35 2 return;
36 }
37  
38  
39 sub add_string {
40 3 ▶ my ( $self, @strings ) = @_;
Subroutine add_string: called CC=1 SCAR=0.0
Pod documented
41  
42 my @addr_strings = map {
43 3 split( /\s*[, ]\s*/, $_ )
44 } @strings;
45
46 my @addr = map {
47 3 NetAddr::IP->new( $_ );
48 } @addr_strings;
49  
50 3 $self->add( @addr );
51  
52 3 return;
53 }
54  
55  
56 sub match {
57 8 ▶ my ( $self, $addr ) = @_;
Subroutine match: called CC=2 SCAR=6.9
Pod documented
58 8 ▶ if( grep { $_->contains( $addr ) } @{$self->list} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_->contains($addr);} @{$self->list;}) 4 4 8
59 4 return 1;
60 }
61 4 return 0;
62 }
63  
64  
65 sub match_string {
66 8 ▶ my ( $self, $string ) = @_;
Subroutine match_string: called CC=1 SCAR=0.0
Pod documented
67 8 my $addr = NetAddr::IP->new( $string );
68 8 return( $self->match( $addr ) );
69 }
70  
71  
72 sub as_string {
73 1 ▶ my $self = shift;
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
74 1 return join(',', map { $_->cidr } @{$self->list});
75 }
76  
77 3 __PACKAGE__->meta->make_immutable;
78  
79 3 1;
80  
81 __END__
82  
83 =pod
84  
85 =encoding UTF-8
86  
87 =head1 NAME
88  
89 Mail::MtPolicyd::AddressList - a class for IP address lists
90  
91 =head1 VERSION
92  
93 version 2.01
94  
95 =head1 Attributes
96  
97 =head2 list
98  
99 Contains an ArrayRef of NetAddr::IP which holds the all entries of this object.
100  
101 =head1 Methods
102  
103 =head2 add
104  
105 Add a list of NetAddr::IP objects to the list.
106  
107 =head2 is_empty
108  
109 Returns a true value when empty.
110  
111 =head2 count
112  
113 Returns the number of entries.
114  
115 =head2 add_localhost
116  
117 Add localhost addresses to list.
118  
119 =head2 add_string
120  
121 Takes a list of IP address strings.
122  
123 The strings itself can contain a list of comma/space separated addresses.
124  
125 Then a list of NetAddr::IP objects is created and pushed to the list.
126  
127 =head2 match
128  
129 Returns true if the give NetAddr::IP object matches an entry of the list.
130  
131 =head2 match_string
132  
133 Same as match(), but takes an string instead of NetAddr::IP object.
134  
135 =head2 as_string
136  
137 Returns a comma separated string with all addresses.
138  
139 =head1 AUTHOR
140  
141 Markus Benning <ich@markusbenning.de>
142  
143 =head1 COPYRIGHT AND LICENSE
144  
145 This software is Copyright (c) 2014 by Markus Benning <ich@markusbenning.de>.
146  
147 This is free software, licensed under:
148  
149 The GNU General Public License, Version 2, June 1991
150  
151 =cut
« MtPolicyd/Connection/Sql.pm ↑ Summary MtPolicyd/Profiler.pm »