QBit/Validator/PathManager.pm untested

filter: Statementstmt 0.0% 0 / 48 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 88 CC 6 SCAR 17.2
CC
6
Cov
0%
get_path_part
12.0
get_data_by_path
12.0
get_absolute_path
6.0
CRAP 5.6
1 package QBit::Validator::PathManager;
2 $QBit::Validator::PathManager::VERSION = '0.012';
3 use qbit;
4  
5 use base qw(QBit::Class);
6  
7 use Exception::Validator::PathManager;
8  
9 sub root {'/'}
10  
11 sub delimiter {'/'}
12  
13 sub concatenate {"$_[1]$_[3]$_[2]"}
14  
15 sub hash_path {$_[1]}
16  
17 sub array_path {$_[1]}
18  
19 sub get_absolute_path {
20 my ($self, $path, $root_path) = @_;
21  
22 my $root = $self->root;
23  
24 return $path if $path =~ /^\Q$root\E/;
25  
26 my $delimiter = $self->delimiter;
27  
28 $root_path //= $root;
29  
30 return $self->concatenate($root_path, $delimiter, $path);
31 }
32  
33 sub get_path_part {
34 my ($self, $type, $value) = @_;
35  
36 if ($type eq 'hash') {
37 $value =~ s/%/%%/g;
38  
39 return $self->hash_path($value);
40 } elsif ($type eq 'array') {
41 return $self->array_path($value);
42 } else {
43 throw Exception::Validator::PathManager gettext('Unknown method: %s', $type);
44 }
45 }
46  
47 sub set_dynamic_part {push(@{$_[0]->{'__DYNAMIC__'}}, $_[1])}
48  
49 sub reset_dynamic_part {pop(@{$_[0]->{'__DYNAMIC__'}})}
50  
51 sub get_current_node_path {
52 sprintf($_[1], map {$$_} @{$_[0]->{'__DYNAMIC__'}});
53 }
54  
55 sub get_data_by_path {
56 my ($self, $path, $data) = @_;
57  
58 my $root = $self->root;
59 $path =~ s/^\Q$root\E//;
60  
61 $path =~ s/(?<!\.)\.(?!\.)\/?//g;
62 while ($path =~ s/[0-9a-zA-Z_]+\/\.\.\/?//) { }
63  
64 my @parts = split($self->delimiter, $path);
65  
66 my $current = $data;
67 foreach (@parts) {
68 if (ref($current) eq 'HASH') {
69 $current = $current->{$_};
70 } elsif (ref($current) eq 'ARRAY') {
71 $current = $current->[$_];
72 } else {
73 return $current;
74 }
75 }
76  
77 return $current;
78 }
79  
80 TRUE;
81  
82 =encoding utf8
83  
84 =head1 Name
85  
86 QBit::Validator::PathManager - path manager. It's works with simple hash keys only.
87  
88 $key =~ /^[0-9a-zA-Z_]+\z/
89  
90 =head1 Package methods
91  
92 =head2 new
93  
94 create object QBit::Validator::PathManager
95  
96 B<Example:>
97  
98 my $path_manager = QBit::Validator::PathManager->new();
99  
100 =head2 root
101  
102 returns root symbol (default: '/')
103  
104 B<Example:>
105  
106 my $root = $path_manager->root();
107  
108 =head2 delimiter
109  
110 return delimiter (default: '/')
111  
112 B<Example:>
113  
114 my $delimiter = $path_manager->delimiter();
115  
116 =head2 concatenate
117  
118 concatenate root path, delimiter and path part
119  
120 B<Example:>
121  
122 my $root_path = '/key';
123  
124 my $path = $path_manager->concatenate($root_path, $path_manager->delimiter, 'key2');
125 # /key/key2
126  
127 =head2 hash_path
128  
129 returns path for hash key (default: as it is). You can use this method for escape a hash key if needed.
130  
131 B<Example:>
132  
133 my $hash_path = $path_manager->hash_path('field');
134 # 'field'
135  
136 =head2 array_path
137  
138 returns path for array index (default: as it is). You can use this method for escape a array index if needed.
139  
140 B<Example:>
141  
142 my $array_path = $path_manager->array_path(0);
143 # 0
144  
145 =head2 get_data_by_path
146  
147 returns data by path. No difference between hash key and array index in path. Hash key must satisfy regex
148  
149 /^[0-9a-zA-Z_]+\z/.
150  
151 Path consists of:
152  
153 =over
154  
155 =item
156  
157 B<root symbol> - '/'
158  
159 =item
160  
161 B<delimiter> - '/'
162  
163 =item
164  
165 B<current element> - '.'
166  
167 =item
168  
169 B<parent> - '..'
170  
171 =back
172  
173 B<Example:>
174  
175 my $data = {
176 key => 1,
177 key2 => [
178 2,
179 3,
180 ],
181 };
182  
183 $path_manager->get_data_by_path('/', $data); # $data
184 $path_manager->get_data_by_path('/key', $data); # 1
185 $path_manager->get_data_by_path('/key/.', $data); # 1
186 $path_manager->get_data_by_path('/key2', $data); # [2, 3]
187 $path_manager->get_data_by_path('/key2/0', $data); # 2
188 $path_manager->get_data_by_path('/key2/../key', $data); # 1
189  
190 =head1 Your path manager
191  
192 You can write path manager for your favoriet data manager (Data::DPath, JSON::Pointer, JSON::Path, etc)
193  
194 B<Example:>
195  
196 package QBit::Validator::PathManager::Data::DPath;
197  
198 use base qw(QBit::Validator::PathManager);
199  
200 use Data::DPath qw(dpath);
201 use String::Escape qw(qqbackslash);
202  
203 sub hash_path {qqbackslash($_[1])}
204  
205 sub array_path {'[$_[1]]'}
206  
207 sub get_data_by_path {my @result = dpath($_[1])->match($_[2]); return $result[0];}
208  
209 1;