Transfigure/Position.pm untested

filter: Statementstmt 0.0% 0 / 33 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 51 CC 1 SCAR 6.9
CC
1
Cov
0%
wildcard_to_regex
2.0
BUILDARGS
2.0
BEGIN
2.0
CRAP 2.0
1 package Data::Transfigure::Position 1.03;
2 use v5.26;
3 use warnings;
4  
5 # ABSTRACT: a compound transfigurator that specifies one or more locations within the data structure to apply to
6  
7 =head1 NAME
8  
9 Data::Transfigure::Position - a compound transfigurator that specifies one or more
10 locations within the data structure to apply to
11  
12 =head1 SYNOPSIS
13  
14 Data::Transfigure::Position->new(
15 position => '/*/author',
16 transfigurator => Data::Transfigure::Type->new(
17 type => 'Result::Person',
18 handler => sub ($data) {
19 sprintf("%s, %s", $data->lastname, $data->firstname)
20 }
21 )
22 ); # applies to any 2nd-level hash key "author", but only if that value's
23 # type is 'Result::Person', and then performs a custom stringification
24  
25 Data::Transfigure::Position->new(
26 position => '/book/author',
27 transfigurator => Data::Transfigure::Default->new(
28 handler => sub ($data ) {
29 {
30 firstname => $data->names->{first} // '',
31 lastname => $data->names->{last} // '',
32 }
33 }
34 )
35 ); # applies only to the node at $data->{book}->{author}, and tries to
36 # hash-ify the value there, regardless of its type.
37  
38 =head1 DESCRIPTION
39  
40 C<Data::Transfigure::Position> is a compound transfigurator, meaning that it both is,
41 and has, a transfigurator. The transfigurator you give it at construction can be of
42 any type, with its own handler and match criteria. The
43 C<Data::Transfigure::Position>'s handler will become the one from the supplied
44 transfigurator, so C<handler> should not be specified when creating this
45 transfigurator.
46  
47 This construction is used so that transfigurators can be treated like building
48 blocks and in some cases inserted to apply to the entire tree, but in other
49 scenarios, used much more specifically.
50  
51 =cut
52  
53 use Object::Pad;
54  
55 class Data::Transfigure::Position : does(Data::Transfigure::Node) {
56 use Data::Transfigure::Constants;
57  
58 =head1 FIELDS
59  
60 =head2 position (required parameter)
61  
62 Ex. C<"/book/author">, C<"/*/*/title">, C<"/values/0/id">
63  
64 A position specifier for a location in the data structure. The forward slash
65 character is used to delineate levels, which are hashes or arrays. The asterisk
66 character can be used to represent "anything" at that level, whether hash key
67 or array index.
68  
69 Cam be an arrayref of position specifiers to match any of them.
70  
71 =head2 transfigurator (required parameter)
72  
73 A C<Data::Transfigure> transfigurator conforming to the C<Data::Transfigure::Node>
74 role. Weird things will happen if you provide a
75 C<Data::Transfigure::Tree> -type transfigurator, so you probably shouldn't do
76 that.
77  
78 =cut
79  
80 field $position : param;
81 field $transfigurator : param;
82  
83 my sub wildcard_to_regex ($str) {
84 $str =~ s|[.]|\\.|g;
85 $str =~ s|[*]{2}|.*|g;
86 $str =~ s|(?<![.])[*]|[^/]*|g;
87 return qr/^$str$/;
88 }
89  
90 sub BUILDARGS ($class, %params) {
91 $class->SUPER::BUILDARGS(
92 position => $params{position},
93 transfigurator => $params{transfigurator},
94 handler => sub (@args) {
95 $params{transfigurator}->transfigure(@args);
96 }
97 );
98 }
99  
100 =head1 applies_to( %params )
101  
102 C<$params{position}> must exist, as well as any params required by the supplied
103 transfigurator.
104  
105 Passes C<%params> to the instance's transfigurator's C<applies_to> method - if that
106 results in C<$NO_MATCH>, then that value is returned by this method.
107  
108 Then, the C<position(s)> is/are checked against C<$params{position}>. If
109 any matches exactly, returns C<$MATCH_EXACT_POSITION>. Otherwise, if any matches
110 with wildcard evaluation, returns C<$MATCH_WILDCARD_POSITION>.
111  
112 If no positions match, returns C<$NO_MATCH>
113  
114 =cut
115  
116 method applies_to(%params) {
117 die('position is a required parameter for Data::Transfigure::Position->applies_to') unless (exists($params{position}));
118 my $loc = $params{position};
119  
120 my $rv = $NO_MATCH;
121 my $tf_match = $transfigurator->applies_to(%params);
122 return $rv if ($tf_match == $rv);
123 my @paths = ref($position) eq 'ARRAY' ? $position->@* : ($position);
124  
125 PATH: foreach (@paths) {
126 return $MATCH_EXACT_POSITION | $tf_match if ($loc eq $_);
127 my $re = wildcard_to_regex($_);
128 $rv = $MATCH_WILDCARD_POSITION | $tf_match if ($loc =~ $re);
129 }
130 return $rv;
131 }
132  
133 }
134  
135 =pod
136  
137 =head1 AUTHOR
138  
139 Mark Tyrrell C<< <mark@tyrrminal.dev> >>
140  
141 =head1 LICENSE
142  
143 Copyright (c) 2024 Mark Tyrrell
144  
145 Permission is hereby granted, free of charge, to any person obtaining a copy
146 of this software and associated documentation files (the "Software"), to deal
147 in the Software without restriction, including without limitation the rights
148 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
149 copies of the Software, and to permit persons to whom the Software is
150 furnished to do so, subject to the following conditions:
151  
152 The above copyright notice and this permission notice shall be included in all
153 copies or substantial portions of the Software.
154  
155 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
156 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
157 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
158 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
159 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
160 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
161 SOFTWARE.
162  
163 =cut
164  
165 1;
166  
167 __END__