DHCPd/Types.pm

filter: Statementstmt 75.0% 63 / 84 Branchbran 25.0% 5 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 73.9% 17 / 23 Podpod 100% 3 / 3 totaltotal 67.6% 88 / 130 CC 13 SCAR 18.2
CC
13
Cov
65%
from_State
20.0
from_FailoverState
20.0
__ANON__
6.0
CRAP 6.2
« DHCPd/Config/SubClass.pm ↑ Summary DHCPd/Config/Zone.pm »
1 package Net::ISC::DHCPd::Types;
2  
3 =head1 NAME
4  
5 Net::ISC::DHCPd::Types - Moose type constraint declaration
6  
7 =head1 SYNOPSIS
8  
9 use Net::ISC::DHCPd::Types @types;
10 has foo => ( isa => SomeType, ... );
11  
12 =cut
13  
14 29 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 29 ▶use MooseX::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 29 ▶use MooseX::Types::Moose ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 29 my @type_list;
19 29 my @failover_states = (
20 "na", "partner down",
21 "normal", "communications interrupted",
22 "resolution interrupted", "potential conflict",
23 "recover", "recover done",
24 "shutdown", "paused",
25 "startup", "recover wait",
26 );
27 29 my @states = qw/
28 na free active expired released
29 abandoned reset backup reserved bootp
30 /;
31  
32 29 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
33 29 MooseX::Types->import(-declare => [@type_list = qw/
34 HexInt Ip Mac State Time Statements FailoverState
35 ConfigObject LeasesObject OMAPIObject ProcessObject
36 /]);
37 }
38  
39 29 my $MAC_REGEX = '^'. join(':', (q{[0-9a-f]{1,2}}) x 6) . '$';
40  
41 =head1 TYPES
42  
43 =head2 HexInt
44  
45 =head2 Ip
46  
47 =head2 Mac
48  
49 =head2 State
50  
51 =head2 Time
52  
53 =cut
54  
55 subtype State, as Str,
56 24 ▶ where { my $s = $_; return grep { $s eq $_ } @states };
Subroutine __ANON__: called CC=1 SCAR=0.0
57 subtype FailoverState, as Str,
58 0 ▶ where { my $s = $_; return grep { $s eq $_ } @failover_states };
Subroutine __ANON__: not called CC=1 SCAR=3.5
59 29 subtype HexInt, as Int;
60 subtype Ip, as Str,
61 24 ▶ where { /^[\d.]+$/ };
Subroutine __ANON__: called CC=1 SCAR=0.0
62 subtype Mac, as Str,
63 52 ▶ where { /$MAC_REGEX/i };
Subroutine __ANON__: called CC=1 SCAR=0.0
64  
65 29 subtype Time, as Int;
66  
67 coerce State, (
68 1 ▶ from Str, via { /(\d+)$/ ? $states[$1] : undef }
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
/(\d+)$/ ? : 1 0 1
69 29 );
70  
71 =head2 from_State
72  
73 =cut
74  
75 0 ▶sub from_State {
Subroutine from_State: not called CC=4 SCAR=30.0
Pod documented
76 0 my $self = shift;
77 0 my $attr = shift;
78 0 ▶ my $value = $self->$attr or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $value = $self->$attr or return 0 0 0 0
79  
80 0 for my $i (0..@states) {
81 0 ▶ return $i if($states[$i] eq $value);
Branch: 1 branch
BranchTrueFalseTotal
if $states[$i] eq $value 0 0 0
82 }
83  
84 0 return 0;
85 }
86  
87 29 coerce FailoverState, (
88 from Str
89 );
90  
91 =head2 from_FailoverState
92  
93 =cut
94  
95 0 ▶sub from_FailoverState {
Subroutine from_FailoverState: not called CC=4 SCAR=30.0
Pod documented
96 0 my $self = shift;
97 0 my $attr = shift;
98 0 ▶ my $value = $self->$attr or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $value = $self->$attr or return 0 0 0 0
99  
100 0 for my $i (0..@failover_states) {
101 0 ▶ return $i if($failover_states[$i] eq $value);
Branch: 1 branch
BranchTrueFalseTotal
if $failover_states[$i] eq $value 0 0 0
102 }
103  
104 0 return 0;
105 }
106  
107 coerce HexInt, (
108 1 ▶ from Str, via { s/://g; hex $_ },
Subroutine __ANON__: called CC=1 SCAR=0.0
109 29 );
110  
111 coerce Ip, (
112 1 ▶ from Str, via { join '.', map { hex $_ } split /:/ },
Subroutine __ANON__: called CC=1 SCAR=0.0
113 29 );
114  
115 coerce Mac, (
116 3 ▶ from Str, via {
Subroutine __ANON__: called CC=2 SCAR=6.9
117 3 my @mac = split /[\-\.:]/;
118 3 ▶ my $format = scalar @mac == 3 ? '%04s' : '%02s';
Branch: 1 branch
BranchTrueFalseTotal
scalar @mac == 3 ? : 1 2 3
119 3 my $str = join '', map { sprintf $format, $_ } @mac; # fix single digits 0:x:ff:00:00:01
120 # the following line handles mac addresses in the format of 123456789101
121 # or any other weirdness the above didn't take care of.
122 3 $_ = $str;
123 3 return join ':', /(\w\w)/g; # rejoin with colons
124 },
125 29 );
126  
127 coerce Time, (
128 1 ▶ from Str, via { s/://g; hex $_ },
Subroutine __ANON__: called CC=1 SCAR=0.0
129 29 );
130  
131 =head2 Statements
132  
133 =cut
134  
135 3 ▶subtype Statements, as Str, where { /^[\w,]+$/ };
Subroutine __ANON__: called CC=1 SCAR=0.0
136  
137 coerce Statements, (
138 0 ▶ from Str, via { s/\s+/,/g; $_; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
139 1 ▶ from ArrayRef, via { join ",", @$_ },
Subroutine __ANON__: called CC=1 SCAR=0.0
140 29 );
141  
142 =head2 ConfigObject
143  
144 =head2 LeasesObject
145  
146 =head2 OMAPIObject
147  
148 =head2 ProcessObject
149  
150 =cut
151  
152 29 subtype ConfigObject, as Object;
153 29 subtype LeasesObject, as Object;
154 29 subtype OMAPIObject, as Object;
155 29 subtype ProcessObject, as Object;
156  
157 4 ▶coerce ConfigObject, from HashRef, via {
Subroutine __ANON__: called CC=2 SCAR=7.2
158 4 ▶ eval "require Net::ISC::DHCPd::Config" or confess $@;
Branch: 1 branch
BranchTrueFalseTotal
eval "require Net::ISC::DHCPd::Config" or confess($@) 4 0 4
159 4 Net::ISC::DHCPd::Config->new($_);
160 29 };
161 4 ▶coerce LeasesObject, from HashRef, via {
Subroutine __ANON__: called CC=2 SCAR=7.2
162 4 ▶ eval "require Net::ISC::DHCPd::Leases" or confess $@;
Branch: 1 branch
BranchTrueFalseTotal
eval "require Net::ISC::DHCPd::Leases" or confess($@) 4 0 4
163 4 Net::ISC::DHCPd::Leases->new($_);
164 29 };
165 0 ▶coerce OMAPIObject, from HashRef, via {
Subroutine __ANON__: not called CC=2 SCAR=17.9
166 0 ▶ eval "require Net::ISC::DHCPd::OMAPI" or confess $@;
Branch: 1 branch
BranchTrueFalseTotal
eval "require Net::ISC::DHCPd::OMAPI" or confess($@) 0 0 0
167 0 Net::ISC::DHCPd::OMAPI->new($_);
168 29 };
169 0 ▶coerce ProcessObject, from HashRef, via {
Subroutine __ANON__: not called CC=2 SCAR=17.9
170 0 ▶ eval "require Net::ISC::DHCPd::Process" or confess $@;
Branch: 1 branch
BranchTrueFalseTotal
eval "require Net::ISC::DHCPd::Process" or confess($@) 0 0 0
171 0 Net::ISC::DHCPd::Process->new($_);
172 29 };
173  
174 =head2 get_type_list
175  
176 @names = $class->get_type_list;
177  
178 Returns the types defined in this package.
179  
180 =cut
181  
182 8 ▶sub get_type_list {
Subroutine get_type_list: called CC=1 SCAR=0.0
Pod documented
183 8 return @type_list;
184 }
185  
186 =head1 COPYRIGHT & LICENSE
187  
188 =head1 AUTHOR
189  
190 See L<Net::ISC::DHCPd>.
191  
192 =cut
193 29 __PACKAGE__->meta->make_immutable;
194 29 1;
« DHCPd/Config/SubClass.pm ↑ Summary DHCPd/Config/Zone.pm »