Profile/Payload/Tie/Dict.pm

filter: Statementstmt 97.6% 42 / 43 Branchbran 75.0% 3 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 14 / 14 Podpod n/a totaltotal 96.7% 59 / 61 CC 3 SCAR 2.3
CC
3
Cov
96%
TIEHASH
2.0
STORE
2.0
BEGIN
1.0
CRAP 1.3
« Profile/Payload/Certificate/Root.pm ↑ Summary Profile.pm »
1 # This is the code for Config::Apple::Profile::Payload::Tie::Dict.
2 # For Copyright, please see the bottom of the file.
3  
4 package Config::Apple::Profile::Payload::Tie::Dict;
5  
6 16 ▶use 5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 16 our $VERSION = '0.87.1';
11  
12 16 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 16 ▶use Tie::Hash; # Also gives us Tie::StdHash
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 =encoding utf8
17  
18 =head1 NAME
19  
20 Config::Apple::Profile::Payload::Tie::Dict - Tying class for dictionaries
21 of things.
22  
23 =head1 DESCRIPTION
24  
25 This class is used to store a dictionary (a I<dict>) of I<things>. Exactly
26 what I<things> are being stored is specified at the time the tie is made.
27  
28 There are several payload types that contain dicts of things. For example,
29 if you have a Wi-Fi network that uses WPA- or WPA2-Enterprise, then some form
30 of EAP will be used, and the EAP parameters are stored in a dictionary.
31  
32 This class is used by payload classes to represent a dictionary.
33  
34 =cut
35  
36 =head2 "CLASS" METHODS
37  
38 =head3 tie %hash, 'Config::Apple::Profile::Payload::Tie::Dict', $validator
39  
40 When this class is tied to an hash, C<TIEHASH> will be called, with the class
41 name as the first argument.
42  
43 C<$validator> is a reference to a function that will be able to validate
44 values that are stored in the dict. The validator will be passed the value as
45 the only parameter, and an untained value is expected as the return value.
46 If C<undef> is returned by the validator, then the value was invalid, and the
47 store attempt will fail.
48  
49 It is suggested that the functions from
50 L<Config::Apple::Profile::Payload::Types::Validation> be used.
51  
52 If C<$validator> is not a valid coderef then an exception will be thrown.
53  
54 =cut
55  
56 22 ▶sub TIEHASH {
Subroutine TIEHASH: called CC=2 SCAR=7.1
57 22 my ($class, $validator) = @_;
58
59 # This is what we'll eventually return
60 22 my %object;
61
62 # We'll still have an array, for convenience
63 22 $object{dict} = {};
64
65 # We don't accept refs, only scalars
66 22 ▶ if (ref $validator ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $validator ne "CODE") 0 22 22
67 0 die "Validator must be a function reference";
68 }
69 22 $object{validator} = $validator;
70  
71 22 return bless \%object, $class;
72 }
73  
74  
75 =head3 FETCH
76  
77 Works as one would expect with a Perl hash. Returns the entry matching the
78 specified key.
79  
80 =cut
81  
82 87 ▶sub FETCH {
Subroutine FETCH: called CC=1 SCAR=0.0
83 87 my ($self, $key) = @_;
84
85 87 return $self->{hash}->{$key};
86 }
87  
88  
89 =head3 STORE
90  
91 Works almost as one would expect with a Perl hash. Stores a value at the
92 specified key. The value will only be stored if it is valid; otherwise an
93 exception will be thrown. C<undef> is not a valid value to store.
94  
95 =cut
96  
97 41 ▶sub STORE {
Subroutine STORE: called CC=2 SCAR=6.9
98 41 my ($self, $key, $value) = @_;
99
100 # Call the validation routine
101 # If the validated value is undef, it was invalid
102 41 my $validated_value = $self->{validator}->($value);
103 40 ▶ if (!defined $validated_value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $validated_value) 9 31 40
104 9 die "Attempting to insert invalid value";
105 }
106
107 31 $self->{hash}->{$key} = $self->{validator}->($validated_value);
108 }
109  
110  
111 =head3 delete
112  
113 Works as one would expect with a Perl hash. Deletes the specified key from
114 the hash.
115  
116 =cut
117  
118 4 ▶sub DELETE {
Subroutine DELETE: called CC=1 SCAR=0.0
119 4 my ($self, $key) = @_;
120 4 delete $self->{hash}->{$key};
121 }
122  
123  
124 =head3 clear
125  
126 Works as one would expect with a Perl hash. Deletes all keys from the hash.
127  
128 =cut
129  
130 1 ▶sub CLEAR {
Subroutine CLEAR: called CC=1 SCAR=0.0
131 1 my ($self) = @_;
132 1 $self->{hash} = {};
133 }
134  
135  
136 =head3 exists
137  
138 Works as expected for a Perl hash. Returns true if the specified key exists
139 in the hash.
140  
141 =cut
142  
143 16 ▶sub EXISTS {
Subroutine EXISTS: called CC=1 SCAR=0.0
144 16 my ($self, $key) = @_;
145 16 return exists $self->{hash}->{$key};
146 }
147  
148  
149 =head3 FIRSTKEY
150  
151 Used as part of C<keys()> and C<each()>. Works as expected for a Perl hash.
152 Returns the first key in the hash.
153  
154 =cut
155  
156 23 ▶sub FIRSTKEY {
Subroutine FIRSTKEY: called CC=1 SCAR=0.0
157 23 my ($self) = @_;
158 # Let's defer to Tie::StdHash for this.
159 23 return Tie::StdHash::FIRSTKEY($self->{hash});
160 }
161  
162  
163 =head3 NEXTKEY
164  
165 Used as part of C<keys()> and C<each()>. Works as expected for a Perl hash.
166 Returns the next key in the hash.
167  
168 =cut
169  
170 227 ▶sub NEXTKEY {
Subroutine NEXTKEY: called CC=1 SCAR=0.0
171 227 my ($self, $lastkey) = @_;
172 # Let's defer to Tie::StdHash for this.
173 227 return Tie::StdHash::NEXTKEY($self->{hash});
174 }
175  
176  
177 =head3 scalar
178  
179 Works as expected, returning the number of keys in the hash.
180  
181 =cut
182  
183 1 ▶sub SCALAR {
Subroutine SCALAR: called CC=1 SCAR=0.0
184 1 my ($self) = @_;
185 1 return scalar %{$self->{hash}};
186 }
187  
188  
189 =head1 ACKNOWLEDGEMENTS
190  
191 Refer to L<Config::Apple::Profile> for acknowledgements.
192  
193 =head1 AUTHOR
194  
195 A. Karl Kornel, C<< <karl at kornel.us> >>
196  
197 =head1 COPYRIGHT AND LICENSE
198  
199 Copyright © 2014 A. Karl Kornel.
200  
201 This program is free software; you can redistribute it and/or modify it
202 under the terms of either: the GNU General Public License as published
203 by the Free Software Foundation; or the Artistic License.
204  
205 See L<http://dev.perl.org/licenses/> for more information.
206  
207 =cut
208  
209 16 1;
« Profile/Payload/Certificate/Root.pm ↑ Summary Profile.pm »