Transfigure/Predicate.pm

filter: Statementstmt 75.0% 6 / 8 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 81.8% 9 / 11 CC 1 SCAR 0.9
CC
1
Cov
75%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Transfigure/Default.pm ↑ Summary Transfigure/Type.pm »
1 package Data::Transfigure::Predicate 1.03;
2 1 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # ABSTRACT: a transfigurator that filters based on a predicate function
6  
7 =encoding UTF-8
8  
9 =head1 NAME
10  
11 Data::Transfigure::Type - a transfigurator that filters based on a predicate function
12  
13 =head1 DESCRIPTION
14  
15 C<Data::Transfigure::Predicate> is a transfigurator that uses a predicate
16 function to determine whether it applies. This allows extrinsic logic to be
17 applied to select appropriate transfigurators.
18  
19 =cut
20  
21 0 ▶use Object::Pad;
Subroutine BEGIN: called CC=1 SCAR=2.6
22  
23 class Data::Transfigure::Predicate : does(Data::Transfigure::Node) {
24 use Data::Transfigure::Constants;
25  
26 =head1 FIELDS
27  
28 =head2 predicate (required param)
29  
30 The predicate function. Must be a CODE refereence. Receives the node value and
31 position as parameters and returns a simple true/false value.
32  
33 =head2 transfigurator (required parameter)
34  
35 A C<Data::Transfigure> transfigurator conforming to the C<Data::Transfigure::Node>
36 role. Weird things will happen if you provide a
37 C<Data::Transfigure::Tree> -type transfigurator, so you probably shouldn't do
38 that.
39  
40 =cut
41  
42 field $predicate : param;
43 field $transfigurator : param;
44  
45 ADJUST {
46 die("predicate must be a CODEREF") unless (ref($predicate) eq 'CODE');
47 }
48  
49 sub BUILDARGS ($class, %params) {
50 $class->SUPER::BUILDARGS(
51 predicate => $params{predicate},
52 transfigurator => $params{transfigurator},
53 handler => sub (@args) {
54 $params{transfigurator}->transfigure(@args);
55 }
56 );
57 }
58  
59 =head1 METHODS
60  
61 =head2 applies_to( %params )
62  
63 Requires C<$params{value}> and C<$params{position}> to exist
64  
65 If C<$predicate> returns true when possed the value and position (in that order),
66 returns C<$MATCH_EXACT>.
67  
68 Otherwise returns C<$NO_MATCH>.
69  
70 =cut
71  
72 method applies_to (%params) {
73 die('value is a required parameter for Data::Transfigure::Predicate->applies_to') unless (exists($params{value}));
74 die('position is a required parameter for Data::Transfigure::Predicate->applies_to') unless (exists($params{position}));
75 my $node = $params{value};
76 my $position = $params{value};
77  
78 my $cbv = $predicate->($node, $position);
79  
80 return $transfigurator->applies_to(%params) if ($cbv);
81 return $NO_MATCH;
82 }
83  
84 }
85  
86 =pod
87  
88 =head1 AUTHOR
89  
90 Mark Tyrrell C<< <mark@tyrrminal.dev> >>
91  
92 =head1 LICENSE
93  
94 Copyright (c) 2024 Mark Tyrrell
95  
96 Permission is hereby granted, free of charge, to any person obtaining a copy
97 of this software and associated documentation files (the "Software"), to deal
98 in the Software without restriction, including without limitation the rights
99 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
100 copies of the Software, and to permit persons to whom the Software is
101 furnished to do so, subject to the following conditions:
102  
103 The above copyright notice and this permission notice shall be included in all
104 copies or substantial portions of the Software.
105  
106 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
107 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
108 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
109 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
110 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
111 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
112 SOFTWARE.
113  
114 =cut
115  
116 1;
117  
118 __END__
« Transfigure/Default.pm ↑ Summary Transfigure/Type.pm »