Zonemaster/Engine/NSArray.pm untested

filter: Statementstmt 0.0% 0 / 81 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 126 CC 5 SCAR 13.3
CC
5
Cov
0%
FETCH
12.0
EXISTS
6.0
_load_name
6.0
CRAP 3.8
1 package Zonemaster::Engine::NSArray;
2  
3 use v5.16.0;
4 use warnings;
5  
6 use version; our $VERSION = version->declare("v1.0.3");
7  
8 use Carp qw( confess croak );
9 use Zonemaster::Engine::Recursor;
10 use Zonemaster::Engine::Nameserver;
11  
12 use Class::Accessor 'antlers';
13  
14 has 'names' => ( is => 'ro', isa => 'ArrayRef', required => 1 );
15 has 'ary' => ( is => 'ro', isa => 'ArrayRef', default => sub { [] } );
16  
17 sub TIEARRAY {
18 my ( $class, @names ) = @_;
19  
20 return $class->new(
21 {
22 ary => [],
23 names => [ sort { $a cmp $b } @names ]
24 }
25 );
26 }
27  
28 sub STORE {
29 my ( $self, $index, $value ) = @_;
30  
31 croak "STORE forbidden for this type of array.";
32 }
33  
34 sub STORESIZE {
35 my ( $self, $index, $value ) = @_;
36  
37 croak "STORESIZE forbidden for this type of array.";
38 }
39  
40 sub FETCH {
41 my ( $self, $index ) = @_;
42  
43 if ( exists $self->ary->[$index] ) {
44 return $self->ary->[$index];
45 }
46 elsif ( scalar( @{ $self->names } ) == 0 ) {
47 return;
48 }
49 else {
50 $self->_load_name( shift @{ $self->names } );
51 return $self->FETCH( $index );
52 }
53 }
54  
55 sub FETCHSIZE {
56 my ( $self ) = @_;
57  
58 while ( my $name = shift @{ $self->names } ) {
59 $self->_load_name( $name );
60 }
61  
62 return scalar( @{ $self->ary } );
63 }
64  
65 sub EXISTS {
66 my ( $self, $index ) = @_;
67  
68 if ( $self->FETCH( $index ) ) {
69 return 1;
70 }
71 else {
72 return;
73 }
74 }
75  
76 sub DELETE {
77 my ( $self, $index ) = @_;
78  
79 croak "DELETE forbidden for this type of array.";
80 }
81  
82 sub CLEAR {
83 my ( $self ) = @_;
84  
85 croak "CLEAR forbidden for this type of array.";
86 }
87  
88 sub PUSH {
89 my ( $self, @values ) = @_;
90  
91 croak "PUSH forbidden for this type of array.";
92 }
93  
94 sub UNSHIFT {
95 my ( $self, @values ) = @_;
96  
97 croak "UNSHIFT forbidden for this type of array.";
98 }
99  
100 sub POP {
101 my ( $self ) = @_;
102  
103 croak "POP forbidden for this type of array.";
104 }
105  
106 sub SHIFT {
107 my ( $self ) = @_;
108  
109 croak "SHIFT forbidden for this type of array.";
110 }
111  
112 sub SPLICE {
113 my ( $self, $offset, $length, @values ) = @_;
114  
115 croak "SPLICE forbidden for this type of array.";
116 }
117  
118 sub UNTIE {
119 my ( $self ) = @_;
120  
121 return;
122 }
123  
124 sub _load_name {
125 my ( $self, $name ) = @_;
126 my @addrs = Zonemaster::Engine::Recursor->get_addresses_for( $name );
127 foreach my $addr ( sort { $a->ip cmp $b->ip } @addrs ) {
128 my $ns = Zonemaster::Engine::Nameserver->new( { name => $name, address => $addr } );
129 if ( not grep { "$ns" eq "$_" } @{ $self->ary } ) {
130 push @{ $self->ary }, $ns;
131 }
132 }
133  
134 return;
135 }
136  
137 1;
138  
139 =head1 NAME
140  
141 Zonemaster::Engine::NSArray - Class implementing arrays that lazily looks up name server addresses from their names
142  
143 =head1 SYNOPSIS
144  
145 tie @ary, 'Zonemaster::Engine::NSArray', @ns_names
146  
147 =head1 DESCRIPTION
148  
149 This class is used for the C<glue> and C<ns> attributes of the
150 L<Zonemaster::Engine::Zone> class. It is initially seeded with a list of
151 names, which will be expanded into proper L<Zonemaster::Engine::Nameserver>
152 objects on demand. Be careful with using Perl functions that act on
153 whole arrays (particularly C<foreach>), since they will usually force
154 the entire array to expand, negating the use of the lazy-loading.
155  
156 =head1 METHODS
157  
158 These are all methods implementing the Perl tie interface. They have no independent use.
159  
160 =over
161  
162 =item TIEARRAY
163  
164 =item STORE
165  
166 =item STORESIZE
167  
168 =item FETCH
169  
170 =item FETCHSIZE
171  
172 =item EXISTS
173  
174 =item DELETE
175  
176 =item CLEAR
177  
178 =item PUSH
179  
180 =item UNSHIFT
181  
182 =item POP
183  
184 =item SHIFT
185  
186 =item SPLICE
187  
188 =item UNTIE
189  
190 =back
191  
192 =head1 AUTHOR
193  
194 Calle Dybedahl, C<< <calle at init.se> >>
195  
196 =cut